<?php

namespace Modules\Account\Models;

use CodeIgniter\Model;

class PayagentModel extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'payagents';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $insertID         = 0;
    protected $returnType       = 'object';
    protected $useSoftDeletes   = true;
    protected $protectFields    = true;
    protected $allowedFields    = ['id', 'company_id', 'agent_id','amount','total_balance','status','approved_id', 'account_id','payment_mode', 'accepted_date', 'transaction_id', 'merchant_reference_id'];

    // 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   = ['setEgyptTime'];
    protected $afterInsert    = [];
    protected $beforeUpdate   = ['setEgyptTime'];
    protected $afterUpdate    = [];
    protected $beforeFind     = [];
    protected $afterFind      = [];
    protected $beforeDelete   = [];
    protected $afterDelete    = [];

    protected function setEgyptTime(array $data)
    {
        if (isset($data['data'])) {
            $now = get_egypt_time()->format('Y-m-d H:i:s');
            if (!isset($data['data']['id'])) {
                $data['data']['created_at'] = $now;
            }
            $data['data']['updated_at'] = $now;
        }
        return $data;
    }
}
