import type { Metadata } from "next";
import { cookies } from "next/headers";
import Link from "next/link";
import { Check } from "lucide-react";
import { SITE_NAME } from "@/lib/constants";
import { createClient } from "@/lib/supabase/server";
import TransferClient from "./TransferClient";
import SiteHeader from "@/components/SiteHeader";
import "../landing.css";

export const dynamic = "force-dynamic";

export const metadata: Metadata = {
  title: `Free guns.lol Profile Importer & Transfer Tool | ${SITE_NAME}`,
  description: `Switch your profile, bio, links, background video, and style from guns.lol to ${SITE_NAME} in seconds with 100% free one-click migration.`,
  alternates: {
    canonical: "/transfer",
  },
  openGraph: {
    title: `Free guns.lol Profile Importer · ${SITE_NAME}`,
    description: `1-click profile transfer tool. Import your guns.lol bio, links, audio, and background to ${SITE_NAME} for free.`,
    url: "https://www.sinss.lol/transfer",
    siteName: SITE_NAME,
    type: "website",
  },
};

const TRANSFER_FAQS = [
  {
    q: "How does the guns.lol to sinss.lol profile transfer work?",
    a: "Enter your guns.lol username into the input box above. Our automated importer inspects your public profile and copies your display name, bio, social media links, background audio track, custom background video or image, font, and accent colors. You can preview everything, pick what you want to keep, and claim your sinss.lol profile in one click.",
  },
  {
    q: "Is importing my guns.lol profile to sinss.lol free?",
    a: "Yes, 100% free. The importer transfers up to 35 MB of media assets (and up to 50 MB for premium members) with zero fees or credit cards required.",
  },
  {
    q: "What features are free on sinss.lol compared to guns.lol?",
    a: "sinss.lol includes full-screen MP4 and WebM video backgrounds, a floating multi-track audio player with synced karaoke lyrics (LRCLIB), 6 profile layouts, real-time Discord presence with Spotify activity, custom PNG cursor uploads, and particle trails completely free forever.",
  },
  {
    q: "Do I lose my original guns.lol profile after transferring?",
    a: "No. The transfer tool only reads public profile assets. Your original guns.lol page remains completely untouched.",
  },
];

