<?php

namespace App\Modules\Admin\Tenancy\DTOs;

final readonly class PlanData
{
    /**
     * @param  array<int, int>  $moduleIds
     */
    public function __construct(
        public string $slug,
        public string $name,
        public ?string $description,
        public float $price,
        public string $currency,
        public string $billingCycle,
        public int $trialDays,
        public bool $isTrial,
        public bool $isDefault,
        public bool $isActive,
        public array $moduleIds,
    ) {}

    public static function fromValidated(array $data): self
    {
        return new self(
            slug: (string) $data['slug'],
            name: (string) $data['name'],
            description: $data['description'] ?? null,
            price: (float) ($data['price'] ?? 0),
            currency: (string) ($data['currency'] ?? 'USD'),
            billingCycle: (string) ($data['billing_cycle'] ?? 'monthly'),
            trialDays: (int) ($data['trial_days'] ?? 0),
            isTrial: (bool) ($data['is_trial'] ?? false),
            isDefault: (bool) ($data['is_default'] ?? false),
            isActive: (bool) ($data['is_active'] ?? true),
            moduleIds: array_map('intval', (array) ($data['modules'] ?? [])),
        );
    }

    public function toAttributes(): array
    {
        return [
            'slug' => $this->slug,
            'name' => $this->name,
            'description' => $this->description,
            'price' => $this->price,
            'currency' => $this->currency,
            'billing_cycle' => $this->billingCycle,
            'trial_days' => $this->trialDays,
            'is_trial' => $this->isTrial,
            'is_default' => $this->isDefault,
            'is_active' => $this->isActive,
        ];
    }
}
