"use client";

import React from "react";
import { useSelector } from "react-redux";
import { RootState } from "@/redux/store";
import { formatConvertedPrice } from "@/utils/helpers";

type PriceSummaryProps = {
    data: any;
    type: "cart" | "order";
};

const PriceSummary: React.FC<PriceSummaryProps> = ({ data, type }) => {
    const { cartCount, deliveryCharge } = useSelector((state: RootState) => state.cart);
    const currency = useSelector((state: RootState) => state.currencies);

    const hasCouponDiscount = (data?.totalCouponAmount ?? 0) > 0;
    const hasProductDiscount = (data?.totalDiscountAmount ?? 0) > 0;

    const loyaltyDiscount = Number(data?.loyalty?.loyaltyAmount || 0);

    // Final total calculation
    const totalAmount =
        (data?.baseTotalAmount ?? 0) +
        Number(deliveryCharge) -
        loyaltyDiscount;

    const formatPrice = (val: number | undefined) =>
        formatConvertedPrice(val, currency.toCurrency, currency.value);

    return (
        <>
            {type === "cart" && (
                <div className="flex justify-between mb-2">
                    <span>Sub Total ({cartCount} items)</span>
                    <span className="font-semibold text-slate-900">{formatPrice(data?.totalProductOriginalPrice)}</span>
                </div>
            )}
            {type === "order" && (
                <div className="flex justify-between mb-2">
                    <span>Sub Total</span>
                    <span>{formatPrice(data?.totalProductOriginalPrice)}</span>
                </div>
            )}

            {hasCouponDiscount && (
                <div className="flex justify-between mb-2">
                    <span>Coupon Discount</span>
                    <span className="font-semibold text-green-600">{formatPrice(data?.totalCouponAmount)}</span>
                </div>
            )}

            {hasProductDiscount && (
                <div className="flex justify-between mb-2">
                    <span>{type === "cart" ? "Cart Discount" : "Discount"}</span>
                    <span className="font-semibold text-green-600">{formatPrice(data?.totalDiscountAmount)}</span>
                </div>
            )}

            {Number(deliveryCharge) > 0 && type == "cart" && (
                <div className="flex justify-between mb-2">
                    <span>Cash on delivery Charge</span>
                    <span className="font-semibold text-slate-900">{formatPrice(deliveryCharge ?? 0)}</span>
                </div>
            )}

            <div className="flex justify-between mb-2">
                <span>Shipping Charge</span>
                <span className="font-semibold text-slate-900">
                    {Number(data?.totalShippingAmount) === 0
                        ? "Free"
                        : formatPrice(data?.totalShippingAmount)}
                </span>
            </div>

            {data?.paymentMethodCharge > 0 && (
                <div className="flex justify-between mb-2">
                    <span>Payment Method Charge</span>
                    <span>{formatPrice(data?.paymentMethodCharge)}</span>
                </div>
            )}

            {data?.loyalty?.loyaltyAmount > 0 && (
                <div className="flex justify-between mb-2 py-1">
                    <span>Loyalty Discount</span>
                    <span className="font-semibold text-green-600">{formatPrice(data?.loyalty?.loyaltyAmount || 0)}</span>
                </div>
            )}

            {data?.totalTaxAmount > 0 && (
                <div className="flex justify-between mb-2">
                    <span>Total Tax Amount</span>
                    <span>{formatPrice(data?.totalTaxAmount)}</span>
                </div>
            )}

            <hr className="my-3 border-gray-200" />

            <div className="flex justify-between mb-2 text-sm font-semibold">
                <span>Total Amount <p>(Inclusive of Vat)</p></span>
                <span>{formatPrice(totalAmount)}</span>
            </div>
        </>
    );
};

export default PriceSummary;