<?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 $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),
            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,
            'is_active' => $this->isActive,
        ];
    }
}
