<?php

declare(strict_types=1);

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

namespace App\Models;

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

class DberpPurchaseConversionLine extends Model
{
    protected $table = 'dberp_purchase_conversion_lines';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'source_type',
        'source_header_id',
        'source_line_id',
        'target_type',
        'target_header_id',
        'target_line_id',
        'purchase_request_id',
        'purchase_request_item_id',
        'purchase_inquiry_id',
        'purchase_inquiry_item_id',
        'purchase_inquiry_quote_id',
        'purchase_order_id',
        'purchase_order_item_id',
        'supplier_id',
        'product_id',
        'unit_id',
        'quantity',
        'source_snapshot',
        'converted_at',
        'reversed_at',
        'reverse_reason',
        'created_by',
        'updated_by',
    ];

    protected function casts(): array
    {
        return [
            'source_header_id' => 'integer',
            'source_line_id' => 'integer',
            'target_header_id' => 'integer',
            'target_line_id' => 'integer',
            'purchase_request_id' => 'integer',
            'purchase_request_item_id' => 'integer',
            'purchase_inquiry_id' => 'integer',
            'purchase_inquiry_item_id' => 'integer',
            'purchase_inquiry_quote_id' => 'integer',
            'purchase_order_id' => 'integer',
            'purchase_order_item_id' => 'integer',
            'supplier_id' => 'integer',
            'product_id' => 'integer',
            'unit_id' => 'integer',
            'quantity' => 'decimal:4',
            'source_snapshot' => 'array',
            'converted_at' => 'datetime',
            'reversed_at' => 'datetime',
            'created_by' => 'integer',
            'updated_by' => 'integer',
        ];
    }

    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 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 purchaseInquiryQuote(): BelongsTo
    {
        return $this->belongsTo(DberpPurchaseInquiryQuote::class, 'purchase_inquiry_quote_id');
    }

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

    public function purchaseOrderItem(): BelongsTo
    {
        return $this->belongsTo(DberpPurchaseOrderItem::class, 'purchase_order_item_id');
    }

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

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

    public function unit(): BelongsTo
    {
        return $this->belongsTo(DberpUnit::class, 'unit_id');
    }

    public function createdByUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function updatedByUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'updated_by');
    }

    public function isReversed(): bool
    {
        return $this->reversed_at !== null;
    }
}
