<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Coupon extends Model {
    protected $fillable = ['code','description','type','value','min_order','max_uses','used_count','active','expires_at'];
    protected $casts = ['active'=>'boolean','expires_at'=>'datetime','value'=>'decimal:2','min_order'=>'decimal:2'];
    public function isValid(float $cartTotal): bool {
        if (!$this->active) return false;
        if ($this->expires_at && $this->expires_at->isPast()) return false;
        if ($this->max_uses && $this->used_count >= $this->max_uses) return false;
        if ($cartTotal < $this->min_order) return false;
        return true;
    }
    public function calculateDiscount(float $total): float {
        if ($this->type === 'percent') return round($total * $this->value / 100, 2);
        return min($this->value, $total);
    }
}
