<?php

namespace Formatrica\License;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Immutable license state value object.
 *
 * Never carries the raw license key or auth token — only redacted status
 * suitable for the builder/admin UI and Site Health.
 */
final class License_State
{
    public function __construct(
        private readonly bool $installed = false,
        private readonly bool $has_key = false,
        private readonly string $status = 'missing',
        private readonly bool $is_usable = false,
        private readonly ?string $plan_code = null,
        private readonly ?string $expires_at = null,
        private readonly ?string $grace_until = null,
        private readonly ?string $last_checked_at = null,
        private readonly ?string $message = null,
    ) {
    }

    /**
     * The default state for Free / no Pro package installed.
     */
    public static function missing(): self
    {
        return new self();
    }

    public function installed(): bool
    {
        return $this->installed;
    }

    public function has_key(): bool
    {
        return $this->has_key;
    }

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

    public function is_usable(): bool
    {
        return $this->is_usable;
    }

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

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

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

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

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

    /**
     * Redacted summary safe to expose to the builder payload and REST.
     *
     * @return array<string, mixed>
     */
    public function to_summary(): array
    {
        return [
            'installed'       => $this->installed,
            'has_key'         => $this->has_key,
            'status'          => $this->status,
            'is_usable'       => $this->is_usable,
            'plan_code'       => $this->plan_code,
            'expires_at'      => $this->expires_at,
            'grace_until'     => $this->grace_until,
            'last_checked_at' => $this->last_checked_at,
            'message'         => $this->message,
        ];
    }
}
