import { useCallback, useEffect, useState } from 'react';
import { Link } from 'react-router-dom';
import { ArrowLeft, ArrowUpCircle, CheckCircle2, Info, Loader2, Sparkles, AlertCircle } from 'lucide-react';
import { api } from '@/api';
import { useAuth } from '@/hooks/useAuth';
import { APP_VERSION } from '@/lib/version';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';

const REPOSITORY_URL = 'https://dream.scheller.ltd/null/BillTracker';

interface UpdateStatus {
  has_update?: boolean;
  latest_release_url?: string | null;
  latest_version?: string;
  up_to_date?: boolean | null;
  error?: string | null;
}

interface AboutInfo {
  version?: string;
  name?: string;
  description?: string;
  stack?: { backend?: string; database?: string };
}

function UpdateBadge({ status, loading }: { status: UpdateStatus | null; loading: boolean }) {
  if (loading) {
    return (
      <span className="inline-flex items-center gap-1 text-xs text-muted-foreground/60">
        <Loader2 className="h-3 w-3 animate-spin shrink-0" />
        Checking…
      </span>
    );
  }
  if (!status) return null;

  if (status.has_update) {
    return (
      <a
        href={status.latest_release_url || '#'}
        target="_blank"
        rel="noopener noreferrer"
        className="inline-flex items-center gap-1 text-xs font-medium text-amber-400 hover:text-amber-300 transition-colors"
      >
        <ArrowUpCircle className="h-3 w-3 shrink-0" />
        v{status.latest_version} available
      </a>
    );
  }

  if (status.up_to_date) {
    return (
      <span className="inline-flex items-center gap-1 text-xs text-emerald-500/80">
        <CheckCircle2 className="h-3 w-3 shrink-0" />
        Up to date
      </span>
    );
  }

  // up_to_date is null — check failed
  return (
    <span className="inline-flex items-center gap-1 text-xs text-muted-foreground/50" title={status.error || 'Update check failed'}>
      <AlertCircle className="h-3 w-3 shrink-0" />
      Could not check
    </span>
  );
}

export default function AboutPage() {
  const { user } = useAuth();

  const [about,         setAbout]         = useState<AboutInfo | null>(null);
  const [loading,       setLoading]       = useState(true);
  const [updateStatus,  setUpdateStatus]  = useState<UpdateStatus | null>(null);
  const [updateLoading, setUpdateLoading] = useState(true);

  const load = useCallback(async () => {
    setLoading(true);
    try {
      setAbout(await api.about() as AboutInfo);
    } finally {
      setLoading(false);
    }
  }, []);

  useEffect(() => { load(); }, [load]);

  useEffect(() => {
    setUpdateLoading(true);
    api.updateStatus()
      .then(d => setUpdateStatus(d as UpdateStatus))
      .catch(() => setUpdateStatus(null))
      .finally(() => setUpdateLoading(false));
  }, []);

  // Use Vite-injected APP_VERSION as the immediate source of truth.
  // api.about() version is shown once loaded as a cross-check; they should always match.
  const displayVersion = about?.version ?? APP_VERSION;

  return (
    <div className="min-h-screen bg-[radial-gradient(circle_at_top_left,oklch(var(--primary)/0.06),transparent_34rem),linear-gradient(180deg,oklch(var(--background)),oklch(var(--muted)/0.18))] px-4 py-8 text-foreground sm:px-6">
      <main className="mx-auto w-full max-w-3xl space-y-5">
        <Button asChild variant="ghost" size="sm" className="-ml-2">
          <Link to={user ? '/' : '/login'}>
            <ArrowLeft className="h-3.5 w-3.5" />
            {user ? 'Back to app' : 'Back'}
          </Link>
        </Button>

        <Card className="border-border/70 bg-card shadow-sm" id="about-card">
          <CardHeader>
            <div className="mb-2 flex h-10 w-10 items-center justify-center rounded-xl border border-border/70 bg-primary/10 text-primary">
              <Info className="h-5 w-5" />
            </div>
            <CardTitle className="text-2xl">{about?.name || 'BillTracker'}</CardTitle>
            <CardDescription>
              <span className="text-sm">{about?.description || ''}</span>
            </CardDescription>
          </CardHeader>
          <CardContent className="space-y-5">
            <div className="grid gap-3 sm:grid-cols-3">

              {/* Version card — shows immediately via APP_VERSION, update status alongside */}
              <div className="rounded-xl border border-border/70 bg-background/65 p-4">
                <p className="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Version</p>
                <p className="mt-1 font-mono text-lg font-bold">v{displayVersion}</p>
                <div className="mt-2">
                  <UpdateBadge status={updateStatus} loading={updateLoading} />
                </div>
              </div>

              <div className="rounded-xl border border-border/70 bg-background/65 p-4">
                <p className="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Backend</p>
                <p className="mt-1 text-sm font-semibold">{about?.stack?.backend || 'Node.js / Express'}</p>
              </div>
              <div className="rounded-xl border border-border/70 bg-background/65 p-4">
                <p className="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Storage</p>
                <p className="mt-1 text-sm font-semibold">{about?.stack?.database || 'SQLite'}</p>
              </div>
            </div>

            <div className="rounded-xl border border-border/70 bg-muted/35 p-4">
              <div className="flex items-start gap-3">
                <Sparkles className="mt-0.5 h-4 w-4 shrink-0 text-primary" />
                <div>
                  <p className="text-sm font-semibold">Produced with AI assistance</p>
                  <p className="mt-1 text-sm leading-6 text-muted-foreground">
                    BillTracker is self-hosted software for personal bill planning and history. This product was produced with the assistance of AI.
                  </p>
                </div>
              </div>
            </div>

            <div className="flex flex-col gap-2 sm:flex-row">
              <Button asChild>
                <Link to="/release-notes">Release Notes</Link>
              </Button>
              <Button asChild variant="outline">
                <a href={REPOSITORY_URL} target="_blank" rel="noopener noreferrer">Repository</a>
              </Button>
              <Button asChild variant="outline">
                <Link to="/privacy">Privacy</Link>
              </Button>
              {user == null && (
                <Button asChild variant="outline">
                  <Link to="/login">Sign In</Link>
                </Button>
              )}
            </div>
          </CardContent>
        </Card>
      </main>
    </div>
  );
}
