<?php

declare(strict_types=1);

namespace App\Domain\Meta\Http\Request;

use App\Framework\Validation\Attributes\Max;
use App\Framework\Validation\Attributes\MaxLength;
use App\Framework\Validation\Attributes\Min;
use App\Framework\Validation\Attributes\Required;
use App\Framework\Validation\Attributes\Url;

final class MetaRequest
{
    #[MaxLength(255)]
    public ?string $routePattern = null;

    #[MaxLength(100)]
    public ?string $entityType = null;

    #[Min(1)]
    public ?int $entityId = null;

    #[Required]
    #[MaxLength(255)]
    public ?string $title = null;

    #[MaxLength(500)]
    public ?string $description = null;

    public ?array $keywords = null;

    #[MaxLength(255)]
    public ?string $ogTitle = null;

    #[MaxLength(500)]
    public ?string $ogDescription = null;

    #[Url]
    #[MaxLength(500)]
    public ?string $ogImage = null;

    #[MaxLength(50)]
    public ?string $ogType = null;

    #[MaxLength(50)]
    public ?string $twitterCard = null;

    #[MaxLength(100)]
    public ?string $twitterSite = null;

    #[Url]
    #[MaxLength(500)]
    public ?string $canonical = null;

    public ?array $customMeta = null;

    #[Min(0)]
    #[Max(999)]
    public int $priority = 0;

    public bool $active = true;

    public function getKeywordsAsArray(): ?array
    {
        if ($this->keywords === null) {
            return null;
        }

        if (is_array($this->keywords)) {
            return $this->keywords;
        }

        if (is_string($this->keywords)) {
            return array_filter(array_map('trim', explode(',', $this->keywords)));
        }

        return null;
    }
}