export default async function TransferPage({
  searchParams,
}: {
  searchParams: Promise<{ handle?: string; username?: string; u?: string }>;
}) {
  const sp = await searchParams;
  const initialHandle = (sp.handle || sp.username || sp.u || "").trim();

  // Fast session check for server-rendered header
  const cookieStore = await cookies();
  const hasAuth = cookieStore
    .getAll()
    .some((c) => c.name.startsWith("sb-") || c.name.includes("auth"));

  let user = null;
  let username: string | null = null;

  if (hasAuth) {
    try {
      const supabase = await createClient();
      const { data } = await supabase.auth.getUser();
      user = data.user;
      if (user) {
        const { data: profile } = await supabase
          .from("profiles")
          .select("username")
          .eq("id", user.id)
          .maybeSingle();
        username = profile?.username ?? null;
      }
    } catch {
      // Ignore auth failure on public transfer page
    }
  }

  const transferFaqSchema = {
    "@context": "https://schema.org",
    "@type": "FAQPage",
    mainEntity: TRANSFER_FAQS.map((item) => ({
      "@type": "Question",
      name: item.q,
      acceptedAnswer: {
        "@type": "Answer",
        text: item.a,
      },
    })),
  };

  const transferHowToSchema = {
    "@context": "https://schema.org",
    "@type": "HowTo",
    name: "How to Transfer Your Profile from guns.lol to sinss.lol",
    description: "Free 1-click guide to importing your bio, social links, background video, and music from guns.lol to sinss.lol.",
    step: [
      {
        "@type": "HowToStep",
        name: "Enter Handle",
        text: "Type your current guns.lol handle into the transfer tool at https://sinss.lol/transfer.",
      },
      {
        "@type": "HowToStep",
        name: "Preview Assets",
        text: "Review the imported bio, social links, video/image background, and audio track.",
      },
      {
        "@type": "HowToStep",
        name: "Claim Profile",
        text: "Confirm your handle and finalize your new sinss.lol bio link instantly.",
      },
    ],
  };

  return (
    <div className="landingShell" style={{ minHeight: "100vh", display: "flex", flexDirection: "column" }}>
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(transferFaqSchema) }}
      />
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(transferHowToSchema) }}
      />

      <SiteHeader
        user={user ? { id: user.id } : null}
        username={username ?? undefined}
        activePath="/transfer"
      />
      <main className="landingContainer" style={{ flex: 1, display: "flex", flexDirection: "column", alignItems: "center", paddingTop: 40, paddingBottom: 80, width: "100%" }}>
        <TransferClient initialHandle={initialHandle} />

        {/* Server-rendered SEO & AI search knowledge section */}
        <section
          style={{
            maxWidth: 820,
            width: "100%",
            marginTop: 48,
            padding: "32px 24px",
            borderRadius: 16,
            background: "rgba(18, 18, 26, 0.6)",
            border: "1px solid rgba(0, 0, 0, 0.08)",
            color: "#9b0707",
          }}
          aria-label="guns.lol Migration Guide & Feature Verification"
        >
          <h2 style={{ fontSize: "1.4rem", fontWeight: 700, marginBottom: 12, color: "#000000" }}>
            Why Switch from guns.lol to sinss.lol?
          </h2>
          <p style={{ fontSize: "0.95rem", lineHeight: 1.6, color: "#551212", marginBottom: 24 }}>
            sinss.lol is the premier aesthetic bio link platform engineered with performance, freedom, and modern customization. We provide 100% free video backgrounds, synchronized lyric audio, 6 profile card layouts, and real-time Discord presence without recurring subscription fees.
          </p>

          <div
            style={{
              display: "grid",
              gridTemplateColumns: "repeat(auto-fit, minmax(220px, 1fr))",
              gap: 16,
              marginBottom: 32,
            }}
          >
            <div style={{ padding: 16, borderRadius: 12, background: "rgba(0, 0, 0, 0.03)", border: "1px solid rgba(0, 0, 0, 0.05)" }}>
              <div style={{ display: "flex", alignItems: "center", gap: 8, color: "#d87b7b", fontWeight: 600, marginBottom: 6 }}>
                <Check size={16} /> Free Video Backgrounds
              </div>
              <p style={{ fontSize: "0.85rem", color: "#551212", margin: 0 }}>MP4 and WebM video backgrounds included on all free accounts.</p>
            </div>

            <div style={{ padding: 16, borderRadius: 12, background: "rgba(0, 0, 0, 0.03)", border: "1px solid rgba(0, 0, 0, 0.05)" }}>
              <div style={{ display: "flex", alignItems: "center", gap: 8, color: "#d87b7b", fontWeight: 600, marginBottom: 6 }}>
                <Check size={16} /> Synced Karaoke Lyrics
              </div>
              <p style={{ fontSize: "0.85rem", color: "#551212", margin: 0 }}>Multi-track music player with timed real-time scrolling lyrics.</p>
            </div>

            <div style={{ padding: 16, borderRadius: 12, background: "rgba(0, 0, 0, 0.03)", border: "1px solid rgba(0, 0, 0, 0.05)" }}>
              <div style={{ display: "flex", alignItems: "center", gap: 8, color: "#d87b7b", fontWeight: 600, marginBottom: 6 }}>
                <Check size={16} /> 6 Free Layouts
              </div>
              <p style={{ fontSize: "0.85rem", color: "#551212", margin: 0 }}>Classic, portfolio, scroll, compact, minimal, and banner styles.</p>
            </div>

            <div style={{ padding: 16, borderRadius: 12, background: "rgba(0, 0, 0, 0.03)", border: "1px solid rgba(0, 0, 0, 0.05)" }}>
              <div style={{ display: "flex", alignItems: "center", gap: 8, color: "#d87b7b", fontWeight: 600, marginBottom: 6 }}>
                <Check size={16} /> Real-Time Discord
              </div>
              <p style={{ fontSize: "0.85rem", color: "#551212", margin: 0 }}>Live presence, Spotify listening status, and avatar decoration.</p>
            </div>
          </div>

          <h3 style={{ fontSize: "1.1rem", fontWeight: 600, marginBottom: 16, color: "#000000" }}>
            Frequently Asked Questions About Profile Migration
          </h3>
          <div style={{ display: "flex", flexDirection: "column", gap: 16 }}>
            {TRANSFER_FAQS.map((faq) => (
              <div key={faq.q} style={{ borderBottom: "1px solid rgba(0, 0, 0, 0.06)", paddingBottom: 14 }}>
                <h4 style={{ fontSize: "0.95rem", fontWeight: 600, color: "#c80404", marginBottom: 6 }}>{faq.q}</h4>
                <p style={{ fontSize: "0.88rem", color: "#551212", lineHeight: 1.5, margin: 0 }}>{faq.a}</p>
              </div>
            ))}
          </div>

          <div style={{ marginTop: 24, display: "flex", justifyContent: "space-between", alignItems: "center", flexWrap: "wrap", gap: 12 }}>
            <Link
              href="/compare/guns-lol"
              style={{ fontSize: "0.88rem", color: "#d87b7b", textDecoration: "underline" }}
            >
              Read full guns.lol feature comparison matrix &rarr;
            </Link>
            <span style={{ fontSize: "0.82rem", color: "#3d1616" }}>
              sinss.lol is an independent platform not affiliated with guns.lol or Vaults.lol.
            </span>
          </div>
        </section>
      </main>
    </div>
  );
}
