<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Twig\Components\Legal;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Twig\Components\Behavior\HasTemplateOverride;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\Twig\LegalContentRenderer;
use CpCreation\VitiCore\Contact\Model\Contact;
use CpCreation\VitiCore\Contact\Repository\ContactRepository;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\PostMount;

#[AsTwigComponent('legal:Page', template: '@components/legal/Page.html.twig')]
final class Page
{
    use HasTemplateOverride;

    public const TYPE_LEGAL = 'legal';
    public const TYPE_CGV = 'cgv';
    public const TYPE_PRIVACY_POLICY = 'privacy_policy';

    public string $type = self::TYPE_LEGAL;

    private ?Contact $contact = null;
    private bool $contactLoaded = false;

    public function __construct(
        private readonly ContactRepository $contactRepository,
        private readonly LegalContentRenderer $contentRenderer,
    ) {}

    public function mount(?Contact $contact = null): void
    {
        if ($contact instanceof Contact) {
            $this->contact = $contact;
            $this->contactLoaded = true;
        }
    }

    #[PostMount]
    public function postMount(): void
    {
        if (!\in_array($this->type, self::getAllowedTypes(), true)) {
            throw new \InvalidArgumentException(\sprintf('Unknown legal page type "%s".', $this->type));
        }

        $this->loadContact();
    }

    public function getContact(): ?Contact
    {
        return $this->loadContact();
    }

    public function getCgvContent(): string
    {
        return $this->renderContent($this->loadContact()?->getCgv());
    }

    public function getLegalNoticeContent(): string
    {
        return $this->renderContent($this->loadContact()?->getLegalNoticeContent());
    }

    public function getPrivacyPolicyContent(): string
    {
        return $this->renderContent($this->loadContact()?->getPrivacyPolicyContent());
    }

    public function isLegal(): bool
    {
        return self::TYPE_LEGAL === $this->type;
    }

    public function isCgv(): bool
    {
        return self::TYPE_CGV === $this->type;
    }

    public function isPrivacyPolicy(): bool
    {
        return self::TYPE_PRIVACY_POLICY === $this->type;
    }

    public function getTitleKey(): string
    {
        return match ($this->type) {
            self::TYPE_CGV => 'cgv.title',
            self::TYPE_PRIVACY_POLICY => 'privacy_policy.title',
            default => 'legals.title',
        };
    }

    public function getIntroKey(): string
    {
        return match ($this->type) {
            self::TYPE_CGV => 'legal_pages.cgv.intro',
            self::TYPE_PRIVACY_POLICY => 'legal_pages.privacy_policy.intro',
            default => 'legal_pages.legal.intro',
        };
    }

    /**
     * @return list<string>
     */
    private static function getAllowedTypes(): array
    {
        return [
            self::TYPE_LEGAL,
            self::TYPE_CGV,
            self::TYPE_PRIVACY_POLICY,
        ];
    }

    private function resolveContact(): ?Contact
    {
        $contact = $this->contactRepository->findOne();

        return $contact instanceof Contact ? $contact : null;
    }

    private function loadContact(): ?Contact
    {
        if ($this->contactLoaded) {
            return $this->contact;
        }

        $this->contactLoaded = true;

        $this->contact = $this->resolveContact();

        return $this->contact;
    }

    private function renderContent(?string $content): string
    {
        $contact = $this->loadContact();

        return $contact instanceof Contact ? $this->contentRenderer->render($content, $contact) : '';
    }
}
