<?php

namespace App\Base\Config;

use Symfony\Component\DependencyInjection\Attribute\Autowire;

class AppConfig implements ConfigInterface
{
    private array $messages = [];

    public function __construct(
        #[Autowire(param: 'app.env')]
        private readonly ?string $appEnv = null,

        #[Autowire(param: 'app.url')]
        private readonly ?string $appUrl = null,

        #[Autowire(param: 'app.debrid.real_debrid.key')]
        private readonly ?string $realDebridApiKey = null,

        #[Autowire(param: 'app.meta_provider.tmdb.key')]
        private readonly ?string $tmdbApiKey = null,

        #[Autowire(param: 'media.movies_path')]
        private readonly ?string $moviesPath = null,

        #[Autowire(param: 'media.tvshows_path')]
        private readonly ?string $tvshowsPath = null,
    ) {}

    public function isValid(): bool
    {
        $valid = true;

        if (false === $this->isVariableValid('APP_ENV', $this->appEnv)) {
            $valid = false;
        }

        if (false === $this->isVariableValid('APP_URL', $this->appUrl)) {
            $valid = false;
        }

        if (false === $this->isVariableValid('REAL_DEBRID_KEY', $this->realDebridApiKey)) {
            $valid = false;
        }

        if (false === $this->isVariableValid('TMDB_API', $this->tmdbApiKey)) {
            $valid = false;
        }

        if (false === $this->isVariableValid('MOVIES_PATH', $this->moviesPath)) {
            $valid = false;
        }

        if (false === $this->isVariableValid('TVSHOWS_PATH', $this->tvshowsPath)) {
            $valid = false;
        }

        return $valid;
    }

    public function getMessages(): array
    {
        return $this->messages;
    }

    private function isVariableValid($key, $value): bool
    {
        if ("" === $value || null === $value) {
            $this->messages[] = "Your system is misconfigured. Please set the $key environment variable appropriately.";
            return false;
        }
        return true;
    }

    public function getAppEnv(): ?string
    {
        return $this->appEnv;
    }

    public function getAppUrl(): ?string
    {
        return $this->appUrl;
    }

    public function getRealDebridApiKey(): ?string
    {
        return $this->realDebridApiKey;
    }

    public function getTmdbApiKey(): ?string
    {
        return $this->tmdbApiKey;
    }

    public function getMoviesPath(): ?string
    {
        return $this->moviesPath;
    }

    public function getTvshowsPath(): ?string
    {
        return $this->tvshowsPath;
    }
}
