import { BookingPriceAdjustmentType } from "@/types/dashboard/price-adjustment";
import { BookingPriceAdjustment, CheckoutBookingInput, MealPackageSelected, RoomDailyPriceType, RoomSelected } from "@/types/home/bookings";
import dayjs from "dayjs";
import isSameOrAfter from "dayjs/plugin/isSameOrAfter";
import isSameOrBefore from "dayjs/plugin/isSameOrBefore";

dayjs.extend(isSameOrAfter);
dayjs.extend(isSameOrBefore);

// This have use cart, summery page

export function getNights(checkIn: string, checkOut: string): number {
    const checkInDate = new Date(checkIn)
    const checkOutDate = new Date(checkOut)

    const diffTime = checkOutDate.getTime() - checkInDate.getTime()

    return Math.ceil(diffTime / (1000 * 60 * 60 * 24))
}

export function calAdultTotal(rooms: RoomSelected[]) {
    return rooms.reduce((sum, room) => sum + room.adults, 0);
}
export function calChildrenTotal(rooms: RoomSelected[]) {
    return rooms.reduce((sum, room) => sum + room.children, 0);
}


export function calMealPackageTotal(mealPackage: MealPackageSelected | null, nights: number, guest: number) {

    if (!mealPackage) return 0


    let price = mealPackage.price

    if (mealPackage.is_variant_selected && mealPackage.variant) {
        price = mealPackage.variant.price
    }

    if (mealPackage.is_entire_stay) return price * nights * guest

    return price * mealPackage.selected_days.length * guest

}

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

export function calRoomTotal(room: RoomSelected, nights: number) {

    const mealPackageTotal = calMealPackageTotal(room.meal_package, nights, (room.adults + room.children))

    const grand_total = calRoomOnlyTotal(room.room_daily_prices) + mealPackageTotal

    return grand_total
}


export function calBookingSubTotal(booking: CheckoutBookingInput) {

    const subTotal = booking.rooms.reduce((sum, room) => sum + calRoomTotal(room, booking.nights), 0);

    return subTotal

}

export function calPriceAdjustment(adjustment: BookingPriceAdjustment, subTotal: number): number {

    if (subTotal === 0) return 0

    let amount = 0;

    if (adjustment.value_type === "fixed") {
        amount = Number(adjustment.value);
    } else {
        amount = (subTotal * Number(adjustment.value)) / 100;
    }

    if (adjustment.direction === "add") {
        return amount;
    }

    return -amount;

}

export function filterPriceAdjustment(adjustments: BookingPriceAdjustmentType[], rooms = 0, nights = 0, subTotal = 0): BookingPriceAdjustmentType[] {

    const today = dayjs();

    return adjustments.filter((item) => {
        // is active
        if (!item.is_active) return false;

        // 📅 date rules
        const startOk =
            !item.start_date || dayjs(item.start_date).isSameOrBefore(today, "day");

        const endOk =
            !item.end_date || dayjs(item.end_date).isSameOrAfter(today, "day");

        if (!startOk || !endOk) return false;

        // 🏨 rooms
        if (rooms > 0) {
            if (item.min_rooms && item.min_rooms > rooms) return false;
            if (item.max_rooms && item.max_rooms < rooms) return false;
        }

        // 🌙 nights
        if (nights > 0) {
            if (item.min_nights && item.min_nights > nights) return false;
            if (item.max_nights && item.max_nights < nights) return false;
        }

        // 💰 total
        if (subTotal > 0) {
            if (item.min_total && item.min_total > subTotal) return false;
            if (item.max_total && item.max_total < subTotal) return false;
        }

        return true;
    });


}

export function calBookingTotal(booking: CheckoutBookingInput) {

    const subTotal = calBookingSubTotal(booking)

    if (subTotal === 0) return 0

    let total = subTotal;

    booking.price_data.forEach(adj => {
        total += calPriceAdjustment(adj, subTotal);
    });

    return total

}


export function calBooking(booking: CheckoutBookingInput) {

    return {
        ...booking,
        total_price: calBookingTotal(booking)
    }
}