<?php

declare(strict_types=1);

namespace App\Models;

use App\Business\Inventory\Enums\InventoryDocumentStatus;
use App\Models\Concerns\GeneratesDberpCode;
use App\Models\Concerns\HasDberpAuditTrail;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Validation\ValidationException;

class DberpPurchaseReceiptOrder extends Model
{
    use GeneratesDberpCode;
    use HasDberpAuditTrail;

    protected $table = 'dberp_purchase_receipt_orders';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'code',
        'purchase_order_id',
        'supplier_id',
        'supplier_contact_person_snapshot',
        'supplier_mobile_snapshot',
        'supplier_phone_snapshot',
        'supplier_country_code_snapshot',
        'supplier_region_name_snapshot',
        'supplier_address_snapshot',
        'warehouse_id',
        'warehouse_contact_phone_snapshot',
        'warehouse_country_code_snapshot',
        'warehouse_region_name_snapshot',
        'warehouse_address_snapshot',
        'handler_employee_id',
        'status',
        'business_time',
        'arrival_date',
        'total_arrival_quantity',
        'total_received_quantity',
        'total_amount',
        'delivery_note',
        'effective_at',
        'voided_at',
        'remark',
    ];

    protected static function booted(): void
    {
        static::deleting(function (self $order): void {
            if ($order->status !== InventoryDocumentStatus::Draft->value) {
                throw ValidationException::withMessages([
                    'status' => __('dberp.errors.purchase_delete_only_draft'),
                ]);
            }
        });
    }

    protected function casts(): array
    {
        return [
            'purchase_order_id' => 'integer',
            'supplier_id' => 'integer',
            'warehouse_id' => 'integer',
            'handler_employee_id' => 'integer',
            'business_time' => 'datetime',
            'arrival_date' => 'date',
            'total_arrival_quantity' => 'decimal:4',
            'total_received_quantity' => 'decimal:4',
            'total_amount' => 'decimal:4',
            'effective_at' => 'datetime',
            'voided_at' => 'datetime',
        ];
    }

    public function getDberpNumberScope(): string
    {
        return 'purchase_receipt';
    }

    public function getDberpAuditActionPrefix(): string
    {
        return 'dberp.purchase_receipt';
    }

    /**
     * @return list<string>
     */
    public function getDberpAuditFields(): array
    {
        return $this->getFillable();
    }

    public function purchaseOrder(): BelongsTo
    {
        return $this->belongsTo(DberpPurchaseOrder::class, 'purchase_order_id');
    }

    public function supplier(): BelongsTo
    {
        return $this->belongsTo(DberpSupplier::class, 'supplier_id');
    }

    public function warehouse(): BelongsTo
    {
        return $this->belongsTo(DberpWarehouse::class, 'warehouse_id');
    }

    public function handlerEmployee(): BelongsTo
    {
        return $this->belongsTo(DberpEmployee::class, 'handler_employee_id');
    }

    public function items(): HasMany
    {
        return $this->hasMany(DberpPurchaseReceiptOrderItem::class, 'order_id');
    }

    public function purchaseExceptions(): HasMany
    {
        return $this->hasMany(DberpPurchaseException::class, 'purchase_receipt_order_id');
    }

    public function returns(): HasMany
    {
        return $this->hasMany(DberpPurchaseReturnOrder::class, 'purchase_receipt_order_id');
    }
}