<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\ProductModel;

class Product extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'code', 'name', 'model', 'category_id', 'brand', 'description',
        'purchase_price', 'selling_price', 'cost_price', 'installment_price',
        'warranty_period', 'stock_quantity', 'minimum_stock', 'reorder_level',
        'supplier_id', 'tax_rate', 'image', 'is_active'
    ];

    protected $casts = [
        'purchase_price' => 'decimal:2',
        'selling_price' => 'decimal:2',
        'installment_price' => 'decimal:2',
        'tax_rate' => 'decimal:2',
        'is_active' => 'boolean',
        'stock_quantity' => 'integer',
        'minimum_stock' => 'integer'
    ];

    /**
     * Get the category that owns the product
     */
    public function category()
    {
        return $this->belongsTo(ProductCategory::class, 'category_id');
    }
    
    public function model()
{
    return $this->belongsTo(Model::class, 'model_id');
}

public function productModel()
{
    return $this->belongsTo(ProductModel::class, 'model_id');
}

    /**
     * Get the supplier that owns the product
     */
    public function supplier()
    {
        return $this->belongsTo(Supplier::class);
    }

    /**
     * Get the sale items for the product
     */
    public function saleItems()
    {
        return $this->hasMany(SaleItem::class);
    }

    /**
     * Get the stock movements for the product
     */
    public function stockMovements()
    {
        return $this->hasMany(StockMovement::class);
    }

    /**
     * Get the purchase items for the product
     */
    public function purchaseItems()
    {
        return $this->hasMany(PurchaseItem::class);
    }

    /**
     * Scope a query to only include active products
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Scope a query to only include low stock products
     */
    public function scopeLowStock($query)
    {
        return $query->whereColumn('stock_quantity', '<=', 'minimum_stock');
    }

    /**
     * Scope a query to only include out of stock products
     */
    public function scopeOutOfStock($query)
    {
        return $query->where('stock_quantity', '<=', 0);
    }

    /**
     * Get the stock status attribute
     */
    public function getStockStatusAttribute()
    {
        if ($this->stock_quantity <= 0) {
            return ['class' => 'danger', 'text' => 'Out of Stock'];
        } elseif ($this->stock_quantity <= $this->minimum_stock) {
            return ['class' => 'warning', 'text' => 'Low Stock'];
        }
        return ['class' => 'success', 'text' => 'In Stock'];
    }

    /**
     * Get the formatted selling price attribute
     */
    public function getFormattedSellingPriceAttribute()
    {
        return '৳ ' . number_format($this->selling_price, 2);
    }

    /**
     * Get the formatted purchase price attribute
     */
    public function getFormattedPurchasePriceAttribute()
    {
        return '৳ ' . number_format($this->purchase_price, 2);
    }

    /**
     * Get the full name with code attribute
     */
    public function getFullNameAttribute()
    {
        return $this->name . ' (' . $this->code . ')';
    }

    /**
     * Reduce stock quantity
     */
    public function reduceStock($quantity)
    {
        if ($this->stock_quantity >= $quantity) {
            $this->decrement('stock_quantity', $quantity);
            return true;
        }
        return false;
    }

    /**
     * Increase stock quantity
     */
    public function increaseStock($quantity)
    {
        $this->increment('stock_quantity', $quantity);
        return true;
    }

    /**
     * Check if product has sufficient stock
     */
    public function hasSufficientStock($quantity)
    {
        return $this->stock_quantity >= $quantity;
    }
}