<?php

namespace Modules\Employee\Repositories;

use Modules\Employee\DTO\EmployeeSettingFilter;
use Modules\Employee\Models\Country;
use Modules\Employee\Models\Gender;
use Modules\Employee\Models\MaritalStatus;
use Modules\Employee\Models\Religion;

class EmployeeLookupRepository
{
    public function genderList(EmployeeSettingFilter $filter, array $columns = ['*'])
    {
        return Gender::select($columns)
            ->when($filter->search, function ($query, $search) {
                $search = '%' . trim($search) . '%';

                $query->where(function ($q) use ($search) {
                    $q->where('name', 'LIKE', $search);
                });
            })
            ->orderBy('name')
            ->paginate($filter->perPage ?? 15);
    }

    public function countryList(EmployeeSettingFilter $filter, array $columns = ['*'])
    {
        return Country::select($columns)
            ->when($filter->search, function ($query, $search) {
                $search = '%' . trim($search) . '%';

                $query->where(function ($q) use ($search) {
                    $q->where('name', 'LIKE', $search);
                })->orWhere('code', 'LIKE', $search);
            })
            ->orderBy('name')
            ->paginate($filter->perPage ?? 15);
    }

    public function religionList(EmployeeSettingFilter $filter)
    {
        return Religion::when($filter->search, function ($query, $search) {
            $search = '%' . trim($search) . '%';

            $query->where(function ($q) use ($search) {
                $q->where('name', 'LIKE', $search);
            });
        })
            ->when(
                $filter->status,
                fn($q, $status) =>
                $q->where('status', $status)
            )
            ->orderBy('name')
            ->paginate($filter->perPage ?? 15);
    }
    public function maritalStatusList(EmployeeSettingFilter $filter)
    {
        return MaritalStatus::when($filter->search, function ($query, $search) {
            $search = '%' . trim($search) . '%';

            $query->where(function ($q) use ($search) {
                $q->where('name', 'LIKE', $search);
            });
        })
            ->when(
                $filter->status,
                fn($q, $status) =>
                $q->where('status', $status)
            )
            ->orderBy('name')
            ->paginate($filter->perPage ?? 15);
    }
}
