<?php

declare(strict_types=1);

namespace App\Infrastructure\Api\RapidMail\ReadModels;

use App\Infrastructure\Api\RapidMail\RecipientListId;

/**
 * Read Model für RecipientLists - immer mit ID
 */
final readonly class RecipientList
{
    public function __construct(
        public RecipientListId $id,
        public string $name,
        public ?string $description = null,
        public ?int $recipientCount = null,
        public ?\DateTimeImmutable $createdAt = null,
        public ?\DateTimeImmutable $updatedAt = null,
        public ?string $unsubscribeBlacklist = null,
        public ?string $default = null,
        public ?string $recipientSubscribeEmail = null,
        public ?string $subscribeFormUrl = null,
        public ?string $subscribeFormFieldKey = null,
        public ?array $links = null
    ) {
    }

    public static function fromArray(array $data): self
    {
        if (! isset($data['id'])) {
            throw new \InvalidArgumentException('RecipientList data must contain ID');
        }

        return new self(
            id: RecipientListId::fromInt($data['id']),
            name: $data['name'] ?? '',
            description: $data['description'] ?? null,
            recipientCount: $data['recipient_count'] ?? null,
            createdAt: isset($data['created'])
                ? new \DateTimeImmutable($data['created'])
                : null,
            updatedAt: isset($data['updated'])
                ? new \DateTimeImmutable($data['updated'])
                : null,
            unsubscribeBlacklist: $data['unsubscribe_blacklist'] ?? null,
            default: $data['default'] ?? null,
            recipientSubscribeEmail: $data['recipient_subscribe_email'] ?? null,
            subscribeFormUrl: $data['subscribe_form_url'] ?? null,
            subscribeFormFieldKey: $data['subscribe_form_field_key'] ?? null,
            links: $data['_links'] ?? null
        );
    }

    public function isDefault(): bool
    {
        return $this->default === 'yes';
    }

    public function hasUnsubscribeBlacklist(): bool
    {
        return $this->unsubscribeBlacklist === 'yes';
    }

    public function hasRecipientSubscribeEmail(): bool
    {
        return $this->recipientSubscribeEmail === 'yes';
    }

    public function isEmpty(): bool
    {
        return $this->recipientCount === 0;
    }
}
