"use client";

import { useState } from "react";
import { Card, CardContent } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import {
  Collapsible,
  CollapsibleContent,
  CollapsibleTrigger,
} from "@/components/ui/collapsible";
import { ChevronDown, ChevronUp, Receipt } from "lucide-react";
import { PACKAGE_DETAIL } from "@/lib/strings";
import { getAgeCategory, calculatePartyPricing } from "./package-detail-utils";

interface PartyPriceSummaryProps {
  pricePerPerson: number;
  adultsCount: number;
  childrenAges: number[];
  className?: string;
}

function formatCurrency(amount: number) {
  return `\u20B9${amount.toLocaleString("en-IN")}`;
}

function PriceBreakdown({
  pricePerPerson,
  adultsCount,
  childrenAges,
}: Omit<PartyPriceSummaryProps, "className">) {
  const pricing = calculatePartyPricing(
    pricePerPerson,
    adultsCount,
    childrenAges
  );

  return (
    <div className="space-y-3">
      {/* Adults */}
      <div className="flex items-center justify-between text-sm">
        <span className="text-gray-600">
          {PACKAGE_DETAIL.ADULTS_LABEL} &times; {pricing.adultsCount}
        </span>
        <span className="font-medium text-gray-900">
          {formatCurrency(pricing.adultTotal)}
        </span>
      </div>

      {/* Children (3–11, 50% charge) */}
      {pricing.childrenCount > 0 && (
        <div className="flex items-center justify-between text-sm">
          <div className="flex items-center gap-2">
            <span className="text-gray-600">
              {PACKAGE_DETAIL.CHILDREN_LABEL} &times; {pricing.childrenCount}
            </span>
            <Badge variant="softWarning" size="sm">
              50%
            </Badge>
          </div>
          <span className="font-medium text-gray-900">
            {formatCurrency(pricing.childTotal)}
          </span>
        </div>
      )}

      {/* Infants (0–2, free) */}
      {pricing.infantsCount > 0 && (
        <div className="flex items-center justify-between text-sm">
          <div className="flex items-center gap-2">
            <span className="text-gray-600">
              {PACKAGE_DETAIL.INFANTS_LABEL} &times; {pricing.infantsCount}
            </span>
            <Badge variant="softSuccess" size="sm">
              {PACKAGE_DETAIL.FREE}
            </Badge>
          </div>
          <span className="font-medium text-gray-900">{formatCurrency(0)}</span>
        </div>
      )}

      <div className="space-y-1 border-t border-gray-200 pt-3">
        <div className="flex items-center justify-between text-sm">
          <span className="text-gray-600">{PACKAGE_DETAIL.SUBTOTAL_LABEL}</span>
          <span className="font-medium text-gray-900">
            {formatCurrency(pricing.subtotal)}
          </span>
        </div>
        <div className="flex items-center justify-between text-sm">
          <span className="text-gray-600">{PACKAGE_DETAIL.GST_18}</span>
          <span className="font-medium text-gray-900">
            {formatCurrency(pricing.gst)}
          </span>
        </div>
      </div>

      <div className="border-t border-gray-200 pt-3">
        <div className="flex items-center justify-between">
          <span className="text-base font-semibold text-gray-900">
            {PACKAGE_DETAIL.TOTAL_PRICE}
          </span>
          <span className="text-lg font-bold text-primary">
            {formatCurrency(pricing.total)}
          </span>
        </div>
      </div>
    </div>
  );
}

/**
 * Live price summary based on party composition.
 * Desktop: sticky side card. Mobile: collapsible summary.
 */
export function PartyPriceSummary({
  pricePerPerson,
  adultsCount,
  childrenAges,
  className,
}: PartyPriceSummaryProps) {
  const [isMobileOpen, setIsMobileOpen] = useState(false);

  const pricing = calculatePartyPricing(
    pricePerPerson,
    adultsCount,
    childrenAges
  );

  return (
    <div className={className}>
      {/* Desktop: visible sticky card */}
      <Card className="hidden overflow-hidden rounded-xl border border-gray-200 bg-white shadow-sm md:block">
        <div className="border-b border-gray-100 px-4 py-3">
          <h3 className="flex items-center gap-2 text-lg font-semibold text-gray-900">
            <Receipt className="h-5 w-5 text-primary" />
            {PACKAGE_DETAIL.PRICE_BREAKDOWN}
          </h3>
        </div>
        <CardContent className="p-4">
          <PriceBreakdown
            pricePerPerson={pricePerPerson}
            adultsCount={adultsCount}
            childrenAges={childrenAges}
          />
        </CardContent>
      </Card>

      {/* Mobile: collapsible summary */}
      <div className="md:hidden">
        <Collapsible open={isMobileOpen} onOpenChange={setIsMobileOpen}>
          <CollapsibleTrigger asChild>
            <button
              type="button"
              className="flex w-full items-center justify-between rounded-xl border border-gray-200 bg-white px-4 py-3 shadow-sm"
            >
              <div className="flex items-center gap-2">
                <Receipt className="h-5 w-5 text-primary" />
                <span className="text-base font-semibold text-gray-900">
                  {PACKAGE_DETAIL.PRICE_BREAKDOWN}
                </span>
              </div>
              <div className="flex items-center gap-2">
                <span className="text-lg font-bold text-primary">
                  {formatCurrency(pricing.total)}
                </span>
                {isMobileOpen ? (
                  <ChevronUp className="h-4 w-4 text-gray-500" />
                ) : (
                  <ChevronDown className="h-4 w-4 text-gray-500" />
                )}
              </div>
            </button>
          </CollapsibleTrigger>
          <CollapsibleContent>
            <div className="mt-2 rounded-xl border border-gray-200 bg-white p-4 shadow-sm">
              <PriceBreakdown
                pricePerPerson={pricePerPerson}
                adultsCount={adultsCount}
                childrenAges={childrenAges}
              />
            </div>
          </CollapsibleContent>
        </Collapsible>
      </div>
    </div>
  );
}
