<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Models;

use App\Business\Purchase\Enums\PurchaseInquiryQuoteStatus;
use App\Models\Concerns\GeneratesDberpCode;
use App\Models\Concerns\HasDberpAuditTrail;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class DberpPurchaseInquiryQuote extends Model
{
    use GeneratesDberpCode;
    use HasDberpAuditTrail;

    protected $table = 'dberp_purchase_inquiry_quotes';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'code',
        'purchase_inquiry_id',
        'purchase_inquiry_item_id',
        'purchase_request_id',
        'purchase_request_code',
        'source_system',
        'external_reference_id',
        'purchase_request_item_id',
        'product_id',
        'product_sku_id',
        'product_sku_code',
        'product_sku_name',
        'product_sku_spec',
        'external_product_id',
        'external_sku',
        'product_name_snapshot',
        'product_spec_snapshot',
        'supplier_id',
        'supplier_name_snapshot',
        'quote_date',
        'valid_until',
        'unit_price',
        'min_order_quantity',
        'lead_time_days',
        'tax_rate',
        'is_tax_included',
        'is_recommended',
        'status',
        'invalidated_at',
        'quote_note',
        'remark',
    ];

    protected function casts(): array
    {
        return [
            'purchase_inquiry_id' => 'integer',
            'purchase_inquiry_item_id' => 'integer',
            'purchase_request_id' => 'integer',
            'purchase_request_item_id' => 'integer',
            'product_id' => 'integer',
            'product_sku_id' => 'integer',
            'supplier_id' => 'integer',
            'quote_date' => 'date',
            'valid_until' => 'date',
            'unit_price' => 'decimal:4',
            'min_order_quantity' => 'decimal:4',
            'lead_time_days' => 'integer',
            'tax_rate' => 'decimal:4',
            'is_tax_included' => 'boolean',
            'is_recommended' => 'boolean',
            'invalidated_at' => 'datetime',
        ];
    }

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

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

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

    public function isUsable(): bool
    {
        return PurchaseInquiryQuoteStatus::tryFrom((string) $this->status)?->isUsable() ?? false;
    }

    public function purchaseInquiry(): BelongsTo
    {
        return $this->belongsTo(DberpPurchaseInquiry::class, 'purchase_inquiry_id');
    }

    public function purchaseInquiryItem(): BelongsTo
    {
        return $this->belongsTo(DberpPurchaseInquiryItem::class, 'purchase_inquiry_item_id');
    }

    public function purchaseRequest(): BelongsTo
    {
        return $this->belongsTo(DberpPurchaseRequest::class, 'purchase_request_id');
    }

    public function purchaseRequestItem(): BelongsTo
    {
        return $this->belongsTo(DberpPurchaseRequestItem::class, 'purchase_request_item_id');
    }

    public function product(): BelongsTo
    {
        return $this->belongsTo(DberpProduct::class, 'product_id');
    }

    public function productSku(): BelongsTo
    {
        return $this->belongsTo(DberpProductSku::class, 'product_sku_id');
    }

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