<?php

namespace App\Models;

use CodeIgniter\Model;

class AttendanceSummaryModel extends Model
{
    protected $table            = 'att_log_summary';
    protected $primaryKey       = 'attlogsum_id';
    protected $useAutoIncrement = true;
    protected $returnType       = \App\Entities\AttendanceSummary::class;
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = ['paytrans_id', 
                                   'pay_group_id', 
                                   'employee_id', 
                                   'dtr_emp_id',
                                   'employee_name',
                                   'att_work_days'];

    protected bool $allowEmptyInserts = false;

    // Dates
    protected $useTimestamps = true;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    // Validation
    protected $validationRules      = [];
    protected $validationMessages   = [];
    protected $skipValidation       = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks = true;
    protected $beforeInsert   = ['assignCreatedBy'];
    protected $afterInsert    = [];
    protected $beforeUpdate   = ['assignUpdatedBy'];
    protected $afterUpdate    = [];
    protected $beforeFind     = [];
    protected $afterFind      = [];
    protected $beforeDelete   = [];
    protected $afterDelete    = [];

    public function assignCreatedBy(array $data)
    {
        $data['data']['created_by'] = auth()->user()->employee_id;
        return $data;
    }

    public function assignUpdatedBy(array $data)
    {
        $data['data']['updated_by'] = auth()->user()->employee_id;
        return $data;
    }

    public function addBatchData($data, $ignoreDuplicate = false)
    {
        $builder = $this->db->table('att_log_summary');
        // ignore($ignoreDuplicate) will ignore duplicate data
        return $builder->ignore($ignoreDuplicate)->insertBatch($data);
    }
}
