import { Lock } from 'lucide-react';

import { FormEventHandler, useState } from 'react';

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

import { Logo, TextLogo } from '@/Components/branding/Logo';
import { ThemeToggle } from '@/Components/theme';
import { Alert, AlertDescription } from '@/Components/ui/alert';
import { Input } from '@/Components/ui/input';
import { Label } from '@/Components/ui/label';
import { LoadingButton } from '@/Components/ui/loading-button';
import { trackEvent } from '@/lib/analytics';
import { CTA_CLICKED } from '@/lib/event-catalog';

interface Props {
  token: string;
  error?: string;
}

export default function PasswordRequired({ token, error }: Props) {
  const [password, setPassword] = useState('');
  const [processing, setProcessing] = useState(false);

  const submit: FormEventHandler = (e) => {
    e.preventDefault();
    setProcessing(true);

    router.post(
      `/shared-reports/${token}/verify-password`,
      { password },
      {
        onFinish: () => setProcessing(false),
      },
    );
  };

  return (
    <>
      <Head title="Password Protected Report" />

      <div className="min-h-screen bg-background">
        {/* Header */}
        <header className="border-b bg-card">
          <div className="container mx-auto px-4 py-4 flex items-center justify-between">
            <div className="flex items-center gap-3">
              <Logo className="h-8 w-8" />
              <TextLogo className="font-bold text-lg" />
            </div>
            <ThemeToggle />
          </div>
        </header>

        {/* Content */}
        <main className="container mx-auto px-4 py-16 max-w-md">
          <div className="space-y-6">
            <div className="text-center space-y-2">
              <div className="mx-auto w-12 h-12 rounded-full bg-primary/10 flex items-center justify-center mb-4">
                <Lock className="h-6 w-6 text-primary" />
              </div>
              <h1 className="text-2xl font-bold">Password Protected Report</h1>
              <p className="text-muted-foreground">
                This report is protected. Please enter the password to view it.
              </p>
            </div>

            {error && (
              <Alert variant="destructive">
                <AlertDescription>{error}</AlertDescription>
              </Alert>
            )}

            <form onSubmit={submit} className="space-y-4">
              <div className="space-y-2">
                <Label htmlFor="password">Password</Label>
                <Input
                  id="password"
                  type="password"
                  value={password}
                  onChange={(e) => setPassword(e.target.value)}
                  placeholder="Enter password"
                  required
                  autoFocus
                  disabled={processing}
                />
              </div>

              <LoadingButton type="submit" className="w-full" loading={processing}>
                View Report
              </LoadingButton>
            </form>

            <div className="text-center">
              <p className="text-sm text-muted-foreground">
                Don't have access?{' '}
                <Link href="/" className="font-medium text-primary hover:underline" onClick={() => trackEvent(CTA_CLICKED, { cta_text: 'Try RankWiz Free', cta_location: 'shared_report_password_gate' })}>
                  Try RankWiz Free
                </Link>
              </p>
            </div>
          </div>
        </main>

        {/* Footer */}
        <footer className="border-t bg-card mt-12">
          <div className="container mx-auto px-4 py-8 text-center">
            <p className="text-xs text-muted-foreground">
              Powered by{' '}
              <Link href="/" className="font-semibold text-foreground hover:underline" onClick={() => trackEvent(CTA_CLICKED, { cta_text: 'RankWiz AI', cta_location: 'shared_report_password_footer' })}>
                RankWiz AI
              </Link>
            </p>
          </div>
        </footer>
      </div>
    </>
  );
}
