<?php

namespace App\Models;

use App\Enums\ProductStockQuality;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

#[Appends(['stock_quality_label'])]
#[Guarded(['id'])]
class StokOpnameItem extends Model
{
    use HasFactory;

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

    protected function stockQualityLabel(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->stock_quality->label(),
        );
    }

    #[Scope]
    protected function good(Builder $query): void
    {
        $query->where('stock_quality', ProductStockQuality::GOOD);
    }

    #[Scope]
    protected function reject(Builder $query): void
    {
        $query->where('stock_quality', ProductStockQuality::REJECT);
    }

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

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