<?php

namespace App\Models;

use CodeIgniter\Model;

class PartnerInsurancePayoutGridModel extends Model
{
    protected $table      = 'partner_insurance_payout_grid';
    protected $primaryKey = 'id';

    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;

    protected $allowedFields = [
        'vehicle_type',
        'fuel',
        'insurer',
        'rto',
        'broker_name',
        'segment',
        'comp',
        'tp',
        'od',
        'remarks',
        'broker_comp',
        'broker_tp',
        'broker_od',
        'partner_agent_incentive_file_id',
        'created_by',
        'updated_by',
    ];

    // Timestamps — map to the column names in your DDL
    protected $useTimestamps = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';

    // Validation
    protected $validationRules = [
        'vehicle_type' => 'required|max_length[100]',
        'insurer'      => 'required|max_length[45]',
        'segment'      => 'required|max_length[100]',
    ];

    protected $validationMessages = [
        'vehicle_type' => ['required' => 'Vehicle type is required.'],
        'insurer'      => ['required' => 'Insurer is required.'],
        'segment'      => ['required' => 'Segment is required.'],
    ];

    protected $skipValidation = false;

    // -------------------------------------------------------------------------
    // Fetch grid filtered by vehicle_type
    // -------------------------------------------------------------------------
    public function getByVehicleType(string $vehicleType): array
    {
        return $this
            ->where('vehicle_type', $vehicleType)
            ->orderBy('insurer', 'ASC')
            ->findAll();
    }

    // -------------------------------------------------------------------------
    // Fetch grid filtered by insurer + rto
    // -------------------------------------------------------------------------
    public function getByInsurerAndRto(string $insurer, string $rto): array
    {
        return $this
            ->where('insurer', $insurer)
            ->where('rto',     $rto)
            ->orderBy('vehicle_type', 'ASC')
            ->findAll();
    }

}
