<?php
namespace App\Modules\HR\Reports\Data;

use App\Modules\HR\Core\Models\Employee;

class EmployeeDetailsReport
{
    public function getData($filters)
    {
        $query = Employee::with([
            'branch', 'department', 'designation', 'gender',
            'maritalStatus', 'bloodGroup', 'grade', 'employmentType',
            'payrollGroup', 'identificationType', 'religion', 'team',
        ]);

        // Branch filter
        if (!empty($filters['branch_id'])) {
            $query->where('branch_id', $filters['branch_id']);
        }

        // Department filter
        if (!empty($filters['department_id'])) {
            $query->where('department_id', $filters['department_id']);
        }

        // Payroll group filter
        if (!empty($filters['payroll_group'])) {
            $query->where('payroll_group_id', $filters['payroll_group']);
        }

        // Currently working filter (checkbox sends "1" when checked, "0" from hidden)
        if (isset($filters['currently_working']) && $filters['currently_working'] !== '') {
            $query->where('currently_working', (bool) $filters['currently_working']);
        }

        // Attendance filter
        if (isset($filters['attendance']) && $filters['attendance'] !== '') {
            $query->where('attendance', (bool) $filters['attendance']);
        }

        // Payroll filter
        if (isset($filters['payroll']) && $filters['payroll'] !== '') {
            $query->where('payroll', (bool) $filters['payroll']);
        }

        $employees = $query->orderBy('employee_code')->get();

        return [
            'employees' => $employees,
            'filters'   => $filters,
        ];
    }
}
