<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Product extends Model
{
    use HasFactory;
    
    // protected $fillable = ['category_id', 'name', 'description', 'image', 'original_amount', 'discount_amount', 'include_product', 'multiple_image', 'lable_value', 'discount', 'gst', 'sku', 'hsn_code', 'stock', 'slug', 'gst_type', 'status', 'meta_title', 'meta_description', 'meta_keyword'];
    
    public function product_list()
    {
        return $this->hasMany(ProductDetails::class,'product_id','id');
    }
    
    public static function createSlug($title, $id = null)
    {
        $slug = Str::slug($title);
        $count = Product::where('slug', $slug)->when($id, function ($query) use ($id) {
            return $query->where('id', '!=', $id);
        })->count();

        return $count ? "{$slug}-{$count}" : $slug;
    }
    
    public function category() {
        return $this->belongsTo(Category::class, 'category_id');
    }
    
    public function sub_category() {
        return $this->belongsTo(SubCategory::class, 'sub_category_id');
    }
    
    public function brands() {
        return $this->belongsTo(Brand::class, 'brand_id');
    }
    
}
