<?php

namespace App\Modules\HR\Leave\DTOs;

final readonly class LeaveTypeWriteData
{
    public function __construct(
        public string $name,
        public bool $isPaid,
        public ?int $annualLimit,
        public bool $requiresApproval,
        public bool $allowHalfDay,
        public bool $allowNegativeBalance,
        public bool $requiresAttachment,
        public bool $allowCarryForward,
        public ?float $carryForwardMaxDays,
        public bool $allowHourly,
        public ?float $maxHourlyHours,
        public bool $isActive,
    ) {}

    /**
     * @param  array<string, mixed>  $data
     */
    public static function fromValidated(array $data): self
    {
        return new self(
            name: (string) $data['name'],
            isPaid: (bool) ($data['is_paid'] ?? true),
            annualLimit: isset($data['annual_limit']) && $data['annual_limit'] !== ''
                ? (int) $data['annual_limit']
                : null,
            requiresApproval: (bool) ($data['requires_approval'] ?? true),
            allowHalfDay: (bool) ($data['allow_half_day'] ?? false),
            allowNegativeBalance: (bool) ($data['allow_negative_balance'] ?? false),
            requiresAttachment: (bool) ($data['requires_attachment'] ?? false),
            allowCarryForward: (bool) ($data['allow_carry_forward'] ?? false),
            carryForwardMaxDays: isset($data['carry_forward_max_days']) ? (float) $data['carry_forward_max_days'] : null,
            allowHourly: (bool) ($data['allow_hourly'] ?? false),
            maxHourlyHours: isset($data['max_hourly_hours']) ? (float) $data['max_hourly_hours'] : null,
            isActive: (bool) ($data['is_active'] ?? true),
        );
    }

    /**
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'name' => $this->name,
            'is_paid' => $this->isPaid,
            'annual_limit' => $this->annualLimit,
            'requires_approval' => $this->requiresApproval,
            'allow_half_day' => $this->allowHalfDay,
            'allow_negative_balance' => $this->allowNegativeBalance,
            'requires_attachment' => $this->requiresAttachment,
            'allow_carry_forward' => $this->allowCarryForward,
            'carry_forward_max_days' => $this->carryForwardMaxDays,
            'allow_hourly' => $this->allowHourly,
            'max_hourly_hours' => $this->maxHourlyHours,
            'is_active' => $this->isActive,
        ];
    }
}
