<?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',
        'description',
        'price',
        'original_price',
        'sku',
        'stock_quantity',
        'image_url',
        'images',
        'features',
        'specifications',
        'compatible_cars',
        'warranty_months',
        'status',
        'created_by',
    ];

    protected $casts = [
        'price' => 'decimal:2',
        'original_price' => 'decimal:2',
        'stock_quantity' => 'integer',
        'warranty_months' => 'integer',
        'status' => 'string',
        'images' => 'array',
        'features' => 'array',
        'specifications' => 'array',
        'compatible_cars' => 'array',
    ];

    /**
     * Relationships
     */
    public function category()
    {
        return $this->belongsTo(ProductCategory::class, 'category_id');
    }

    // Note: make_id and model_id fields don't exist in migration
    // These relationships are commented out as the fields are not in the database
    // public function make()
    // {
    //     return $this->belongsTo(CarMake::class, 'make_id');
    // }

    // public function model()
    // {
    //     return $this->belongsTo(CarModel::class, 'model_id');
    // }

    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

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

    /**
     * Get final price (using original_price as discount price)
     */
    public function getFinalPriceAttribute()
    {
        return $this->original_price ?? $this->price;
    }

    /**
     * Check if product is on discount
     */
    public function getIsDiscountedAttribute()
    {
        return !is_null($this->original_price) && $this->original_price < $this->price;
    }

    /**
     * Get discount percentage
     */
    public function getDiscountPercentageAttribute()
    {
        if (!$this->is_discounted) {
            return 0;
        }
        return round((($this->price - $this->original_price) / $this->price) * 100);
    }

    /**
     * Scope for active products
     */
    public function scopeActive($query)
    {
        return $query->where('status', 'active');
    }

    /**
     * Scope for featured products
     * Note: is_featured field doesn't exist in migration, using status for now
     */
    public function scopeFeatured($query)
    {
        return $query->where('status', 'active');
    }

    /**
     * Scope for products by category
     */
    public function scopeByCategory($query, $categoryId)
    {
        return $query->where('category_id', $categoryId);
    }

    /**
     * Scope for products by make
     */
    public function scopeByMake($query, $makeId)
    {
        return $query->where('make_id', $makeId);
    }

    /**
     * Scope for products by model
     */
    public function scopeByModel($query, $modelId)
    {
        return $query->where('model_id', $modelId);
    }

    /**
     * Scope for discounted products
     */
    public function scopeDiscounted($query)
    {
        return $query->whereNotNull('discount_price')->where('discount_price', '<', \DB::raw('price'));
    }
}