<?php

namespace Modules\Account\Models;

use CodeIgniter\Model;

class AccountModel extends Model
{
	protected $DBGroup              = 'default';
	protected $table                = 'accounts';
	protected $primaryKey           = 'id';
	protected $useAutoIncrement     = true;
	protected $insertID             = 0;
	protected $returnType           = 'object';
	protected $useSoftDeletes       = true;
	protected $protectFields        = true;
	protected $allowedFields        = ['id','booking_id', 'type', 'transaction_type', 'detail','amount','doc_location','system_user_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;
    }
}
