<?php

namespace App\Models\Admin;

use App\Enums\ProductCategories;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Product extends Model
{
    use HasFactory, HasTranslations;

    protected $fillable = [
        'old_id',
        'title',
        'slug',
        'technical_features',
        'pressure_temperature_ranges',
        'material_specs',
        'figure_options',
        'dimensions',
        'dikkan_figure',
        'impa_code',
        'icons',
        'image_path',
        'td_image_path',
        'datasheet_path',
        'glb_file_path',
        'youtube_video_id',
        'status',
        'ordernum'
    ];

    public $translatable = [
        'title',
        'slug',
        'technical_features',
        'pressure_temperature_ranges',
        'material_specs',
        'figure_options',
        'dimensions',
    ];

    public function taxonomies()
    {
        return $this->belongsToMany(Taxonomy::class, 'taxonomy_product');
    }

    public function taxonomiesByCategory(ProductCategories $category)
    {
        return $this->taxonomies->where('parent_id', $category->value);
    }

    public function scopeSearch($query, $keyword)
    {
        return $query->where('title', 'like', '%' . $keyword . '%');
    }

    public function images()
    {
        return $this->hasMany(ProductImage::class);
    }
}
