<?php

namespace App\Models\invoice;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\invoice\ArPayment;
use App\Models\invoice\Fishtank;
use App\Models\invoice\Deposit;
use Laravel\Scout\Searchable;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
class Invoice extends Model
{
    use HasFactory;
    use Searchable;

    public $table = 'invoice';

    protected $fillable = [
        'debtorCode',
        'debtorName',
        'vechicleNo',
        'invoiceNo',
        'date',
        'totalQuantity',
        'totalAmount',
        'statusID'
    ];

    //accessor for invoiceNo with 'KPBIV-'
    public function getInvoiceNameAttribute()
    {
        return 'KPBIV-' . $this->attributes['invoiceNo'];
    }

    //define relationship with Status Model
    public function status()
    {
        return $this->belongsTo(Status::class, 'statusID');
    }

    //define relationship with arPayments Model
    public function arPayment()
    {
        return $this->hasMany(ArPayment::class, 'InvoiceID');
    }

    //define relationship with fishtank Model
    public function fishtank()
    {
        return $this->hasMany(Fishtank::class, 'InvoiceID');
    }

    //define relationship with deposit Model
    public function deposit()
    {
        return $this->hasMany(Deposit::class, 'InvoiceID');
    }

    public function toSearchableArray()
    {
        return [
            'invoiceNo' => $this->invoiceNo,
            'debtorCode' => $this->debtorCode,
            'debtorName' => $this->debtorName,
            'vechicleNo' => $this->vechicleNo,
        ];
    }
}
