<?php

namespace App\Models;

use App\Enums\Concentration;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Veelasky\LaravelHashId\Eloquent\HashableId;

class Perfume extends Model implements HasMedia
{
    use HasFactory, HashableId, HasSlug, InteractsWithMedia, SoftDeletes;

    protected $guarded = ['id'];

    protected function casts(): array
    {
        return [
            'concentration' => Concentration::class,
            'cost_price' => 'int',
            'sale_price' => 'int',
            'point_per_ml' => 'int',
        ];
    }

    public function getSlugOptions(): SlugOptions
    {
        return SlugOptions::create()
            ->generateSlugsFrom('name')
            ->saveSlugsTo('slug');
    }

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

    public function categories(): BelongsToMany
    {
        return $this->belongsToMany(Category::class);
    }

    public function outlets(): BelongsToMany
    {
        return $this->belongsToMany(Outlet::class)->withPivot('stock');
    }

    public function purchaseItems(): MorphMany
    {
        return $this->morphMany(PurchaseItem::class, 'purchasable');
    }

    public function items(): MorphMany
    {
        return $this->morphMany(PurchaseItem::class, 'orderable');
    }
}
