import { Form, Head } from '@inertiajs/react';
import { useState } from 'react';
import { CreditCard, HandCoins } from 'lucide-react';
import { PromoCodeField } from '@/components/booking/promo-code-field';
import InputError from '@/components/input-error';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { Button } from '@/components/ui/button';
import { Separator } from '@/components/ui/separator';
import { cn } from '@/lib/utils';

type Props = {
    subtotal: number;
    addonsTotal: number;
    discountTotal: number;
    discountLabel: string | null;
    total: number;
    stripeEnabled: boolean;
};

type PaymentMethod = 'card' | 'pay_at_service';

export default function Step7Payment({
    subtotal,
    addonsTotal,
    discountTotal,
    discountLabel,
    total,
    stripeEnabled,
}: Props) {
    const defaultMethod: PaymentMethod = stripeEnabled ? 'card' : 'pay_at_service';
    const [paymentMethod, setPaymentMethod] = useState<PaymentMethod>(defaultMethod);
    const [liveDiscountTotal, setLiveDiscountTotal] = useState(discountTotal);
    const [liveDiscountLabel, setLiveDiscountLabel] = useState(discountLabel);
    const liveTotal = Math.max(0, total - discountTotal + liveDiscountTotal);

    return (
        <>
            <Head title="Book: payment" />

            <div className="flex flex-col gap-6">
                <div>
                    <h2 className="text-xl font-semibold tracking-tight">Payment</h2>
                    <p className="mt-1 text-sm text-muted-foreground">
                        Choose how you'd like to pay for your service.
                    </p>
                </div>

                    {!stripeEnabled && (
                        <Alert className="mt-4">
                            <AlertDescription>
                                Online card payments are not configured. You can book now and pay in person when your service is complete.
                            </AlertDescription>
                        </Alert>
                    )}

                    <PromoCodeField
                        appliedLabel={discountLabel}
                        onApplied={(result) => {
                            setLiveDiscountTotal(result.discountTotal);
                            setLiveDiscountLabel(result.discountLabel);
                        }}
                    />

                    <div className="rounded-2xl border bg-muted/30 p-4 text-sm">
                        <div className="flex items-center justify-between">
                            <span className="text-muted-foreground">Subtotal</span>
                            <span>${(subtotal / 100).toFixed(2)}</span>
                        </div>
                        {addonsTotal > 0 && (
                            <div className="mt-1.5 flex items-center justify-between">
                                <span className="text-muted-foreground">Add-ons</span>
                                <span>+${(addonsTotal / 100).toFixed(2)}</span>
                            </div>
                        )}
                        {liveDiscountTotal > 0 && (
                            <div className="mt-1.5 flex items-center justify-between">
                                <span className="text-muted-foreground">
                                    {liveDiscountLabel ?? 'Discount'}
                                </span>
                                <span className="text-green-600 dark:text-green-500">
                                    -${(liveDiscountTotal / 100).toFixed(2)}
                                </span>
                            </div>
                        )}
                        <Separator className="my-2" />
                        <div className="flex items-center justify-between font-semibold">
                            <span>Total</span>
                            <span>${(liveTotal / 100).toFixed(2)}</span>
                        </div>
                    </div>

                    <Form action="/book/payment" method="post">
                        {({ errors, processing }) => (
                            <div className="mt-6 flex flex-col gap-5">
                                {/* Payment method selection */}
                                <div>
                                    <p className="mb-3 text-sm font-medium">Payment method</p>
                                    <div className="flex flex-col gap-2">
                                        {stripeEnabled && (
                                            <label
                                                className={cn(
                                                    'flex cursor-pointer items-center gap-4 rounded-2xl border-2 p-4 transition-all',
                                                    paymentMethod === 'card'
                                                        ? 'border-primary bg-primary/5'
                                                        : 'border-border hover:border-primary/40 hover:bg-accent/40',
                                                )}
                                            >
                                                <input
                                                    type="radio"
                                                    name="payment_method"
                                                    value="card"
                                                    checked={paymentMethod === 'card'}
                                                    onChange={() => setPaymentMethod('card')}
                                                    className="sr-only"
                                                />
                                                <div
                                                    className={cn(
                                                        'flex size-10 shrink-0 items-center justify-center rounded-xl',
                                                        paymentMethod === 'card'
                                                            ? 'bg-primary/10 text-primary'
                                                            : 'bg-muted text-muted-foreground',
                                                    )}
                                                >
                                                    <CreditCard className="size-5" />
                                                </div>
                                                <div>
                                                    <p className="font-semibold">Pay now with card</p>
                                                    <p className="text-sm text-muted-foreground">
                                                        Secured by Stripe
                                                    </p>
                                                </div>
                                            </label>
                                        )}

                                        <label
                                            className={cn(
                                                'flex cursor-pointer items-center gap-4 rounded-2xl border-2 p-4 transition-all',
                                                paymentMethod === 'pay_at_service'
                                                    ? 'border-primary bg-primary/5'
                                                    : 'border-border hover:border-primary/40 hover:bg-accent/40',
                                            )}
                                        >
                                            <input
                                                type="radio"
                                                name="payment_method"
                                                value="pay_at_service"
                                                checked={paymentMethod === 'pay_at_service'}
                                                onChange={() => setPaymentMethod('pay_at_service')}
                                                className="sr-only"
                                            />
                                            <div
                                                className={cn(
                                                    'flex size-10 shrink-0 items-center justify-center rounded-xl',
                                                    paymentMethod === 'pay_at_service'
                                                        ? 'bg-primary/10 text-primary'
                                                        : 'bg-muted text-muted-foreground',
                                                )}
                                            >
                                                <HandCoins className="size-5" />
                                            </div>
                                            <div>
                                                <p className="font-semibold">Pay at service</p>
                                                <p className="text-sm text-muted-foreground">
                                                    Pay in person when complete
                                                </p>
                                            </div>
                                        </label>
                                    </div>
                                    <InputError message={errors.payment_method} />
                                </div>

                                <input
                                    type="hidden"
                                    name="pay_full_amount"
                                    value={paymentMethod === 'card' ? '1' : '0'}
                                />
                                <InputError message={errors.pay_full_amount} />

                                <Button type="submit" disabled={processing} size="lg" className="w-full">
                                    {paymentMethod === 'card' ? 'Continue to payment →' : 'Confirm booking'}
                                </Button>

                                <p className="text-center text-xs text-muted-foreground">
                                    By confirming, you agree to our service terms.
                                </p>
                            </div>
                        )}
                </Form>
            </div>
        </>
    );
}
