<?php

namespace App\Models;

use CodeIgniter\Model;

class InvoiceModel extends Model
{
    protected $table      = 'invoices';
    protected $primaryKey = 'id_invoice';

    protected $returnType = 'array';

    protected $useAutoIncrement = true;

    protected $allowedFields = [
        'id_order',
        'nomor_invoice',
        'access_token',
        'tanggal_jatuh_tempo',
        'total_tagihan',
        'sisa_tagihan',
        'status',
    ];

    protected $useTimestamps = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = '';

    protected $validationRules = [
        'id_order'      => 'required|integer',
        'nomor_invoice' => 'required',
    ];

    protected $validationMessages = [];

    protected $skipValidation = false;

    /**
     * Cari invoice berdasarkan access token
     */
    public function findByToken(string $token): ?array
    {
        return $this->where('access_token', $token)->first();
    }
}
