<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'name',
        'code',
        'type',
        'description',
        'image',
        'category_id',
        'brand_id',
        'unit_id',
        'tax_id',
        'selling_price',
        'purchase_price',
        'discount_type',
        'discount_value',
        'barcode',
        'stock',
        'alert_quantity',
        'status',
        'gallery_images',
        'created_by',
    ];

    protected function casts(): array
    {
        return [
            'selling_price' => 'decimal:2',
            'purchase_price' => 'decimal:2',
            'discount_value' => 'decimal:2',
            'stock' => 'integer',
            'alert_quantity' => 'integer',
            'status' => 'boolean',
            'gallery_images' => 'array',
        ];
    }

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

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

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

    /**
     * Get the tax rate that owns the product
     */
    public function tax(): BelongsTo
    {
        return $this->belongsTo(TaxRate::class, 'tax_id');
    }
}
