<?php

namespace App\Models;

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

class Product extends Model
{
    use HasFactory;

    protected $fillable = [
        'category_id',
        'name',
        'slug',
        'description',
        'price',
        'sale_price',
        'image',
        'rating',
        'discount_percent',
        'is_flash_deal',
        'is_special_pick',
        'is_best_seller',
        'stock',
        'min_order_quantity',
    ];

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

    public function orderItems()
    {
        return $this->hasMany(OrderItem::class);
    }

    public function reviews()
    {
        return $this->hasMany(ProductReview::class);
    }

    public function getDisplayPriceAttribute()
    {
        return $this->sale_price ?: $this->price;
    }

    public function getIsInStockAttribute()
    {
        return $this->stock > 0;
    }
}
