<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Support\Collection;

class MealPackageVariant extends Model
{
    protected $fillable = [
        'meal_package_id',
        'sort_order',
        'title',
        'description',
        'unit',
        'price',
        'currency',
        'is_published',
        'default_availability',
        'is_icon',
        'icon_value',
    ];

    protected $casts = [
        'is_icon' => 'boolean',
        'default_availability' => 'boolean',
        'is_published' => 'boolean',
    ];

    protected $appends = ['available_dates'];

    public function meanPackage()
    {
        return $this->belongsTo(MealPackage::class);
    }


    public function availabilityRules(): MorphMany
    {
        return $this->morphMany(AvailabilityRule::class, 'available');
    }

    protected static function booted()
    {
        static::deleting(function ($variant) {
            $variant->availabilityRules()->delete();
        });
    }

    public function getAvailableDatesAttribute()
    {
        return $this->attributes['available_dates'] ?? collect();
    }

    // Helper to set available dates
    public function setAvailableDates(array|Collection $dates)
    {
        $this->available_dates = $dates;
        return $this;
    }


    // public function isAvailableForDates($checkIn, $checkOut, $parentDefaultActive = null): bool
    // {
    //     $checkIn = Carbon::parse($checkIn);
    //     $checkOut = Carbon::parse($checkOut);

    //     // Start with default_active
    //     $available =  $parentDefaultActive ?? false;

    //     foreach ($this->availabilityRules as $rule) {

    //         // Skip rules outside validity period
    //         if ($rule->valid_from && $checkOut->lt(Carbon::parse($rule->valid_from))) continue;
    //         if ($rule->valid_until && $checkIn->gt(Carbon::parse($rule->valid_until))) continue;

    //         switch ($rule->pattern_type) {

    //             case 'date_range':
    //                 $rangeStart = Carbon::parse($rule->start_date);
    //                 $rangeEnd = Carbon::parse($rule->end_date);

    //                 // If booking overlaps the rule range at all
    //                 if ($checkIn->lte($rangeEnd) && $checkOut->gte($rangeStart)) {
    //                     $available = $rule->rule_mode === 'active';
    //                 }
    //                 break;

    //             case 'weekly':
    //                 // Loop through all days in booking period
    //                 $period = new CarbonPeriod($checkIn, $checkOut);
    //                 foreach ($period as $day) {
    //                     if ($day->dayOfWeek === $rule->day_of_week) {
    //                         $available = $rule->rule_mode === 'active';
    //                     }
    //                 }
    //                 break;

    //             case 'monthly':
    //                 $period = new CarbonPeriod($checkIn, $checkOut);
    //                 foreach ($period as $day) {
    //                     if ($day->day === $rule->day_of_month) {
    //                         $available = $rule->rule_mode === 'active';
    //                     }
    //                 }
    //                 break;

    //             case 'yearly':
    //                 $period = new CarbonPeriod($checkIn, $checkOut);
    //                 foreach ($period as $day) {
    //                     if ($day->day === $rule->day_of_month && $day->month === $rule->month_of_year) {
    //                         $available = $rule->rule_mode === 'active';
    //                     }
    //                 }
    //                 break;
    //         }
    //     }

    //     // return $available;
    //     return $available;
    // }
}
