<?php

namespace App\Models;

use App\Models\Scopes\ProductVariantScope;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Image\Enums\Fit;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

#[Appends(['formatted_name', 'formatted_stock', 'formatted_reject_stock', 'formatted_retail_stock'])]
#[Guarded(['id'])]
#[ScopedBy([ProductVariantScope::class])]
class ProductVariant extends Model implements HasMedia
{
    use HasFactory, InteractsWithMedia, SoftDeletes;

    protected function casts(): array
    {
        return [
            'stock' => 'integer',
            'reject_stock' => 'integer',
            'retail_stock' => 'integer',
        ];
    }

    protected function formattedName(): Attribute
    {
        return Attribute::make(
            get: fn () => ucfirst($this->name),
        );
    }

    protected function formattedStock(): Attribute
    {
        return Attribute::make(
            get: fn () => number_format($this->stock, 0, ',', '.'),
        );
    }

    protected function formattedRejectStock(): Attribute
    {
        return Attribute::make(
            get: fn () => number_format($this->reject_stock, 0, ',', '.'),
        );
    }

    protected function formattedRetailStock(): Attribute
    {
        return Attribute::make(
            get: fn () => number_format($this->retail_stock, 0, ',', '.'),
        );
    }

    public function orderItems(): HasMany
    {
        return $this->hasMany(OrderItem::class);
    }

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

    public function productPrices(): HasMany
    {
        return $this->hasMany(ProductPrice::class, 'variant_id');
    }

    public function restockItems(): HasMany
    {
        return $this->hasMany(RestockItem::class);
    }

    public function retailStockHistories(): HasMany
    {
        return $this->hasMany(RetailStockHistory::class);
    }

    public function stokOpnameItems(): HasMany
    {
        return $this->hasMany(StokOpnameItem::class);
    }

    public function stockMutations(): HasMany
    {
        return $this->hasMany(StockMutation::class, 'stockable_id')
            ->where('stockable_type', self::class);
    }

    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('images');
    }

    public function registerMediaConversions(?Media $media = null): void
    {
        $this->addMediaConversion('thumb')
            ->fit(Fit::Contain, 150, 150);
    }
}
