<?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 LaundrySaleProduct extends Model
{
    use HasFactory;

    protected $table = 'laundry_sale_products';

    public const BILLING_UNIT_PIECE = 'piece';

    public const BILLING_UNIT_KG = 'kg';

    public const TIPO_SERVICIO = 'servicio';

    public const TIPO_PRODUCTO = 'producto';

    protected $fillable = [
        'laundry_sale_category_id',
        'code',
        'name',
        'tipo',
        'description',
        'default_etapa_id',
        'default_load_type_id',
        'default_tipo_servicio_id',
        'billing_unit',
        'unit_price',
        'extra_cost',
        'is_active',
        'sort_order',
        'inventory_product_id',
        'tracks_stock',
        'stock_quantity',
    ];

    protected $casts = [
        'unit_price' => 'decimal:2',
        'extra_cost' => 'decimal:2',
        'is_active' => 'boolean',
        'sort_order' => 'integer',
        'tracks_stock' => 'boolean',
        'stock_quantity' => 'integer',
    ];

    public function manejaInventarioPos(): bool
    {
        return $this->tipo === self::TIPO_PRODUCTO
            && $this->billing_unit === self::BILLING_UNIT_PIECE
            && $this->tracks_stock;
    }

    public function tieneStockSuficiente(float $cantidad): bool
    {
        if (! $this->manejaInventarioPos()) {
            return true;
        }
        $stock = $this->stock_quantity ?? 0;

        return $stock >= (int) round($cantidad, 0);
    }

    public function descontarStock(int $cantidad): void
    {
        if (! $this->manejaInventarioPos() || $cantidad <= 0) {
            return;
        }
        $this->decrement('stock_quantity', $cantidad);
    }

    public function devolverStock(int $cantidad): void
    {
        if (! $this->manejaInventarioPos() || $cantidad <= 0) {
            return;
        }
        $this->increment('stock_quantity', $cantidad);
    }

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

    public function defaultEtapa(): BelongsTo
    {
        return $this->belongsTo(Etapa::class, 'default_etapa_id');
    }

    public function defaultLoadType(): BelongsTo
    {
        return $this->belongsTo(LoadType::class, 'default_load_type_id');
    }

    public function defaultTipoServicio(): BelongsTo
    {
        return $this->belongsTo(TipoServicio::class, 'default_tipo_servicio_id');
    }

    public function inventoryProduct(): BelongsTo
    {
        return $this->belongsTo(InventoryProduct::class, 'inventory_product_id');
    }

    public function ordenLavanderiaDetalles(): HasMany
    {
        return $this->hasMany(OrdenLavanderiaDetalle::class, 'laundry_sale_product_id');
    }

    public function servicePrices(): HasMany
    {
        return $this->hasMany(LaundrySaleProductServicePrice::class, 'laundry_sale_product_id');
    }
}
