<?php

declare(strict_types=1);

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

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 DberpSalesOutboundOrder extends Model
{
    use GeneratesDberpCode;
    use HasDberpAuditTrail;

    protected $table = 'dberp_sales_outbound_orders';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'code',
        'sales_order_id',
        'customer_id',
        'customer_contact_person_snapshot',
        'customer_mobile_snapshot',
        'customer_phone_snapshot',
        'customer_country_code_snapshot',
        'customer_region_name_snapshot',
        'customer_address_snapshot',
        'warehouse_id',
        'warehouse_contact_phone_snapshot',
        'warehouse_country_code_snapshot',
        'warehouse_region_name_snapshot',
        'warehouse_address_snapshot',
        'handler_employee_id',
        'source_type',
        'status',
        'business_time',
        'total_shipped_quantity',
        'total_amount',
        '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.sales_delete_only_draft'),
                ]);
            }
        });
    }

    protected function casts(): array
    {
        return [
            'sales_order_id' => 'integer',
            'customer_id' => 'integer',
            'warehouse_id' => 'integer',
            'handler_employee_id' => 'integer',
            'business_time' => 'datetime',
            'total_shipped_quantity' => 'decimal:4',
            'total_amount' => 'decimal:4',
            'effective_at' => 'datetime',
            'voided_at' => 'datetime',
        ];
    }

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

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

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

    public function salesOrder(): BelongsTo
    {
        return $this->belongsTo(DberpSalesOrder::class, 'sales_order_id');
    }

    public function customer(): BelongsTo
    {
        return $this->belongsTo(DberpCustomer::class, 'customer_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(DberpSalesOutboundOrderItem::class, 'order_id');
    }

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