<?php

namespace App\Models;

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

#[Fillable(['product_id', 'product_variant_id', 'admin_user_id', 'order_id', 'type', 'quantity_change', 'stock_before', 'stock_after', 'note'])]
class InventoryMovement extends Model
{
    public const TYPE_INITIAL = 'initial';
    public const TYPE_MANUAL_ADJUSTMENT = 'manual_adjustment';
    public const TYPE_ORDER_DEDUCTION = 'order_deduction';

    protected function casts(): array
    {
        return [
            'quantity_change' => 'integer',
            'stock_before' => 'integer',
            'stock_after' => 'integer',
        ];
    }

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

    public function variant(): BelongsTo
    {
        return $this->belongsTo(ProductVariant::class, 'product_variant_id');
    }

    public function admin(): BelongsTo
    {
        return $this->belongsTo(AdminUser::class, 'admin_user_id');
    }

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

    public function typeLabel(): string
    {
        return match ($this->type) {
            self::TYPE_INITIAL => 'Initial Stock',
            self::TYPE_MANUAL_ADJUSTMENT => 'Manual Adjustment',
            self::TYPE_ORDER_DEDUCTION => 'Order Deduction',
            default => str($this->type)->replace('_', ' ')->title()->toString(),
        };
    }
}
