<?php
declare(strict_types=1);

namespace SeoScope\Licensing;

final class LicenseState
{
    public function __construct(
        public string $status, // unlicensed|active|in_use|invalid|grace|expired
        public ?string $token,
        public ?int $lastOkAt,
        public ?int $nextCheckAt,
        public ?string $message
    ) {
    }

    /**
     * @param array<string,mixed> $data
     */
    public static function fromArray(array $data): self
    {
        return new self(
            is_string($data['status'] ?? null) ? (string) $data['status'] : 'unlicensed',
            is_string($data['token'] ?? null) ? (string) $data['token'] : null,
            is_numeric($data['last_ok_at'] ?? null) ? (int) $data['last_ok_at'] : null,
            is_numeric($data['next_check_at'] ?? null) ? (int) $data['next_check_at'] : null,
            is_string($data['message'] ?? null) ? (string) $data['message'] : null
        );
    }

    /**
     * @return array<string,mixed>
     */
    public function toArray(): array
    {
        return [
            'status' => $this->status,
            'token' => $this->token,
            'last_ok_at' => $this->lastOkAt,
            'next_check_at' => $this->nextCheckAt,
            'message' => $this->message,
        ];
    }
}

