<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    use HasFactory, SoftDeletes;
    protected $table = 'product';
    protected $fillable = [
        'id',
        'category_id',
        'product_name',
        'product_tags',
        'sku',
        'color_id',
        'product_price',
        'cutted_price',
        'product_size',
        'product_description',
        'product_image',
        'qty',
        'views',
        'weight',
        'rating',
        'orders',
        'status',
        'created_at',
        'updated_at',
    ];
    public function category()
    {
        return $this->belongsTo(Category::class, 'category_id', 'id');
    }

    public function productcolor()
    {
        return $this->hasMany(Productcolor::class);
    }
    public function carts()
    {
        return $this->hasMany(Cart::class, 'productid', 'id');
    }
    public function wishlist()
    {
        return $this->hasMany(Wishlist::class, 'product_id', 'id');
    }

    public function orders()
    {
        return $this->hasMany(Order::class, 'product_id', 'id');
    }
    public function dealcheck()
{
    return $this->hasOne(Dealcheck::class, 'product_id', 'id');
}

    // Boot method to add model events
    protected static function boot()
    {
        parent::boot();

        static::deleting(function ($product) {
            // Delete related cart records
            $product->carts()->delete();
            $product->wishlist()->delete();
            $product->orders()->delete();
            $product->productcolor()->delete();
        });

        static::restored(function ($product) {
            // Restore related productcolor records
            $product->productcolor()->withTrashed()->restore();
        });
    }
}
