<?php

namespace App\Models;

use CodeIgniter\Model;

class WalletModel extends Model
{
    protected $table         = 'wallets';
    protected $allowedFields = ['agency_id', 'balance', 'currency'];

    public function forAgency(int $agencyId): ?array
    {
        return $this->where('agency_id', $agencyId)->first();
    }

    public function ensureExists(int $agencyId, string $currency = 'PKR'): array
    {
        $existing = $this->forAgency($agencyId);
        if ($existing) {
            return $existing;
        }
        $id = $this->insert(['agency_id' => $agencyId, 'balance' => 0, 'currency' => $currency], true);
        return $this->find($id);
    }
}
