import { AddonSelected, MealPackageSelected, RoomDailyPriceType } from "@/types/home/bookings"


interface MealPackagePriceInput {
    nights: number
    adults: number
    children: number
    selectedMealPackage: MealPackageSelected | null
}
interface RoomPrice {
    nights: number
    adults: number
    children: number
    room_daily_prices: RoomDailyPriceType[]
    selectedMealPackage: MealPackageSelected | null
    selectedAddons: AddonSelected[]
}

interface AddonCalculationInput {
    selectedAddons: AddonSelected[];
    adults: number;
    children: number;
    nights: number;               // number of nights in the booking
    rooms?: number;               // number of rooms booked
}

export function calculateAddonTotal({ selectedAddons, adults, children, nights, rooms = 1 }: AddonCalculationInput): number {
    return selectedAddons.reduce((total, addon) => {
        let count = 1;

        switch (addon.calculation_type) {
            case 'per_booking':
                count = 1;
                break;

            case 'per_room':
                count = rooms;
                break;

            case 'per_room_per_night':
                count = rooms * nights;
                break;

            case 'per_night':
                count = nights;
                break;

            case 'per_person':
                count = adults + children;
                break;

            case 'per_person_per_night':
                count = (adults + children) * nights;
                break;

            // TODO add fetires
            // case 'per_quantity':
            //     count = addon.quantity ?? 1;
            //     break;

            default:
                count = 1;
        }

        const unit = addon.unit ?? 1; // If addon has units (beds, cars, etc.)
        return total + addon.price * count * unit;
    }, 0);
}

export function calMealPackageTotal({ nights, adults, children, selectedMealPackage }: MealPackagePriceInput) {

    const guest = adults + children

    if (!selectedMealPackage) return 0

    // Juset meal pckage no variant
    if (!selectedMealPackage.is_variant_selected || !selectedMealPackage.variant) {
        if (selectedMealPackage.is_entire_stay) {
            return selectedMealPackage.price * nights * guest
        } else {
            return selectedMealPackage.price * selectedMealPackage.selected_days.length * guest
        }
    }


    if (selectedMealPackage.is_entire_stay) {
        return selectedMealPackage.variant.price * nights * guest
    } else {
        return selectedMealPackage.variant.price * selectedMealPackage.selected_days.length * guest
    }
}

export function calRoomOnlyTotal(room_daily_prices: RoomDailyPriceType[]) {
    return room_daily_prices.reduce((sum, date) => sum + date.price, 0);
}

export function calculateRoomPrice({ nights, adults, children, room_daily_prices, selectedMealPackage, selectedAddons }: RoomPrice) {

    const meal_total = calMealPackageTotal({ nights, adults, children, selectedMealPackage })

    const addons_total = calculateAddonTotal({ selectedAddons, adults, children, nights })
    const room_only_total = calRoomOnlyTotal(room_daily_prices)
    const grand_total = room_only_total + meal_total + addons_total;


    return {
        room_avg_price: room_only_total / nights,
        room_only_total,
        meal_total,
        addons_total,
        grand_total
    }

}