<?php

namespace App\Models;

use CodeIgniter\Model;

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

    protected $allowedFields = [
        'code', 'name', 'type', 'is_sio_applicable', 'is_eosb_applicable',
        'is_system', 'sort_order', 'status',
    ];

    public function getActive(): array
    {
        return $this->where('status', 'active')->orderBy('sort_order', 'ASC')->findAll();
    }

    public function getByCode(string $code): ?array
    {
        return $this->where('code', strtoupper($code))->first() ?: null;
    }

    public function getCodeMap(): array
    {
        $map = [];
        foreach ($this->getActive() as $row) {
            $map[$row['code']] = $row;
        }

        return $map;
    }
}
