<?php

namespace App\Services\Client;

use App\Models\BookingPriceAdjustment;
use Carbon\Carbon;

class BookingPriceAdjustmentService
{
    /**
     * Get all valid adjustments for today
     */
    public static function getTodayValid()
    {
        $today = Carbon::today();
        return BookingPriceAdjustment::query()
            ->where('is_active', true)
            ->where(function ($q) use ($today) {
                $q->whereNull('start_date')->orWhere('start_date', '<=', $today);
            })
            ->where(function ($q) use ($today) {
                $q->whereNull('end_date')->orWhere('end_date', '>=', $today);
            })
            ->get();
    }

    /**
     * Get adjustments based on booking context
     */
    public static function getForBooking($rooms = 0, $nights = 0, $total = 0)
    {
        $today = Carbon::today();

        $query = BookingPriceAdjustment::query()
            ->where('is_active', true)

            // 📅 Date rules
            ->where(function ($q) use ($today) {
                $q->whereNull('start_date')
                    ->orWhere('start_date', '<=', $today);
            })
            ->where(function ($q) use ($today) {
                $q->whereNull('end_date')
                    ->orWhere('end_date', '>=', $today);
            });

        // 🏨 Room rules
        if ($rooms > 0) {

            $query->where(function ($q) use ($rooms) {
                $q->whereNull('min_rooms')
                    ->orWhere('min_rooms', '<=', $rooms);
            });

            $query->where(function ($q) use ($rooms) {
                $q->whereNull('max_rooms')
                    ->orWhere('max_rooms', '>=', $rooms);
            });
        }

        // 🌙 Night rules
        if ($nights > 0) {

            $query->where(function ($q) use ($nights) {
                $q->whereNull('min_nights')
                    ->orWhere('min_nights', '<=', $nights);
            });

            $query->where(function ($q) use ($nights) {
                $q->whereNull('max_nights')
                    ->orWhere('max_nights', '>=', $nights);
            });
        }

        // 💰 Total rules
        if ($total > 0) {

            $query->where(function ($q) use ($total) {
                $q->whereNull('min_total')
                    ->orWhere('min_total', '<=', $total);
            });

            $query->where(function ($q) use ($total) {
                $q->whereNull('max_total')
                    ->orWhere('max_total', '>=', $total);
            });
        }

        return $query->get();
    }

    /**
     * Clean format for session storage
     */
    public static function formatForSession($adjustments)
    {
        return $adjustments->map(fn($item) => [
            'id' => $item->id,
            'type' => $item->type,
            'name' => $item->name,
            'description' => $item->description,
            'value' => $item->value,
            'value_type' => $item->value_type,
            'direction' => $item->direction,
        ]);
    }

    /**
     * Store in session
     */
    public static function storeInSession($adjustments)
    {
        session([
            'booking_price_adjustments' =>
            self::formatForSession($adjustments)
        ]);
    }
}
