<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Product extends Model
{
    use HasFactory;

    protected $fillable = [
        'store_id',
        'category_id',
        'name',
        'description',
        'image',
        'stock_quantity',
        'price',
        'unit_type',
        'is_active',
    ];

    protected function casts(): array
    {
        return [
            'stock_quantity' => 'integer',
            'price' => 'decimal:2',
            'is_active' => 'boolean',
        ];
    }

    public function store(): BelongsTo
    {
        return $this->belongsTo(Store::class);
    }

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

    public function stockAlerts(): HasMany
    {
        return $this->hasMany(StockAlert::class);
    }

    public function sellerProductSales(): HasMany
    {
        return $this->hasMany(SellerProductSale::class);
    }

    public function productReservations(): HasMany
    {
        return $this->hasMany(ProductReservation::class);
    }

    public function getStockLabelAttribute(): string
    {
        if ($this->stock_quantity === 0) return 'Yok';
        if ($this->stock_quantity <= 10) return 'Az';
        return 'Var';
    }
}
