<?php

namespace App\Models\Invoicing;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

use App\Enums\Invoicing\InvoiceStatus;
use App\Enums\Invoicing\InvoiceCancellationStatus;

class TaxPayment extends Model
{
  //* Associated Table & Primary Key
    protected $table = 'tax_payments';
    protected $primaryKey = 'id';

  //* Fillable Fields
    protected $fillable = [
      // IDENTIDAD Y RELACIONES
        "substitution_invoice_id",
        "client_id",
        "tax_format_id",
        "tax_branch_id",

      // ENCABEZADO CFDI
        "cfdi_use",
        "payment_form",
        "currency",
        "payment_date",
        "certification_date",
        'third_party_account',

      // IDENTIFICADORES DE TIMBRADO
        "serial_consecutive",
        "facturapi_payment_id",
        "sat_uuid",
        "cancel_motive",

        "cfdi_seal",
        "sat_seal",
        "sat_cert_number",
        "tfd_original_string",
        "verification_url",

      // IMPORTES, IMPUESTOS Y TOTALES
        "exchange_rate",
        "amount_total",

      // ESTATUS Y CICLO DE VIDA (LOCALES)
        "status",
        "cancellation_status",

      // ARTEFACTOS / RECURSOS (DESCARGAS Y RUTAS)
        "xml_path",

      // CONTROL DE RESPUESTAS
        "request_payload",
        "response_payload",
        "last_error_returned",
    ];

  //* Cast Columns
    protected $casts = [
        'third_party_account' => 'array', // Example of structure/cases on resources\json\modules\invoices\third_party_account_structure.json

        'request_payload' => 'array', // Dynamic Structure (Depends of Facturapi)
        'response_payload' => 'array', // Dynamic Structure (Depends of Facturapi)
        'last_error_returned' => 'array', // Dynamic Structure (Depends of Facturapi)

        'payment_date' => 'datetime',
        'certification_date' => 'datetime',
        'status' => InvoiceStatus::class,
        'cancellation_status' => InvoiceCancellationStatus::class,
    ];

  //* Relationships
    //- Self Relation
        public function previousInvoice()
        {
            return $this->belongsTo(TaxInvoice::class, 'substitution_invoice_id');
        }

        public function replacedBy()
        {
            return $this->hasMany(TaxInvoice::class, 'substitution_invoice_id');
        }


    //- Internal Relations
        public function taxPaymentDoc()
        {
            return $this->hasOne(TaxPaymentDoc::class, 'tax_payment_id');
        }

        public function taxPaymentDocs(){
            return $this->hasMany(TaxPaymentDoc::class, 'tax_payment_id');
        }

        public function receptor() {
            return $this->belongsTo(\App\Models\Hotel\Client::class, 'client_id');
        }

        public function taxBranch() {
            return $this->belongsTo(TaxBranch::class);
        }

        public function taxFormat() {
            return $this->belongsTo(TaxFormat::class);
        }

  //* External Relationships
    public function currency()
    {
        return $this->belongsTo(\App\Models\Static\SAT\CatalogItem::class, 'currency', 'item_key')
            ->where('catalog_code', 'c_Moneda');
    }

    public function paymentForm()
    {
        return $this->belongsTo(\App\Models\Static\SAT\CatalogItem::class, 'payment_form', 'item_key')
            ->where('catalog_code', 'c_FormaPago');
    }
}
