<?php
declare(strict_types=1);

namespace ROITheme\Shared\Domain\ValueObjects;

/**
 * Value Object que representa los tipos de página válidos
 *
 * @package ROITheme\Shared\Domain\ValueObjects
 */
final class PageType
{
    public const HOME = 'home';
    public const POST = 'post';
    public const PAGE = 'page';
    public const ARCHIVE = 'archive';
    public const SEARCH = 'search';
    public const UNKNOWN = 'unknown';

    private const VALID_TYPES = [
        self::HOME,
        self::POST,
        self::PAGE,
        self::ARCHIVE,
        self::SEARCH,
        self::UNKNOWN,
    ];

    private function __construct(
        private readonly string $value
    ) {}

    public static function fromString(string $type): self
    {
        if (!in_array($type, self::VALID_TYPES, true)) {
            return new self(self::UNKNOWN);
        }
        return new self($type);
    }

    public static function home(): self
    {
        return new self(self::HOME);
    }

    public static function post(): self
    {
        return new self(self::POST);
    }

    public static function page(): self
    {
        return new self(self::PAGE);
    }

    public static function archive(): self
    {
        return new self(self::ARCHIVE);
    }

    public static function search(): self
    {
        return new self(self::SEARCH);
    }

    public function value(): string
    {
        return $this->value;
    }

    public function equals(self $other): bool
    {
        return $this->value === $other->value;
    }

    /**
     * Retorna el nombre del campo de visibilidad correspondiente
     */
    public function toVisibilityField(): string
    {
        return match ($this->value) {
            self::HOME => 'show_on_home',
            self::POST => 'show_on_posts',
            self::PAGE => 'show_on_pages',
            self::ARCHIVE => 'show_on_archives',
            self::SEARCH => 'show_on_search',
            default => 'show_on_posts',
        };
    }
}
