<?php

namespace App\Models\Inventory;

use App\Models\Branch;
use App\Models\Catalog\Product;
use App\Models\Concerns\Auditable;
use App\Models\Concerns\BelongsToTenant;
use App\Models\Concerns\BranchScoped;
use App\Models\Concerns\HasDefaultUnitFromProduct;
use App\Models\Lookups\DamageReason;
use App\Models\Lookups\UnitOfMeasure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class Damage extends Model
{
    use Auditable, BelongsToTenant, BranchScoped, HasDefaultUnitFromProduct, SoftDeletes;

    protected $fillable = [
        'damage_number', 'branch_id', 'product_id', 'unit_id', 'damage_reason_id', 'stock_batch_id', 'product_serial_id',
        'quantity_damaged', 'unit_cost', 'total_damage_cost',
        'source_type', 'source_id', 'note', 'recorded_by',
    ];

    protected function casts(): array
    {
        return [
            'quantity_damaged' => 'decimal:4',
            'unit_cost' => 'decimal:2',
            'total_damage_cost' => 'decimal:2',
        ];
    }

    public function branch(): BelongsTo
    {
        return $this->belongsTo(Branch::class, 'branch_id');
    }

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

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

    public function reason(): BelongsTo
    {
        return $this->belongsTo(DamageReason::class, 'damage_reason_id');
    }

    public function stockBatch(): BelongsTo
    {
        return $this->belongsTo(StockBatch::class, 'stock_batch_id');
    }

    public function serial(): BelongsTo
    {
        return $this->belongsTo(ProductSerial::class, 'product_serial_id');
    }
}
