<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class DocumentData extends Model
{
    protected $table = 'document_data';

    protected $fillable = [
        'document_id', 'invoice_number', 'invoice_title_id', 'invoice_record_as_id',
        'supplier_id', 'customer_id', 'category_id', 'vat_gst_rate_id', 'payment_method_id',
        'document_date', 'due_date', 'subtotal', 'tax_total', 'total_amount',
        'currency', 'transaction_currency_id', 'exchange_rate',
        'vat_amount', 'net_amount', 'converted_gross_amount', 'converted_vat_amount',
        'converted_net_amount', 'description', 'transaction_missing', 'vat_missing',
        'description_missing', 'additional_options', 'data_entry_operator_id',
        'notes', 'tags', 'extracted_text', 'line_items', 'extracted_data',
    ];

    protected $casts = [
        'document_date' => 'date',
        'due_date' => 'date',
        'line_items' => 'array',
        'extracted_data' => 'array',
        'subtotal' => 'decimal:4',
        'tax_total' => 'decimal:4',
        'total_amount' => 'decimal:4',
        'vat_amount' => 'decimal:4',
        'net_amount' => 'decimal:4',
        'converted_gross_amount' => 'decimal:4',
        'converted_vat_amount' => 'decimal:4',
        'converted_net_amount' => 'decimal:4',
        'exchange_rate' => 'decimal:6',
        'transaction_missing' => 'boolean',
        'vat_missing' => 'boolean',
        'description_missing' => 'boolean',
    ];

    /**
     * Map the .NET DocumentDataRequest body shape onto our column names.
     */
    public static function mapFromRequest(array $payload): array
    {
        return array_filter([
            'invoice_number'           => $payload['invoiceNumber']           ?? null,
            'invoice_title_id'         => $payload['invoiceTitleId']          ?? null,
            'invoice_record_as_id'     => $payload['invoiceRecordAsId']       ?? null,
            'supplier_id'              => $payload['supplierId']              ?? null,
            'customer_id'              => $payload['customerId']              ?? null,
            'category_id'              => $payload['categoryId']              ?? null,
            'vat_gst_rate_id'          => $payload['vatGstRateId']            ?? null,
            'payment_method_id'        => $payload['paymentMethodId']         ?? null,
            'document_date'            => $payload['invoiceDate']             ?? $payload['documentDate'] ?? null,
            'due_date'                 => $payload['dueDate']                 ?? null,
            'subtotal'                 => $payload['netAmount']               ?? null,
            'tax_total'                => $payload['vatAmount']               ?? null,
            'total_amount'             => $payload['grossAmount']             ?? null,
            'currency'                 => $payload['currency']                ?? null,
            'transaction_currency_id'  => $payload['transactionCurrencyId']   ?? null,
            'exchange_rate'            => $payload['exchangeRate']            ?? null,
            'vat_amount'               => $payload['vatAmount']               ?? null,
            'net_amount'               => $payload['netAmount']               ?? null,
            'converted_gross_amount'   => $payload['convertedGrossAmount']    ?? null,
            'converted_vat_amount'    => $payload['convertedVATAmount']       ?? null,
            'converted_net_amount'     => $payload['convertedNetAmount']      ?? null,
            'description'              => $payload['description']             ?? null,
            'transaction_missing'      => isset($payload['transactionMissing']) ? (bool)$payload['transactionMissing'] : null,
            'vat_missing'              => isset($payload['vatMissing']) ? (bool)$payload['vatMissing'] : null,
            'description_missing'      => isset($payload['descriptionMissing']) ? (bool)$payload['descriptionMissing'] : null,
            'additional_options'       => $payload['additionalOptions']       ?? null,
            'data_entry_operator_id'   => $payload['dataEntryOperatorId']     ?? null,
            'notes'                    => $payload['notes']                   ?? null,
            'tags'                     => $payload['tags']                    ?? null,
            'extracted_text'           => $payload['extractedText']           ?? null,
            'line_items'               => $payload['lineItems']               ?? null,
        ], fn ($v) => $v !== null);
    }

    public function document(): BelongsTo { return $this->belongsTo(Document::class); }
    public function supplier(): BelongsTo { return $this->belongsTo(Contact::class, 'supplier_id'); }
    public function customer(): BelongsTo { return $this->belongsTo(Contact::class, 'customer_id'); }
}
