<?php

namespace App\Modules\HR\Employees\DTOs;

/**
 * Validated payload for creating/updating an employee.
 */
final readonly class EmployeeWriteData
{
    public function __construct(
        public string $fullName,
        public ?string $arabicName,
        public ?string $fingerprintId,
        public string $phone1,
        public ?string $phone2,
        public ?string $email,
        public ?string $companyEmail,
        public int $departmentId,
        public int $positionId,
        public ?int $managerEmployeeId,
        public string $salary,
        public string $hireDate,
        public ?string $probationEndDate,
        public ?string $confirmationDate,
        public string $gender,
        public ?string $dateOfBirth,
        public ?string $maritalStatus,
        public ?string $nationality,
        public string $nationalId,
        public ?string $passportNumber,
        public ?string $socialInsuranceNumber,
        public ?string $bankName,
        public ?string $bankAccountNumber,
        public ?string $iban,
        public int $employmentStatusId,
        public bool $isActive,
        public EmployeeAddressWriteData $address,
    ) {}

    /**
     * @param  array<string, mixed>  $data
     */
    public static function fromValidated(array $data): self
    {
        return new self(
            fullName: (string) $data['full_name'],
            arabicName: isset($data['arabic_name']) ? (string) $data['arabic_name'] : null,
            fingerprintId: isset($data['fingerprint_id']) ? (string) $data['fingerprint_id'] : null,
            phone1: (string) $data['phone_1'],
            phone2: isset($data['phone_2']) ? (string) $data['phone_2'] : null,
            email: isset($data['email']) ? (string) $data['email'] : null,
            companyEmail: isset($data['company_email']) ? (string) $data['company_email'] : null,
            departmentId: (int) $data['department_id'],
            positionId: (int) $data['position_id'],
            managerEmployeeId: isset($data['manager_employee_id']) ? (int) $data['manager_employee_id'] : null,
            salary: (string) ($data['salary'] ?? '0'),
            hireDate: (string) $data['hire_date'],
            probationEndDate: isset($data['probation_end_date']) ? (string) $data['probation_end_date'] : null,
            confirmationDate: isset($data['confirmation_date']) ? (string) $data['confirmation_date'] : null,
            gender: (string) $data['gender'],
            dateOfBirth: isset($data['date_of_birth']) ? (string) $data['date_of_birth'] : null,
            maritalStatus: isset($data['marital_status']) ? (string) $data['marital_status'] : null,
            nationality: isset($data['nationality']) ? (string) $data['nationality'] : null,
            nationalId: (string) $data['national_id'],
            passportNumber: isset($data['passport_number']) ? (string) $data['passport_number'] : null,
            socialInsuranceNumber: isset($data['social_insurance_number']) ? (string) $data['social_insurance_number'] : null,
            bankName: isset($data['bank_name']) ? (string) $data['bank_name'] : null,
            bankAccountNumber: isset($data['bank_account_number']) ? (string) $data['bank_account_number'] : null,
            iban: isset($data['iban']) ? (string) $data['iban'] : null,
            employmentStatusId: (int) $data['employment_status_id'],
            isActive: (bool) ($data['is_active'] ?? true),
            address: EmployeeAddressWriteData::fromValidated($data),
        );
    }

    /**
     * @return array<string, mixed>
     */
    public function toEmployeeArray(): array
    {
        return [
            'full_name' => $this->fullName,
            'arabic_name' => $this->arabicName,
            'fingerprint_id' => $this->fingerprintId,
            'phone_1' => $this->phone1,
            'phone_2' => $this->phone2,
            'email' => $this->email,
            'company_email' => $this->companyEmail,
            'department_id' => $this->departmentId,
            'position_id' => $this->positionId,
            'manager_employee_id' => $this->managerEmployeeId,
            'salary' => number_format((float) $this->salary, 2, '.', ''),
            'hire_date' => $this->hireDate,
            'probation_end_date' => $this->probationEndDate,
            'confirmation_date' => $this->confirmationDate,
            'gender' => $this->gender,
            'date_of_birth' => $this->dateOfBirth,
            'marital_status' => $this->maritalStatus,
            'nationality' => $this->nationality,
            'national_id' => $this->nationalId,
            'passport_number' => $this->passportNumber,
            'social_insurance_number' => $this->socialInsuranceNumber,
            'bank_name' => $this->bankName,
            'bank_account_number' => $this->bankAccountNumber,
            'iban' => $this->iban,
            'employment_status_id' => $this->employmentStatusId,
            'is_active' => $this->isActive,
        ];
    }
}
