<?php

namespace App\Models;

use CodeIgniter\Model;

class CompanyBranchModel extends Model
{
    protected $table            = 'company_branch';
    protected $primaryKey       = 'branch_code';
    protected $useAutoIncrement = false;
    protected $returnType       = \App\Entities\CompanyBranch::class;
    protected $useSoftDeletes   = true;
    protected $protectFields    = true;
    protected $allowedFields    = ['branch_code', 
                                   'company_id', 
                                   'branch_name', 
                                   'address', 
                                   'contact_number', 
                                   'email_address'];

    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;
    }
}
