<?php

namespace App\Models\Tenants;

use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * 재고 입출고 거래 이력
 *
 * 모든 재고 변동을 스택으로 기록합니다.
 * - IN: 입고 (재고 증가)
 * - OUT: 출고/생산투입 (재고 감소)
 * - RESERVE: 수주 확정으로 예약
 * - RELEASE: 수주 취소로 예약 해제
 */
class StockTransaction extends Model
{
    use BelongsToTenant;

    const UPDATED_AT = null; // updated_at 사용 안함 (이력은 불변)

    // 거래 유형 상수
    public const TYPE_IN = 'IN';

    public const TYPE_OUT = 'OUT';

    public const TYPE_RESERVE = 'RESERVE';

    public const TYPE_RELEASE = 'RELEASE';

    public const TYPES = [
        self::TYPE_IN => '입고',
        self::TYPE_OUT => '출고',
        self::TYPE_RESERVE => '예약',
        self::TYPE_RELEASE => '예약해제',
    ];

    // 사유 상수
    public const REASON_RECEIVING = 'receiving';

    public const REASON_WORK_ORDER_INPUT = 'work_order_input';

    public const REASON_SHIPMENT = 'shipment';

    public const REASON_ORDER_CONFIRM = 'order_confirm';

    public const REASON_ORDER_CANCEL = 'order_cancel';

    public const REASON_PRODUCTION_OUTPUT = 'production_output';

    public const REASONS = [
        self::REASON_RECEIVING => '입고',
        self::REASON_WORK_ORDER_INPUT => '생산투입',
        self::REASON_SHIPMENT => '출하',
        self::REASON_ORDER_CONFIRM => '수주확정',
        self::REASON_ORDER_CANCEL => '수주취소',
        self::REASON_PRODUCTION_OUTPUT => '생산입고',
    ];

    protected $fillable = [
        'tenant_id',
        'stock_id',
        'stock_lot_id',
        'type',
        'qty',
        'balance_qty',
        'reference_type',
        'reference_id',
        'lot_no',
        'reason',
        'remark',
        'item_code',
        'item_name',
        'created_by',
    ];

    protected $casts = [
        'stock_id' => 'integer',
        'stock_lot_id' => 'integer',
        'qty' => 'decimal:3',
        'balance_qty' => 'decimal:3',
        'reference_id' => 'integer',
        'created_by' => 'integer',
        'created_at' => 'datetime',
    ];

    // ===== 관계 =====

    public function stock(): BelongsTo
    {
        return $this->belongsTo(Stock::class);
    }

    public function stockLot(): BelongsTo
    {
        return $this->belongsTo(StockLot::class);
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(\App\Models\Members\User::class, 'created_by');
    }

    // ===== Accessors =====

    public function getTypeLabelAttribute(): string
    {
        return self::TYPES[$this->type] ?? $this->type;
    }

    public function getReasonLabelAttribute(): string
    {
        return self::REASONS[$this->reason] ?? ($this->reason ?? '-');
    }
}
