<?php

namespace App\Services\Client;

use App\Models\RoomCategory;
use App\Models\Facility;
use App\Models\MealPackage;
use App\Models\Addon;
use Carbon\Carbon;
use Carbon\CarbonPeriod;

class RoomDetailService
{


    public static function getCategory(string $slug, string $checkIn, string $checkOut, int $adults, int $children, $onlyAvailable = true): RoomCategory
    {
        $category = RoomCategory::where('slug', $slug)
            ->when($onlyAvailable, function ($query) use ($adults, $children) {
                $query->where('max_adults', '>=', $adults)
                    ->where('max_children', '>=', $children);
            })
            ->where('is_published', true)
            ->select('id', 'short_name', 'name', 'slug', 'description', 'base_price', 'max_adults', 'max_children', 'default_bed_count', 'default_bathroom_count', 'default_size', 'details', 'policies')
            ->with([
                'facilities' => fn($q) => $q->where('is_published', true)->select('name', 'is_icon', 'icon_value'),
                'mealPackages' => fn($q) => $q->where('is_published', true)->get(),
                // 'addOns' => fn($q) => $q->where('is_published', true)->select('addons.id', 'sort_order', 'title', 'description', 'tag', 'is_icon', 'icon_value', 'price','calculation_type', 'unit', 'default_availability'),
                'rooms' => fn($q) => $q->where('is_published', true)
                    // Check the room availability 
                    ->when($onlyAvailable, function ($query) use ($checkIn, $checkOut) {
                        $query->availableFor($checkIn, $checkOut);
                    })
                    ->select('id', 'room_category_id', 'room_number', 'floor', 'price_override', 'view', 'description', 'details', 'policies')
                    ->with([
                        'images:alt_text,url',
                        'facilities' => fn($q) => $q->where('is_published', true)->select('name', 'is_icon', 'icon_value'),
                        'mealPackages' => fn($q) => $q->where('is_published', true)->get(),
                        // 'addOns' => fn($q) => $q->where('is_published', true)->select('addons.id', 'sort_order', 'title', 'description', 'tag', 'is_icon', 'icon_value', 'price','calculation_type', 'unit', 'default_availability'),
                    ])
            ])
            ->withCount('rooms')
            ->firstOrFail();

        return $category;
    }

    public static function getAllRoomItems(string $checkIn, string $checkOut): array
    {

        $allRoomFacilities = Facility::where('scope', 'all_rooms')->where('is_published', true)
            ->get(['name', 'is_icon', 'icon_value']);


        $allRoomMealPackages = MealPackage::where('scope', 'all_rooms')->where('is_published', true)->with(['images' => fn($q) => $q->select('url', 'alt_text')])->get();
        $allRoomMealPackages = MealPackageAvailabilityService::filterAvailbleItems($allRoomMealPackages, $checkIn, $checkOut);

        $allRoomAddons = Addon::where('scope', 'all_rooms')->where('is_published', true)
            ->get(['id', 'sort_order', 'title', 'description', 'tag', 'is_icon', 'icon_value', 'price', 'calculation_type', 'unit', 'default_availability'])
            ->filter(fn($a) => $a->isAvailableForDates($checkIn, $checkOut))
            ->values();

        // dump("MealPackages:", $allRoomMealPackages);

        return compact('allRoomFacilities', 'allRoomMealPackages', 'allRoomAddons');
    }

    /**
     * Filter a collection of items (mealPackages or addOns) by availability dates.
     *
     * @param \Illuminate\Support\Collection<int, object> $items
     * @param string $checkIn
     * @param string $checkOut
     * @return \Illuminate\Support\Collection
     */
    public static function filterByDates($items, string $checkIn, string $checkOut)
    {
        return collect($items)
            ->filter(fn($item) => $item->isAvailableForDates($checkIn, $checkOut))
            ->values();
    }
}
