'use client';
import React, { useEffect, useMemo, useState } from 'react';
import PageLayout from '../../components/PageLayout';
import { useRouter } from 'next/navigation';
import { useActiveCoffees } from '../hooks/getActiveCoffees';
import { getStandardVatRate, getVatRates } from './hooks/getTaxRate';
import { subscribeAbo } from './hooks/subscribeAbo';
import useAuthStore from '../../store/authStore'

export default function SummaryPage() {
  const router = useRouter();
  const { coffees, loading, error } = useActiveCoffees();
  const user = useAuthStore(state => state.user)
  const [selections, setSelections] = useState<Record<string, number>>({});
  const [selectedPlanCapsules, setSelectedPlanCapsules] = useState<60 | 120>(120);
  const [isForSelf, setIsForSelf] = useState(true);
  const [form, setForm] = useState({
    firstName: '',
    lastName: '',
    email: '',
    street: '',
    postalCode: '',
    city: '',
    country: 'DE',
    frequency: 'monatlich',
    startDate: '',
    recipientEmail: '',
    recipientName: '',
    recipientNotes: '',
  });
  const [showThanks, setShowThanks] = useState(false);
  const [confetti, setConfetti] = useState<{ left: number; delay: number; color: string }[]>([]);
  const [taxRate, setTaxRate] = useState(0.07); // minimal fallback only
  const [vatRates, setVatRates] = useState<{ code: string; rate: number | null }[]>([]);
  const [submitError, setSubmitError] = useState<string | null>(null);
  const [submitLoading, setSubmitLoading] = useState(false);
  const COLORS = ['#1C2B4A', '#233357', '#2A3B66', '#314475', '#3A4F88', '#5B6C9A']; // dark blue palette

  useEffect(() => {
    try {
      const raw = sessionStorage.getItem('coffeeSelections');
      if (raw) setSelections(JSON.parse(raw));
      const rawPlan = sessionStorage.getItem('coffeeAboSizeCapsules');
      if (rawPlan === '60' || rawPlan === '120') {
        setSelectedPlanCapsules(Number(rawPlan) as 60 | 120);
      }
    } catch {}
  }, []);

  useEffect(() => {
    if (!showThanks) return;
    const items = Array.from({ length: 40 }).map(() => ({
      left: Math.random() * 100,
      delay: Math.random() * 0.6,
      color: COLORS[Math.floor(Math.random() * COLORS.length)],
    }));
    setConfetti(items);
  }, [showThanks]);

  const selectedEntries = useMemo(
    () =>
      Object.entries(selections)
        .map(([id, qty]) => {
          const coffee = coffees.find(c => c.id === id);
          return coffee ? { coffee, quantity: qty } : null;
        })
        .filter(Boolean) as { coffee: ReturnType<typeof useActiveCoffees>['coffees'][number]; quantity: number }[],
    [selections, coffees]
  );

  // NEW: computed packs/capsules for validation
  const totalCapsules = useMemo(
    () => selectedEntries.reduce((sum, e) => sum + e.quantity, 0),
    [selectedEntries]
  )
  const totalPacks = totalCapsules / 10
  const requiredPacks = selectedPlanCapsules / 10

  // NEW: capture logged-in user id for referral
  const rawUserId = user?.id
  const currentUserId = typeof rawUserId === 'number'
    ? rawUserId
    : (typeof rawUserId === 'string' && /^\d+$/.test(rawUserId) ? Number(rawUserId) : undefined)
  console.info('[SummaryPage] currentUserId:', currentUserId)

  // Countries list from backend VAT rates (fallback to current country if list empty)
  const countryOptions = useMemo(() => {
    const currentCode = (form.country || 'DE').toUpperCase();
    const opts = vatRates.length > 0 ? vatRates.map(r => r.code) : [currentCode]
    if (!opts.includes(currentCode)) opts.unshift(currentCode)
    console.info('[SummaryPage] countryOptions:', opts)
    return opts
  }, [vatRates, form.country]);

  // Load VAT rates list from backend and set initial taxRate
  useEffect(() => {
    let active = true;
    (async () => {
      console.info('[SummaryPage] Loading vat rates (mount). country:', form.country)
      const list = await getVatRates();
      if (!active) return;
      console.info('[SummaryPage] getVatRates result count:', list.length)
      setVatRates(list);
      const upper = form.country.toUpperCase();
      const match = list.find(r => r.code === upper);
      if (match?.rate != null) {
        console.info('[SummaryPage] Initial taxRate from list:', match.rate, 'country:', upper)
        setTaxRate(match.rate);
      } else {
        const rate = await getStandardVatRate(form.country);
        console.info('[SummaryPage] Fallback taxRate via getStandardVatRate:', rate, 'country:', upper)
        setTaxRate(rate ?? 0.07);
      }
    })();
    return () => { active = false; };
  }, []); // mount-only

  // Update taxRate when country changes (from backend only)
  useEffect(() => {
    let active = true;
    (async () => {
      const upper = form.country.toUpperCase();
      console.info('[SummaryPage] Country changed:', upper)
      const fromList = vatRates.find(r => r.code === upper)?.rate;
      if (fromList != null) {
        console.info('[SummaryPage] taxRate from existing list:', fromList)
        if (active) setTaxRate(fromList);
        return;
      }
      const rate = await getStandardVatRate(form.country);
      console.info('[SummaryPage] taxRate via getStandardVatRate:', rate)
      if (active) setTaxRate(rate ?? 0.07);
    })();
    return () => { active = false; };
  }, [form.country, vatRates]);

  const totalPrice = useMemo(
    () => selectedEntries.reduce((sum, e) => sum + (e.quantity / 10) * e.coffee.pricePer10, 0),
    [selectedEntries]
  );
  const taxAmount = useMemo(() => totalPrice * taxRate, [totalPrice, taxRate]);
  const totalWithTax = useMemo(() => totalPrice + taxAmount, [totalPrice, taxRate, taxAmount]);

  const handleInput = (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => {
    const { name, value } = e.target;
    setForm(prev => ({ ...prev, [name]: value }));
  };

  const handleRecipientNotes = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
    const { name, value } = e.target;
    setForm(prev => ({ ...prev, [name]: value }));
  };

  const fillFromLoggedInData = () => {
    if (!user) {
      setSubmitError('No logged-in user data found to fill the fields.');
      return;
    }

    const pick = (...values: any[]) => {
      for (const value of values) {
        if (typeof value === 'string' && value.trim() !== '') return value.trim();
      }
      return '';
    };

    setSubmitError(null);
    setForm(prev => ({
      ...prev,
      firstName: pick(user.firstName, user.firstname, user.givenName, user.first_name) || prev.firstName,
      lastName: pick(user.lastName, user.lastname, user.familyName, user.last_name) || prev.lastName,
      email: pick(user.email, user.mail) || prev.email,
      street: pick(user.street, user.addressStreet, user.address?.street, user.address_line_1) || prev.street,
      postalCode: pick(user.postalCode, user.zipCode, user.zip, user.addressPostalCode, user.address?.postalCode) || prev.postalCode,
      city: pick(user.city, user.addressCity, user.town, user.address?.city) || prev.city,
      country: (pick(user.country, user.countryCode, user.addressCountry, user.address?.country) || prev.country).toUpperCase(),
    }));
  };

  const requiredSelfFields: Array<keyof typeof form> = [
    'firstName',
    'lastName',
    'email',
    'street',
    'postalCode',
    'city',
    'country',
    'frequency',
  ]

  const hasRequiredSelfFields = requiredSelfFields.every(k => form[k].trim() !== '')
  const hasRequiredGiftFields = isForSelf || form.recipientEmail.trim() !== ''

  const canSubmit =
    selectedEntries.length > 0 &&
    totalPacks === requiredPacks &&
    hasRequiredSelfFields &&
    hasRequiredGiftFields;

  const backToSelection = () => router.push('/coffee-abonnements');

  const submit = async () => {
    if (!canSubmit || submitLoading) return
    // NEW: guard (defensive) — backend requires selected package size
    if (totalPacks !== requiredPacks) {
      setSubmitError(`Order must contain exactly ${requiredPacks} packs (${selectedPlanCapsules} capsules).`)
      return
    }
    if (!isForSelf && !form.recipientEmail.trim()) {
      setSubmitError('Recipient email is required when the subscription is for someone else.')
      return
    }

    setSubmitError(null)
    setSubmitLoading(true)
    try {
      const payload = {
        items: selectedEntries.map(entry => ({
          coffeeId: entry.coffee.id,
          quantity: Math.round(entry.quantity / 10), // packs
        })),
        billing_interval: 'month',
        interval_count: 1,
        is_auto_renew: true,
        is_for_self: isForSelf,
        // NEW: pass customer fields
        firstName: form.firstName.trim(),
        lastName: form.lastName.trim(),
        email: form.email.trim(),
        street: form.street.trim(),
        postalCode: form.postalCode.trim(),
        city: form.city.trim(),
        country: form.country.trim(),
        frequency: form.frequency.trim(),
        startDate: form.startDate.trim() || undefined,
        recipient_email: isForSelf ? undefined : form.recipientEmail.trim(),
        recipient_name: isForSelf ? undefined : (form.recipientName.trim() || undefined),
        recipient_notes: isForSelf ? undefined : (form.recipientNotes.trim() || undefined),
        // NEW: always include referred_by if available
        referred_by: typeof currentUserId === 'number' ? currentUserId : undefined,
      }
      console.info('[SummaryPage] subscribeAbo payload:', payload)
      // NEW: explicit JSON preview to match request body
      console.info('[SummaryPage] subscribeAbo payload JSON:', JSON.stringify(payload))
      await subscribeAbo(payload)
      setShowThanks(true);
      try { sessionStorage.removeItem('coffeeSelections'); } catch {}
      try { sessionStorage.removeItem('coffeeAboSizeCapsules'); } catch {}
    } catch (e: any) {
      setSubmitError(e?.message || 'Subscription could not be created.');
    } finally {
      setSubmitLoading(false);
    }
  };

  return (
    <PageLayout>
      <div className="mx-auto max-w-6xl px-4 py-10 space-y-8 bg-gradient-to-b from-white to-[#1C2B4A0D]">
        <div className="flex items-center justify-between">
          <h1 className="text-3xl font-bold tracking-tight">
            <span className="text-[#1C2B4A]">Summary & Details</span>
          </h1>
          <button
            onClick={backToSelection}
            className="rounded-md border border-gray-300 px-3 py-2 text-sm hover:bg-gray-100"
          >
            Back to selection
          </button>
        </div>

        {/* Stepper */}
        <div className="flex items-center gap-3 text-sm text-gray-600">
          <div className="flex items-center opacity-60">
            <span className="h-8 w-8 rounded-full bg-gray-200 text-gray-600 flex items-center justify-center font-semibold">1</span>
            <span className="ml-2 font-medium">Selection</span>
          </div>
          <div className="h-px flex-1 bg-gray-200" />
          <div className="flex items-center">
            <span className="h-8 w-8 rounded-full bg-[#1C2B4A] text-white flex items-center justify-center font-semibold">2</span>
            <span className="ml-2 font-medium">Summary</span>
          </div>
        </div>

        {error && (
          <div className="rounded-xl border p-6 bg-white shadow-sm">
            <p className="text-sm text-red-700 mb-4">{error}</p>
            <button
              onClick={backToSelection}
              className="rounded-md bg-[#1C2B4A] text-white px-4 py-2 font-semibold hover:bg-[#1C2B4A]/90"
            >
              Back to selection
            </button>
          </div>
        )}

        {/* submit error */}
        {submitError && (
          <div className="rounded-xl border p-6 bg-white shadow-sm">
            <p className="text-sm text-red-700">{submitError}</p>
          </div>
        )}

        {loading ? (
          <div className="rounded-xl border p-6 bg-white shadow-sm">
            <div className="h-20 rounded-md bg-gray-100 animate-pulse" />
          </div>
        ) : selectedEntries.length === 0 ? (
          <div className="rounded-xl border p-6 bg-white shadow-sm">
            <p className="text-sm text-gray-600 mb-4">No selection found.</p>
            <button
              onClick={backToSelection}
              className="rounded-md bg-[#1C2B4A] text-white px-4 py-2 font-semibold hover:bg-[#1C2B4A]/90"
            >
              Back to selection
            </button>
          </div>
        ) : (
          <div className="grid gap-8 lg:grid-cols-3">
            {/* Left: Customer data */}
            <section className="lg:col-span-2">
              <h2 className="text-xl font-semibold mb-4">1. Your details</h2>
              <div className="rounded-xl border border-[#1C2B4A]/20 bg-white/80 backdrop-blur-sm p-6 shadow-lg">
                <button
                  type="button"
                  onClick={fillFromLoggedInData}
                  className="mb-4 w-full rounded-md border border-[#1C2B4A] px-3 py-2 text-sm font-medium text-[#1C2B4A] hover:bg-[#1C2B4A]/5"
                >
                  Fill fields with logged in data
                </button>
                <div className="mb-4 grid gap-3 sm:grid-cols-2">
                  <button
                    type="button"
                    onClick={() => setIsForSelf(true)}
                    className={`rounded-md border px-3 py-2 text-sm font-medium transition ${
                      isForSelf
                        ? 'border-[#1C2B4A] bg-[#1C2B4A]/5 text-[#1C2B4A]'
                        : 'border-gray-300 text-gray-700 hover:bg-gray-50'
                    }`}
                  >
                    For me
                  </button>
                  <button
                    type="button"
                    onClick={() => setIsForSelf(false)}
                    className={`rounded-md border px-3 py-2 text-sm font-medium transition ${
                      !isForSelf
                        ? 'border-[#1C2B4A] bg-[#1C2B4A]/5 text-[#1C2B4A]'
                        : 'border-gray-300 text-gray-700 hover:bg-gray-50'
                    }`}
                  >
                    For someone else
                  </button>
                </div>
                <div className="grid gap-4 sm:grid-cols-2">
                  {/* inputs translated */}
                  <div>
                    <label className="block text-sm font-medium mb-1">First name</label>
                    <input name="firstName" value={form.firstName} onChange={handleInput} className="w-full rounded border px-3 py-2 bg-white border-gray-300 focus:outline-none focus:ring-2 focus:ring-[#1C2B4A]" />
                  </div>
                  <div>
                    <label className="block text-sm font-medium mb-1">Last name</label>
                    <input name="lastName" value={form.lastName} onChange={handleInput} className="w-full rounded border px-3 py-2 bg-white border-gray-300 focus:outline-none focus:ring-2 focus:ring-[#1C2B4A]" />
                  </div>
                  <div className="sm:col-span-2">
                    <label className="block text-sm font-medium mb-1">Email</label>
                    <input type="email" name="email" value={form.email} onChange={handleInput} className="w-full rounded border px-3 py-2 bg-white border-gray-300 focus:outline-none focus:ring-2 focus:ring-[#1C2B4A]" />
                  </div>
                  <div className="sm:col-span-2">
                    <label className="block text-sm font-medium mb-1">Street & No.</label>
                    <input name="street" value={form.street} onChange={handleInput} className="w-full rounded border px-3 py-2 bg-white border-gray-300 focus:outline-none focus:ring-2 focus:ring-[#1C2B4A]" />
                  </div>
                  <div>
                    <label className="block text-sm font-medium mb-1">ZIP</label>
                    <input name="postalCode" value={form.postalCode} onChange={handleInput} className="w-full rounded border px-3 py-2 bg-white border-gray-300 focus:outline-none focus:ring-2 focus:ring-[#1C2B4A]" />
                  </div>
                  <div>
                    <label className="block text-sm font-medium mb-1">City</label>
                    <input name="city" value={form.city} onChange={handleInput} className="w-full rounded border px-3 py-2 bg-white border-gray-300 focus:outline-none focus:ring-2 focus:ring-[#1C2B4A]" />
                  </div>
                  <div>
                    <label className="block text-sm font-medium mb-1">Country</label>
                    <select name="country" value={form.country} onChange={handleInput} className="w-full rounded border px-3 py-2 bg-white border-gray-300 focus:outline-none focus:ring-2 focus:ring-[#1C2B4A]">
                      {countryOptions.map(code => (
                        <option key={code} value={code}>{code}</option>
                      ))}
                    </select>
                  </div>
                  <div>
                    <label className="block text-sm font-medium mb-1">Delivery interval</label>
                    <select name="frequency" value={form.frequency} onChange={handleInput} className="w-full rounded border px-3 py-2 bg-white border-gray-300 focus:outline-none focus:ring-2 focus:ring-[#1C2B4A]">
                      <option value="monatlich">Monthly</option>
                      <option value="zweimonatlich">Every 2 months</option>
                      <option value="vierteljährlich">Quarterly</option>
                    </select>
                  </div>
                  <div>
                    <label className="block text-sm font-medium mb-1">Start date (optional)</label>
                    <input type="date" name="startDate" value={form.startDate} onChange={handleInput} className="w-full rounded border px-3 py-2 bg-white border-gray-300 focus:outline-none focus:ring-2 focus:ring-[#1C2B4A]" />
                  </div>
                  {!isForSelf && (
                    <>
                      <div className="sm:col-span-2">
                        <label className="block text-sm font-medium mb-1">Recipient email</label>
                        <input
                          type="email"
                          name="recipientEmail"
                          value={form.recipientEmail}
                          onChange={handleInput}
                          className="w-full rounded border px-3 py-2 bg-white border-gray-300 focus:outline-none focus:ring-2 focus:ring-[#1C2B4A]"
                        />
                      </div>
                      <div className="sm:col-span-2">
                        <label className="block text-sm font-medium mb-1">Recipient name (optional)</label>
                        <input
                          name="recipientName"
                          value={form.recipientName}
                          onChange={handleInput}
                          className="w-full rounded border px-3 py-2 bg-white border-gray-300 focus:outline-none focus:ring-2 focus:ring-[#1C2B4A]"
                        />
                      </div>
                      <div className="sm:col-span-2">
                        <label className="block text-sm font-medium mb-1">Recipient note (optional)</label>
                        <textarea
                          name="recipientNotes"
                          value={form.recipientNotes}
                          onChange={handleRecipientNotes}
                          className="w-full rounded border px-3 py-2 bg-white border-gray-300 focus:outline-none focus:ring-2 focus:ring-[#1C2B4A]"
                          rows={3}
                        />
                      </div>
                    </>
                  )}
                </div>
                <button
                  onClick={submit}
                  disabled={!canSubmit || submitLoading}
                  className={`group w-full mt-6 rounded-lg px-4 py-3 font-semibold transition inline-flex items-center justify-center ${
                    canSubmit && !submitLoading ? 'bg-[#1C2B4A] text-white hover:bg-[#1C2B4A]/90 shadow-md hover:shadow-lg' : 'bg-gray-200 text-gray-600 cursor-not-allowed'
                  }`}
                >
                  {submitLoading ? 'Creating…' : 'Complete subscription'}
                  <svg className={`ml-2 h-5 w-5 transition-transform ${canSubmit ? 'group-hover:translate-x-0.5' : ''}`} viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
                    <path fillRule="evenodd" d="M10.293 3.293a1 1 0 011.414 0l5.999 6a1 1 0 010 1.414l-6 6a1 1 0 11-1.414-1.414L14.586 11H3a1 1 0 110-2h11.586l-4.293-4.293a1 1 0 010-1.414z" clipRule="evenodd" />
                  </svg>
                </button>
                {!canSubmit && (
                  <p className="text-xs text-gray-500 mt-2">
                    {isForSelf
                      ? 'Please select coffees and fill all required buyer fields.'
                      : 'Please select coffees and fill all required buyer fields plus recipient email.'}
                  </p>
                )}
              </div>
            </section>

            {/* Right: Order summary */}
            <section className="lg:col-span-1">
              <h2 className="text-xl font-semibold mb-4">2. Your selection</h2>
              <div className="rounded-xl border border-[#1C2B4A]/20 bg-white/80 backdrop-blur-sm p-6 shadow-lg lg:sticky lg:top-6">
                {selectedEntries.map(entry => (
                  <div key={entry.coffee.id} className="flex justify-between text-sm border-b last:border-b-0 pb-2 last:pb-0">
                    <div className="flex flex-col">
                      <span className="font-medium">{entry.coffee.name}</span>
                      <span className="text-xs text-gray-500">
                        {entry.quantity} pcs • <span className="inline-flex items-center font-semibold text-[#1C2B4A]">€{entry.coffee.pricePer10}/10</span>
                      </span>
                    </div>
                    <div className="text-right font-semibold">€{((entry.quantity / 10) * entry.coffee.pricePer10).toFixed(2)}</div>
                  </div>
                ))}
                <div className="flex justify-between pt-2 border-t">
                  <span className="text-sm font-semibold">Total (net)</span>
                  <span className="text-lg font-extrabold tracking-tight text-[#1C2B4A]">€{totalPrice.toFixed(2)}</span>
                </div>
                <div className="flex justify-between">
                  <span className="text-sm">Tax ({(taxRate * 100).toFixed(1)}%)</span>
                  <span className="text-sm font-medium">€{taxAmount.toFixed(2)}</span>
                </div>
                <div className="flex items-center justify-between">
                  <span className="text-sm font-semibold">Total incl. tax</span>
                  <span className="text-xl font-extrabold text-[#1C2B4A]">€{totalWithTax.toFixed(2)}</span>
                </div>
                {/* Validation summary (refined design) */}
                <div className="mt-2 text-xs text-gray-700">
                  Selected: {totalCapsules} capsules ({totalPacks} packs of 10). Target: {selectedPlanCapsules} capsules ({requiredPacks} packs).
                  {totalPacks !== requiredPacks && (
                    <span className="ml-2 inline-flex items-center rounded-md bg-red-50 text-red-700 px-2 py-1 border border-red-200">
                      Exactly {requiredPacks} packs ({selectedPlanCapsules} capsules) are required.
                    </span>
                  )}
                </div>
              </div>
            </section>
          </div>
        )}
      </div>

      {/* Thank you overlay */}
      {showThanks && (
        <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm">
          <div className="relative mx-4 w-full max-w-md rounded-2xl bg-white p-8 text-center shadow-2xl">
            <div className="pointer-events-none absolute inset-0 overflow-hidden">
              {confetti.map((c, i) => (
                <span key={i} className="confetti" style={{ left: `${c.left}%`, animationDelay: `${c.delay}s`, background: c.color }} />
              ))}
            </div>

            <div className="mx-auto mb-4 flex h-16 w-16 items-center justify-center rounded-full bg-[#1C2B4A]/10 text-[#1C2B4A] pop">
              <svg viewBox="0 0 24 24" className="h-9 w-9" fill="none" stroke="currentColor" strokeWidth="2">
                <path d="M20 6L9 17l-5-5" strokeLinecap="round" strokeLinejoin="round" />
              </svg>
            </div>
            <h3 className="text-2xl font-bold">Thanks for your subscription!</h3>
            <p className="mt-1 text-sm text-gray-600">
              {isForSelf ? 'Subscription created.' : 'Subscription created, invitation sent.'}
            </p>

            <div className="mt-6 grid gap-3 sm:grid-cols-2">
              <button onClick={() => { setShowThanks(false); backToSelection(); }} className="rounded-lg bg-[#1C2B4A] text-white px-4 py-2 font-semibold hover:bg-[#1C2B4A]/90">
                Back to selection
              </button>
              <button onClick={() => setShowThanks(false)} className="rounded-lg border border-gray-300 px-4 py-2 font-semibold hover:bg-gray-50">
                Close
              </button>
            </div>

            <style jsx>{`
              .confetti {
                position: absolute;
                top: -10%;
                width: 8px;
                height: 12px;
                border-radius: 2px;
                opacity: 0.9;
                animation: fall 1.8s linear forwards;
              }
              @keyframes fall {
                0%   { transform: translateY(0) rotate(0deg); }
                100% { transform: translateY(110vh) rotate(720deg); }
              }
              .pop {
                animation: pop 450ms ease-out forwards;
              }
              @keyframes pop {
                0%   { transform: scale(0.6); opacity: 0; }
                60%  { transform: scale(1.08); opacity: 1; }
                100% { transform: scale(1); }
              }
            `}</style>
          </div>
        </div>
      )}
    </PageLayout>
  );
}
