import { BarChart3, Clock, FileText, Lock, TrendingUp } from 'lucide-react';

import { Head, useForm, usePage } from '@inertiajs/react';

import { UpgradePrompt } from '@/Components/billing/UpgradePrompt';
import { Input } from '@/Components/ui/input';
import { Label } from '@/Components/ui/label';
import { LoadingButton } from '@/Components/ui/loading-button';
import { useUnsavedChanges } from '@/hooks/useUnsavedChanges';
import DashboardLayout from '@/Layouts/DashboardLayout';
import { trackEvent } from '@/lib/analytics';
import { SITE_CREATED } from '@/lib/event-catalog';
import type { PageProps } from '@/types';

const BENEFITS = [
  {
    icon: TrendingUp,
    title: "Find what's losing traffic",
    description: 'See exactly which pages dropped and why, with before/after comparisons.',
  },
  {
    icon: BarChart3,
    title: 'Get actionable recommendations',
    description: 'Rule-based recommendations ranked by impact — no guesswork needed.',
  },
  {
    icon: FileText,
    title: 'AI Drafts Included',
    description: 'Turn recommendations into ready-to-publish AI drafts. Included on every plan — no setup needed.',
  },
];

const TRUST_SIGNALS = [
  { icon: Lock, text: 'Read-only GSC access — we never modify your data' },
  { icon: Clock, text: 'First insights in under 10 minutes' },
];

export default function SitesCreate() {
  const { limits } = usePage<PageProps>().props;
  const { data, setData, post, processing, errors, isDirty } = useForm({
    name: '',
    domain: '',
  });

  useUnsavedChanges(isDirty);

  const submit = (e: React.FormEvent) => {
    e.preventDefault();
    post(route('sites.store'), {
      onSuccess: () => {
        trackEvent(SITE_CREATED);
      },
    });
  };

  const atSitesLimit =
    limits != null &&
    limits.max_sites_per_user > 0 &&
    limits.sites_used_count >= limits.max_sites_per_user;

  return (
    <>
      <Head title="Create Site" />

      <div className="container py-8 max-w-4xl">
        {atSitesLimit && (
          <div className="mb-6">
            <UpgradePrompt
              limitType="sites"
              currentUsage={limits!.sites_used_count}
              maxUsage={limits!.max_sites_per_user}
              proLimit={limits!.pro_sites}
              customMessage="Manage up to 5 sites with Pro — track all your properties"
              ctaLabel="Unlock More Sites"
            />
          </div>
        )}

        <div className="grid gap-10 lg:grid-cols-2 lg:gap-16 items-start">
          {/* Left: value proposition */}
          <div>
            <h1 className="text-2xl font-bold tracking-tight">Connect your WordPress site</h1>
            <p className="text-muted-foreground mt-2">
              Enter your site details to start diagnosing SEO issues and generating AI-powered
              content fixes.
            </p>

            <ul className="mt-8 space-y-5">
              {BENEFITS.map(({ icon: Icon, title, description }) => (
                <li key={title} className="flex gap-3">
                  <div className="mt-0.5 flex h-8 w-8 shrink-0 items-center justify-center rounded-md bg-primary/10">
                    <Icon className="h-4 w-4 text-primary" aria-hidden />
                  </div>
                  <div>
                    <p className="text-sm font-medium">{title}</p>
                    <p className="text-sm text-muted-foreground">{description}</p>
                  </div>
                </li>
              ))}
            </ul>

            <ul className="mt-8 space-y-2">
              {TRUST_SIGNALS.map(({ icon: Icon, text }) => (
                <li key={text} className="flex items-center gap-2 text-xs text-muted-foreground">
                  <Icon className="h-3.5 w-3.5 shrink-0" aria-hidden />
                  {text}
                </li>
              ))}
            </ul>
          </div>

          {/* Right: form */}
          <form onSubmit={submit} className="space-y-4 rounded-xl border bg-card p-6 shadow-sm">
            <div>
              <Label htmlFor="site-name">Site name</Label>
              <Input
                id="site-name"
                value={data.name}
                onChange={(e) => setData('name', e.target.value)}
                placeholder="My Website"
                className="mt-1"
                aria-invalid={!!errors.name}
                aria-describedby={errors.name ? 'site-name-error' : undefined}
              />
              {errors.name && (
                <p id="site-name-error" className="text-sm text-destructive mt-1">
                  {errors.name}
                </p>
              )}
            </div>
            <div>
              <Label htmlFor="site-domain">Domain</Label>
              <Input
                id="site-domain"
                value={data.domain}
                onChange={(e) => setData('domain', e.target.value)}
                placeholder="https://example.com"
                className="mt-1"
                aria-invalid={!!errors.domain}
                aria-describedby={errors.domain ? 'site-domain-error' : 'site-domain-hint'}
              />
              {errors.domain ? (
                <p id="site-domain-error" className="text-sm text-destructive mt-1">
                  {errors.domain}
                </p>
              ) : (
                <p id="site-domain-hint" className="text-xs text-muted-foreground mt-1">
                  Enter the full URL including https://, e.g. https://myblog.com
                </p>
              )}
            </div>
            <LoadingButton loading={processing} className="w-full">
              Connect site
            </LoadingButton>
          </form>
        </div>
      </div>
    </>
  );
}

SitesCreate.layout = (page: React.ReactNode) => <DashboardLayout>{page}</DashboardLayout>;
