<?php

namespace App\Models;

use App\Enums\CategoryType;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
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 Category extends Model implements HasMedia
{
    use HasFactory, HashableId, HasSlug, InteractsWithMedia, SoftDeletes;

    protected $guarded = ['id'];

    protected function casts(): array
    {
        return [
            'sort_order' => 'int',
            'type' => CategoryType::class,
        ];
    }

    #[Scope]
    protected function perfume(Builder $query): Builder
    {
        return $query->where('type', CategoryType::PERFUME);
    }

    #[Scope]
    protected function article(Builder $query): Builder
    {
        return $query->where('type', CategoryType::ARTICLE);
    }

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

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

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