<?php

namespace Modules\Employee\Transformers\Employee;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Modules\Employee\Transformers\BloodResource;
use Modules\Employee\Transformers\BranchResource;
use Modules\Employee\Transformers\DepartmentResource;
use Modules\Employee\Transformers\DesignationResource;
use Modules\Employee\Transformers\EmploymentStatusResource;
use Modules\Employee\Transformers\GenderResource;
use Modules\Employee\Transformers\MaritalStatusResource;
use Modules\Employee\Transformers\NationalityResource;
use Modules\Employee\Transformers\ProjectResource;
use Modules\Employee\Transformers\ReligionResource;
use Modules\Employee\Transformers\ShiftLookUpResource;
use Modules\HR\Transformers\AttendancePolicyLookupResource;
use Modules\HR\Transformers\SalaryStructureResource;
use Modules\Settings\Transformers\AddressableResource;
use Modules\Settings\Transformers\AttachableResource;
use Modules\Settings\Transformers\BankInfoResource;
use Modules\HR\Transformers\EmployeeTaxProfileResource;
use Modules\HR\Transformers\HolidayAllowancePolicyLookupResource;
use Modules\HR\Transformers\WeekendPolicyResource;

class EmployeeInfo extends JsonResource
{
    /**
     * Transform the resource into an array.
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'emp_number' => $this->emp_number,
            'name' => $this->name,
            'phone' => $this->phone,
            'work_phone' => $this->work_phone,
            'email' => $this->email,
            'dob' => $this->dob,
            'nid' => $this->nid,
            'father_name' => $this->father_name,
            'mother_name' => $this->mother_name,
            'joined_date' => $this->joined_date,
            'current_basic_salary' => $this->current_basic_salary,
            'image' => $this->image ? asset($this->image) : null,

            'payrollSettings' => [
                'cpf_active' => (bool) $this->cpf_active,
                'attendance_required' => (bool) $this->attendance_required,
                'overtime_allowed' => (bool) $this->overtime_allowed,
                'is_payroll' => (bool) $this->is_payroll,
                'tax_deduction' => (bool) $this->tax_deduction,
            ],

            'hasUserAccount' => $this->hasUserAccount(),
            'user' => $this->whenLoaded('user', function () {
                return new EmployeeUser($this->user);
            }),
            'department' => $this->whenLoaded('department', function () {
                return new DepartmentResource($this->department);
            }),
            'designation' => $this->whenLoaded('designation', function () {
                return new DesignationResource($this->designation);
            }),
            'project' => $this->whenLoaded('project', function () {
                return new ProjectResource($this->project);
            }),
            'branch' => $this->whenLoaded('branch', function () {
                return new BranchResource($this->branch);
            }),
            'nationality' => $this->whenLoaded('nationality', function () {
                return new NationalityResource($this->nationality);
            }),
            'gender' => $this->whenLoaded('gender', function () {
                return new GenderResource($this->gender);
            }),
            'religion' => $this->whenLoaded('religion', function () {
                return new ReligionResource($this->religion);
            }),
            'blood' => $this->whenLoaded('blood', function () {
                return new BloodResource($this->blood);
            }),
            'maritalStatus' => $this->whenLoaded('maritalStatus', function () {
                return new MaritalStatusResource($this->maritalStatus);
            }),
            'employmentStatus' => $this->whenLoaded('employmentStatus', function () {
                return new EmploymentStatusResource($this->employmentStatus);
            }),
            'report_to' => $this->whenLoaded('reportTo', function () {
                return [
                    'id' => $this->reportTo->id,
                    'name' => $this->reportTo->name,
                ];
            }),
            'address' => $this->whenLoaded('presentAddress', function () {
                return new AddressableResource($this->presentAddress);
            }),
            'bankInfo' => $this->whenLoaded('bankInfo', function () {
                return new BankInfoResource($this->bankInfo);
            }),
            'emergencies' => EmpEmergencyResource::collection(
                $this->whenLoaded('emergencies')
            ),
            'educations' => EmpEducationResource::collection(
                $this->whenLoaded('educations')
            ),
            'experiences' => EmpExperienceResource::collection(
                $this->whenLoaded('experiences')
            ),
            'attachments' => AttachableResource::collection(
                $this->whenLoaded('attachments')
            ),

            'attendancePolicy' => $this->whenLoaded('attendancePolicy', function () {
                return new AttendancePolicyLookupResource($this->attendancePolicy);
            }),
            'weekendPolicy' => $this->whenLoaded('weekendPolicy', function () {
                return new WeekendPolicyResource($this->weekendPolicy);
            }),
            'holidayAllowancePolicy' => $this->whenLoaded('holidayAllowancePolicy', function () {
                return new HolidayAllowancePolicyLookupResource($this->holidayAllowancePolicy);
            }),
            'shift' => $this->whenLoaded('shift', function () {
                return new ShiftLookUpResource($this->shift);
            }),

            'status' => $this->status,
            'terminationRecord' => $this->when(
                $this->isTerminated(),
                new EmpTerminationResource($this->whenLoaded('terminationRecord'))
            ),
            'created_by' => $this->whenLoaded('createdBy', function () {
                return new EmployeeUser($this->createdBy);
            }),
            'updated_by' => $this->whenLoaded('updatedBy', function () {
                return new EmployeeUser($this->updatedBy);
            }),
            'taxProfile' => $this->whenLoaded('taxProfile', function () {
                return new EmployeeTaxProfileResource($this->taxProfile);
            }),
            'currentSalaryStructure' => $this->whenLoaded('currentSalaryStructure', function () {
                return new  SalaryStructureResource($this->currentSalaryStructure);
            }),
        ];
    }
}
