<?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 $branchId,
        public ?int $sectionId,
        public ?int $teamId,
        public int $positionId,
        public ?int $jobGradeId,
        public ?int $workLocationId,
        public ?int $costCenterId,
        public ?int $managerEmployeeId,
        public string $salary,
        public ?int $salaryStructureId,
        public ?string $paymentMethod,
        public ?string $currency,
        public string $hireDate,
        public ?string $probationEndDate,
        public ?string $confirmationDate,
        public string $gender,
        public ?string $dateOfBirth,
        public ?string $maritalStatus,
        public ?string $bloodType,
        public ?string $religion,
        public ?string $nationality,
        public string $nationalId,
        public ?string $passportNumber,
        public ?string $socialInsuranceNumber,
        public ?string $taxNumber,
        public ?string $bankName,
        public ?string $bankAccountNumber,
        public ?string $iban,
        public ?string $swiftCode,
        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'],
            branchId: ! empty($data['branch_id']) ? (int) $data['branch_id'] : null,
            sectionId: ! empty($data['section_id']) ? (int) $data['section_id'] : null,
            teamId: ! empty($data['team_id']) ? (int) $data['team_id'] : null,
            positionId: (int) $data['position_id'],
            jobGradeId: ! empty($data['job_grade_id']) ? (int) $data['job_grade_id'] : null,
            workLocationId: ! empty($data['work_location_id']) ? (int) $data['work_location_id'] : null,
            costCenterId: ! empty($data['cost_center_id']) ? (int) $data['cost_center_id'] : null,
            managerEmployeeId: isset($data['manager_employee_id']) ? (int) $data['manager_employee_id'] : null,
            salary: (string) ($data['salary'] ?? '0'),
            salaryStructureId: ! empty($data['salary_structure_id']) ? (int) $data['salary_structure_id'] : null,
            paymentMethod: isset($data['payment_method']) ? (string) $data['payment_method'] : null,
            currency: isset($data['currency']) ? strtoupper((string) $data['currency']) : null,
            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,
            bloodType: isset($data['blood_type']) ? (string) $data['blood_type'] : null,
            religion: isset($data['religion']) ? (string) $data['religion'] : 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,
            taxNumber: isset($data['tax_number']) ? (string) $data['tax_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,
            swiftCode: isset($data['swift_code']) ? (string) $data['swift_code'] : 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,
            'branch_id' => $this->branchId,
            'section_id' => $this->sectionId,
            'team_id' => $this->teamId,
            'position_id' => $this->positionId,
            'job_grade_id' => $this->jobGradeId,
            'work_location_id' => $this->workLocationId,
            'cost_center_id' => $this->costCenterId,
            'manager_employee_id' => $this->managerEmployeeId,
            'salary' => number_format((float) $this->salary, 2, '.', ''),
            'salary_structure_id' => $this->salaryStructureId,
            'payment_method' => $this->paymentMethod,
            'currency' => $this->currency,
            '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,
            'blood_type' => $this->bloodType,
            'religion' => $this->religion,
            'nationality' => $this->nationality,
            'national_id' => $this->nationalId,
            'passport_number' => $this->passportNumber,
            'social_insurance_number' => $this->socialInsuranceNumber,
            'tax_number' => $this->taxNumber,
            'bank_name' => $this->bankName,
            'bank_account_number' => $this->bankAccountNumber,
            'iban' => $this->iban,
            'swift_code' => $this->swiftCode,
            'employment_status_id' => $this->employmentStatusId,
            'is_active' => $this->isActive,
        ];
    }
}
