<?php

namespace App\Models;

use CodeIgniter\Model;

class PayrollSettingsModel extends Model
{
    protected $table         = 'payroll_settings';
    protected $primaryKey    = 'id';
    protected $returnType    = 'array';
    protected $useTimestamps = true;
    protected $dateFormat    = 'datetime';

    protected $allowedFields = [
        'sio_cap_amount', 'sio_bahraini_employer_rate', 'sio_bahraini_employee_rate',
        'sio_expat_employer_rate', 'sio_expat_employee_rate',
        'eosb_rate_first_3_years', 'eosb_rate_after_3_years', 'eosb_effective_date',
        'working_days_per_month', 'daily_work_hours',
        'overtime_day_multiplier', 'overtime_night_multiplier',
        'payment_day', 'wps_employer_id',
    ];

    public function getSettings(): array
    {
        $row = $this->first();
        if ($row) {
            return $row;
        }

        $now = date('Y-m-d H:i:s');
        $this->insert([
            'sio_cap_amount'             => 4000.000,
            'sio_bahraini_employer_rate' => 18.00,
            'sio_bahraini_employee_rate' => 8.00,
            'sio_expat_employer_rate'    => 3.00,
            'sio_expat_employee_rate'    => 1.00,
            'eosb_rate_first_3_years'    => 4.20,
            'eosb_rate_after_3_years'    => 8.40,
            'eosb_effective_date'        => '2024-03-01',
            'working_days_per_month'     => 26,
            'daily_work_hours'           => 8.00,
            'overtime_day_multiplier'    => 1.25,
            'overtime_night_multiplier'  => 1.50,
            'payment_day'                => 25,
            'created_at'                 => $now,
            'updated_at'                 => $now,
        ]);

        return $this->first();
    }
}
