<?php

namespace Modules\Payroll\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Modules\Employee\Models\Traits\HasEmployee;

// use Modules\Payroll\Database\Factories\CpfAccountsFactory;

class CpfAccounts extends Model
{
    use HasFactory, HasEmployee;

    /**
     * The attributes that are mass assignable.
     */
    protected $fillable = [
        'employee_id',
        'account_no',
        'employee_percentage',
        'employer_percentage',
        'balance',
        'opening_date',
        'closing_date',
        'status',
    ];

    public function scopeSearch($query, ?string $search)
    {
        if (blank($search)) {
            return $query;
        }

        $search = '%' . trim($search) . '%';

        return $query->when($search, function ($q, $search) {

            $q->where(function ($q) use ($search) {
                $q->where('account_no', 'LIKE', $search)
                    ->orWhereHas('employee', function ($q) use ($search) {
                        $q->where('name', 'LIKE', $search)
                            ->orWhere('emp_number', 'LIKE', $search);
                    });
            });
        });
    }

    public function scopeFilterByStatus($query, ?string $status)
    {
        return $query->when($status, fn($q, $status) => $q->where('status', $status));
    }

    // protected static function newFactory(): CpfAccountsFactory
    // {
    //     // return CpfAccountsFactory::new();
    // }
}
