<?php

namespace App\Models\Ecommerce\Product\Property;

use App\Models\Ecommerce\Product\Category\Category;
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\HasMany;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Spatie\Translatable\HasTranslations;

class Attribute extends Model implements Sortable
{
    use HasFactory;
    use HasSlug;
    use HasTranslations;
    use SortableTrait;

    /**
     * The attributes that are mass assignable.
     */
    protected $fillable = [
        'name',
        'slug',
        'description',
        'type',
        'is_visible',
        'is_required',
        'is_searchable',
        'is_filterable',
        'sort_order',
    ];

    /**
     * Translatable fields
     *
     * @var array<string>
     */
    public $translatable = ['name', 'description'];

    /**
     * Translatable fields
     *
     * @var array<string, mixed>
     */
    public $sortable = [
        'order_column_name' => 'sort_order',
        'sort_when_creating' => true,
    ];

    /**
     * Get the options for generating the slug.
     */
    public function getSlugOptions(): SlugOptions
    {
        return SlugOptions::create()
            ->generateSlugsFrom('name')
            ->saveSlugsTo('slug');
    }

    /**
     * Attribute values (color: red, black, ...)
     */
    public function values(): HasMany
    {
        return $this->hasMany(AttributeValue::class);
    }

    /**
     * Attribute Category
     */
    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class);
    }

    /**
     * Attributes categories as belongs-to-many
     */
    public function categories(): BelongsToMany
    {
        return $this->belongsToMany(Category::class);
    }

    /**
     * Return available fields types.
     *
     * @return array<string, string>
     */
    public static function typesFields(): array
    {
        return [
            'text' => __('Text'),
            'number' => __('Number'),
            'select' => __('Select option'),
        ];
    }
}
