<?php

namespace Modules\Payroll\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
// use Modules\Payroll\Database\Factories\PayrollCycleFactory;

class PayrollCycle extends Model
{

    use HasFactory;

    /**
     * The attributes that are mass assignable.
     */
    protected $fillable = [
        'name',
        'month',
        'year',
        'from_date',
        'to_date',
        'salary_date',
        'status',
        'total_employees',
        'total_net_salary',
        'remarks',
        'approved_by',
        'approved_at',
        'paid_by',
        'paid_at',
        'is_locked',
        'locked_by',
        'locked_at',
    ];

    protected $casts = [
        'is_locked' => 'boolean',
    ];

    public function scopeSearch($query, $term)
    {
        if (blank($term)) {
            return $query;
        }

        return $query->where('name', 'LIKE', "%{$term}%");
    }

    public function scopeDateRange($query, $from = null, $to = null)
    {
        if ($from && $to) {
            return $query->whereBetween('from_date', [$from, $to])
                ->orWhereBetween('to_date', [$from, $to])
                ->orWhere(function ($q) use ($from, $to) {
                    $q->where('from_date', '<=', $from)
                        ->where('to_date', '>=', $to);
                });
        }

        return $query;
    }
    
    public function scopeFilterByStatus($query, $status)
    {
        if (blank($status)) {
            return $query;
        }

        return $query->where('status', $status);
    }



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