<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PointReward extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'name_ar',
        'description',
        'description_ar',
        'type',
        'points_required',
        'value',
        'currency',
        'is_active',
        'max_redemptions',
        'current_redemptions',
        'valid_from',
        'valid_until',
    ];

    protected $casts = [
        'is_active' => 'boolean',
        'valid_from' => 'datetime',
        'valid_until' => 'datetime',
    ];

    /**
     * استردادات هذه المكافأة
     */
    public function redemptions()
    {
        return $this->hasMany(PointRedemption::class);
    }

    /**
     * التحقق من إمكانية الاسترداد
     */
    public function canBeRedeemed()
    {
        if (!$this->is_active) {
            return false;
        }

        if ($this->max_redemptions && $this->current_redemptions >= $this->max_redemptions) {
            return false;
        }

        if ($this->valid_from && now() < $this->valid_from) {
            return false;
        }

        if ($this->valid_until && now() > $this->valid_until) {
            return false;
        }

        return true;
    }

    /**
     * زيادة عدد الاستردادات
     */
    public function incrementRedemptions()
    {
        $this->increment('current_redemptions');
    }
}
