<?php

declare(strict_types=1);

namespace App\Domain\SmartLink\Entities;

use App\Domain\SmartLink\Enums\ServiceType;
use App\Domain\SmartLink\ValueObjects\DestinationUrl;
use App\Domain\SmartLink\ValueObjects\SmartLinkId;
use App\Framework\Core\ValueObjects\CountryCode;
use App\Framework\Core\ValueObjects\Timestamp;
final readonly class GeoRule
{
    public function __construct(
        public string $id,
        public SmartLinkId $linkId,
        public CountryCode $countryCode,
        public ServiceType $serviceType,
        public DestinationUrl $url,
        public Timestamp $createdAt
    ) {
    }

    public static function create(
        SmartLinkId $linkId,
        CountryCode $countryCode,
        ServiceType $serviceType,
        DestinationUrl $url
    ): self {
        return new self(
            id: uniqid('geo_', true),
            linkId: $linkId,
            countryCode: $countryCode,
            serviceType: $serviceType,
            url: $url,
            createdAt: Timestamp::now()
        );
    }

    public function matchesCountry(CountryCode $country): bool
    {
        return $this->countryCode->equals($country);
    }

    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'link_id' => $this->linkId->toString(),
            'country_code' => (string) $this->countryCode,
            'country_name' => $this->countryCode->getCountryName(),
            'service_type' => $this->serviceType->value,
            'service_label' => $this->serviceType->getLabel(),
            'url' => $this->url->toString(),
            'created_at' => $this->createdAt->format('Y-m-d H:i:s'),
        ];
    }
}
