import { useState } from 'react';

import { Head } from '@inertiajs/react';
import { router } from '@inertiajs/react';

import { PageProps } from '@/types';

interface UnsubscribeConfirmationProps extends PageProps {
    notification_type: string;
    confirmed: boolean;
    site_name?: string;
    confirm_url?: string;
}

const TYPE_LABELS: Record<string, string> = {
    lifecycle_onboarding: 'onboarding tips and guides',
    lifecycle_reengagement: 're-engagement updates',
    lifecycle_milestones: 'milestone notifications',
    lifecycle_conversion: 'upgrade and billing tips',
    lifecycle_feature_discovery: 'feature discovery emails',
    lifecycle_activation: 'activation tips',
    lifecycle_retention: 'retention updates',
    lifecycle_alerts: 'alert notifications',
    lifecycle_quick_wins: 'quick win recommendations',
    analysis_complete: 'analysis completion notifications',
    traffic_alert: 'traffic alert notifications',
    new_recommendations: 'new recommendation notifications',
};

export default function UnsubscribeConfirmation({
    notification_type,
    confirmed,
    site_name,
    confirm_url,
}: UnsubscribeConfirmationProps) {
    const label = TYPE_LABELS[notification_type] ?? notification_type.replace(/_/g, ' ');
    const [isSubmitting, setIsSubmitting] = useState(false);

    const handleConfirm = () => {
        if (confirm_url) {
            setIsSubmitting(true);
            router.post(confirm_url, {}, {
                preserveScroll: true,
                onFinish: () => setIsSubmitting(false),
            });
        }
    };

    return (
        <>
            <Head title={confirmed ? 'Unsubscribed' : 'Confirm Unsubscribe'} />
            <div className="min-h-screen bg-background flex items-center justify-center p-4">
                <div className="max-w-md w-full bg-card rounded-lg border border-border p-8 text-center space-y-6">
                    {confirmed ? (
                        <>
                            <div className="text-4xl">&#10003;</div>
                            <h1 className="text-xl font-semibold text-foreground">
                                You&apos;ve been unsubscribed
                            </h1>
                            <p className="text-muted-foreground">
                                You will no longer receive {label} emails
                                {site_name ? ` for ${site_name}` : ''}.
                            </p>
                            <a
                                href="/settings/notifications"
                                className="text-sm text-primary hover:underline"
                            >
                                Manage all email preferences
                            </a>
                        </>
                    ) : (
                        <>
                            <h1 className="text-xl font-semibold text-foreground">
                                Confirm unsubscribe
                            </h1>
                            <p className="text-muted-foreground">
                                You are about to unsubscribe from{' '}
                                <span className="font-medium text-foreground">{label}</span> emails
                                {site_name ? ` for ${site_name}` : ''}.
                            </p>
                            <button
                                type="button"
                                onClick={handleConfirm}
                                disabled={isSubmitting}
                                className="w-full rounded-md bg-destructive px-4 py-2 text-sm font-medium text-destructive-foreground hover:bg-destructive/90 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
                            >
                                {isSubmitting ? 'Working…' : 'Yes, unsubscribe me'}
                            </button>
                            <a
                                href="/"
                                className="block text-sm text-muted-foreground hover:text-foreground"
                            >
                                Cancel, keep receiving these emails
                            </a>
                        </>
                    )}
                </div>
            </div>
        </>
    );
}
