<?php
/**
 * License Model
 *
 * @package Jeremias\WcLicensedProduct\License
 */

declare(strict_types=1);

namespace Jeremias\WcLicensedProduct\License;

/**
 * License entity model
 */
class License
{
    public const STATUS_ACTIVE = 'active';
    public const STATUS_INACTIVE = 'inactive';
    public const STATUS_EXPIRED = 'expired';
    public const STATUS_REVOKED = 'revoked';

    private int $id;
    private string $licenseKey;
    private int $orderId;
    private int $productId;
    private int $customerId;
    private string $domain;
    private ?int $versionId;
    private string $status;
    private int $activationsCount;
    private int $maxActivations;
    private ?\DateTimeInterface $expiresAt;
    private \DateTimeInterface $createdAt;
    private \DateTimeInterface $updatedAt;

    /**
     * Create license from database row
     */
    public static function fromArray(array $data): self
    {
        $license = new self();
        $license->id = (int) $data['id'];
        $license->licenseKey = $data['license_key'];
        $license->orderId = (int) $data['order_id'];
        $license->productId = (int) $data['product_id'];
        $license->customerId = (int) $data['customer_id'];
        $license->domain = $data['domain'];
        $license->versionId = $data['version_id'] ? (int) $data['version_id'] : null;
        $license->status = $data['status'];
        $license->activationsCount = (int) $data['activations_count'];
        $license->maxActivations = (int) $data['max_activations'];
        $license->expiresAt = $data['expires_at'] ? new \DateTimeImmutable($data['expires_at']) : null;
        $license->createdAt = new \DateTimeImmutable($data['created_at']);
        $license->updatedAt = new \DateTimeImmutable($data['updated_at']);

        return $license;
    }

    public function getId(): int
    {
        return $this->id;
    }

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

    public function getOrderId(): int
    {
        return $this->orderId;
    }

    public function getProductId(): int
    {
        return $this->productId;
    }

    public function getCustomerId(): int
    {
        return $this->customerId;
    }

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

    public function getVersionId(): ?int
    {
        return $this->versionId;
    }

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

    public function getActivationsCount(): int
    {
        return $this->activationsCount;
    }

    public function getMaxActivations(): int
    {
        return $this->maxActivations;
    }

    public function getExpiresAt(): ?\DateTimeInterface
    {
        return $this->expiresAt;
    }

    public function getCreatedAt(): \DateTimeInterface
    {
        return $this->createdAt;
    }

    public function getUpdatedAt(): \DateTimeInterface
    {
        return $this->updatedAt;
    }

    /**
     * Check if license is currently valid
     */
    public function isValid(): bool
    {
        if ($this->status !== self::STATUS_ACTIVE) {
            return false;
        }

        if ($this->expiresAt !== null && $this->expiresAt < new \DateTimeImmutable()) {
            return false;
        }

        return true;
    }

    /**
     * Check if license has expired
     */
    public function isExpired(): bool
    {
        return $this->expiresAt !== null && $this->expiresAt < new \DateTimeImmutable();
    }

    /**
     * Check if license can be activated on another domain
     */
    public function canActivate(): bool
    {
        return $this->isValid() && $this->activationsCount < $this->maxActivations;
    }

    /**
     * Convert to array for JSON/API responses
     */
    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'license_key' => $this->licenseKey,
            'order_id' => $this->orderId,
            'product_id' => $this->productId,
            'customer_id' => $this->customerId,
            'domain' => $this->domain,
            'version_id' => $this->versionId,
            'status' => $this->status,
            'activations_count' => $this->activationsCount,
            'max_activations' => $this->maxActivations,
            'expires_at' => $this->expiresAt?->format('Y-m-d H:i:s'),
            'created_at' => $this->createdAt->format('Y-m-d H:i:s'),
            'updated_at' => $this->updatedAt->format('Y-m-d H:i:s'),
            'is_valid' => $this->isValid(),
        ];
    }
}
