<?php

namespace App\Models;

use App\Enums\ProductStockQuality;
use App\Models\Concerns\InteractsWithActivityLog;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

#[Guarded(['id'])]
class StokOpnameItem extends Model
{
    // 1. Use Trait
    use HasFactory, InteractsWithActivityLog;

    // 2. Casting
    protected function casts(): array
    {
        return [
            'difference' => 'integer',
            'physical_stock' => 'integer',
            'system_stock' => 'integer',
            'stock_quality' => ProductStockQuality::class,
        ];
    }

    protected $appends = ['product_name', 'variant_name'];

    public function getProductNameAttribute(): string
    {
        return $this->productVariant->product->name;
    }

    public function getVariantNameAttribute(): string
    {
        $qualityLabel = $this->stock_quality ? ' ('.$this->stock_quality->label().')' : '';

        return $this->productVariant->name.$qualityLabel;
    }

    // 3. Relation
    public function productVariant(): BelongsTo
    {
        return $this->belongsTo(ProductVariant::class)->withTrashed();
    }

    public function stokOpname(): BelongsTo
    {
        return $this->belongsTo(StokOpname::class)->withTrashed();
    }
}
