<?php

namespace App\Models;

use Config\Lookups;

/**
 * One row per pay period — see CreatePayrollRunsTable's migration
 * comment for the draft -> finalized -> paid workflow.
 */
class PayrollRunModel extends TenantModel
{
    protected $table            = 'payroll_runs';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'object';
    protected $useTimestamps    = true;
    protected $createdField     = 'created_at';
    protected $updatedField     = 'updated_at';

    protected $allowedFields = [
        'period_month',
        'status',
        'created_by',
        'finalized_at',
        'paid_at',
        'payment_reference',
    ];

    public function __construct()
    {
        parent::__construct();

        $statuses = array_keys((new Lookups())->payrollRunStatuses);

        $this->validationRules = [
            'period_month' => 'required|valid_date',
            'status'       => 'required|in_list[' . implode(',', $statuses) . ']',
        ];
    }

    public function latest(): array
    {
        return $this->orderBy('period_month', 'desc')->findAll();
    }
}
