import Link from "next/link";
import { Check, X, Film, Music, Shield, Palette, Sparkles, ArrowRight, RefreshCw } from "lucide-react";
import type { CompetitorDetail } from "@/lib/alternatives-data";
import { COMPETITORS } from "@/lib/alternatives-data";
import SiteHeader from "@/components/SiteHeader";
import MasterComparisonTable from "@/components/MasterComparisonTable";
import styles from "@/app/compare/compare.module.css";
import "@/app/landing.css";

interface CompetitorComparisonViewProps {
  competitor: CompetitorDetail;
  user: { id: string } | null;
  username?: string;
}

export default function CompetitorComparisonView({
  competitor,
  user,
  username,
}: CompetitorComparisonViewProps) {
  const faqSchema = {
    "@context": "https://schema.org",
    "@type": "FAQPage",
    mainEntity: competitor.faqs.map((item) => ({
      "@type": "Question",
      name: item.q,
      acceptedAnswer: {
        "@type": "Answer",
        text: item.a,
      },
    })),
  };

  const softwareSchema = {
    "@context": "https://schema.org",
    "@type": "SoftwareApplication",
    name: "sinss.lol",
    applicationCategory: "SocialNetworkingApplication",
    operatingSystem: "Web",
    offers: {
      "@type": "Offer",
      price: "0.00",
      priceCurrency: "USD",
    },
    featureList: [
      "Direct 1-click guns.lol profile importer",
      "Free MP4 and WebM video backgrounds",
      "Multi-track audio player with synced scrolling lyrics",
      "Real-time Discord presence and Spotify listening status",
      "15+ custom Google and aesthetic designer fonts",
      "Custom cursor upload and ambient particle trails",
      "6 profile card layouts",
      "$6.99 one-time lifetime premium with zero subscriptions",
    ],
    description: competitor.metaDescription,
  };

  const howToSchema = {
    "@context": "https://schema.org",
    "@type": "HowTo",
    name: `How to Switch from ${competitor.name} to sinss.lol`,
    description: `Step-by-step instructions for transferring your bio link and aesthetic profile to sinss.lol in under 30 seconds.`,
    step: [
      {
        "@type": "HowToStep",
        name: "Visit the Transfer Tool",
        text: "Go to https://sinss.lol/transfer in your browser.",
        url: "https://sinss.lol/transfer",
      },
      {
        "@type": "HowToStep",
        name: "Enter Your Existing Handle",
        text: `Type your existing ${competitor.name.includes("guns") ? "guns.lol" : "bio link"} handle into the transfer tool.`,
      },
      {
        "@type": "HowToStep",
        name: "Preview Transferred Profile",
        text: "Preview your transferred bio, links, audio track, background video, and colors instantly.",
      },
      {
        "@type": "HowToStep",
        name: "Claim on sinss.lol",
        text: "Select your sinss.lol username and claim your page with 100% free video backgrounds and synced lyrics.",
      },
    ],
  };

  const breadcrumbsSchema = {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    itemListElement: [
      {
        "@type": "ListItem",
        position: 1,
        name: "Home",
        item: "https://sinss.lol",
      },
      {
        "@type": "ListItem",
        position: 2,
        name: "Compare",
        item: "https://sinss.lol/compare",
      },
      {
        "@type": "ListItem",
        position: 3,
        name: `${competitor.name} Comparison`,
        item: `https://sinss.lol/compare/${competitor.slug}`,
      },
    ],
  };

  const navLinks = [
    { slug: "all", name: "All Comparisons", href: "/compare" },
    ...Object.values(COMPETITORS).map((c) => ({
      slug: c.slug,
      name: `vs ${c.name}`,
      href: `/compare/${c.slug}`,
    })),
  ];

  return (
    <div className={styles.pageShell}>
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(faqSchema) }}
      />
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(softwareSchema) }}
      />
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(howToSchema) }}
      />
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(breadcrumbsSchema) }}
      />

      <SiteHeader
        user={user}
        username={username}
        activePath={`/compare/${competitor.slug}`}
        showThemeToggle={false}
      />

      <main className={styles.contentWrapper}>
        <section className={styles.heroSection}>
          <div className={styles.badge}>{competitor.badge}</div>
          <h1 className={styles.heroTitle}>{competitor.heroTitle}</h1>
          <p className={styles.heroLead}>{competitor.heroLead}</p>
          <div className={styles.ctaGroup}>
            <Link href="/transfer" className={styles.primaryBtn}>
              Import from guns.lol (1-Click) <ArrowRight size={16} />
            </Link>
            <Link href="/claim" className={styles.secondaryBtn}>
              Claim Free Handle
            </Link>
          </div>
        </section>

        {/* Competitor Navigation Switcher */}
        <nav className={styles.competitorsNav} aria-label="Alternative Bio Links">
          {navLinks.map((item) => {
            const isActive = item.slug === competitor.slug;
            return (
              <Link
                key={item.slug}
                href={item.href}
                className={`${styles.navPill} ${isActive ? styles.navPillActive : ""}`}
              >
                {item.name}
              </Link>
            );
          })}
        </nav>

        {/* 1-on-1 Comparison Table */}
        <section className={styles.tableCard} aria-label={`sinss.lol vs ${competitor.name} Comparison`}>
          <div style={{ padding: "24px 24px 16px" }}>
            <h2 className={styles.sectionTitle} style={{ fontSize: "1.3rem" }}>
              sinss.lol vs {competitor.name}
            </h2>
            <p className={styles.sectionSubtitle}>
              Direct side-by-side feature comparison between sinss.lol and {competitor.name}.
            </p>
          </div>
          <div className={styles.tableWrapper}>
            <table className={styles.comparisonTable}>
              <thead>
                <tr>
                  <th style={{ width: "38%" }}>Feature</th>
                  <th className={styles.highlightTh} style={{ width: "32%" }}>
                    sinss.lol
                  </th>
                  <th style={{ width: "30%" }}>{competitor.name}</th>
                </tr>
              </thead>
              <tbody>
                {competitor.rows.map((row) => (
                  <tr key={row.feature}>
                    <td>
                      <span className={styles.featureName}>{row.feature}</span>
                      <span className={styles.featureSub}>{row.sub}</span>
                    </td>
                    <td className={styles.highlightCol}>
                      <span className={styles.tagIncluded}>
                        <Check size={16} /> {row.sinss}
                      </span>
                      <span className={styles.featureSub}>{row.sinssNote}</span>
                    </td>
                    <td>
                      <span className={row.winner === "sinss" ? styles.tagPaid : styles.tagIncluded}>
                        {row.winner === "sinss" ? <X size={15} /> : <Check size={15} />} {row.competitor}
                      </span>
                      <span className={styles.featureSub}>{row.competitorNote}</span>
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </section>

        {/* Core Feature Pillars */}
        <section className={styles.pillarsGrid} aria-label="Core Advantages">
          <div className={styles.pillarCard}>
            <div className={styles.pillarIcon}>
              <RefreshCw size={20} />
            </div>
            <h2 className={styles.pillarTitle}>Free 1-Click Profile Importer</h2>
            <p className={styles.pillarDesc}>
              Move your entire profile from guns.lol in 30 seconds. Seamlessly copies your bio, links, audio tracks, background video, colors, and font at{" "}
              <Link href="/transfer" style={{ color: "#d87b7b", textDecoration: "underline" }}>
                sinss.lol/transfer
              </Link>.
            </p>
          </div>

          <div className={styles.pillarCard}>
            <div className={styles.pillarIcon}>
              <Film size={20} />
            </div>
            <h2 className={styles.pillarTitle}>Free Video Backgrounds</h2>
            <p className={styles.pillarDesc}>
              Upload your own MP4 or WebM video background directly. No paywalls, no monthly subscription required just to have an animated aesthetic page.
            </p>
          </div>

          <div className={styles.pillarCard}>
            <div className={styles.pillarIcon}>
              <Music size={20} />
            </div>
            <h2 className={styles.pillarTitle}>Multi-Track Synced Lyrics</h2>
            <p className={styles.pillarDesc}>
              Integrate background music tracks with automatic real-time scrolling lyrics powered by LRCLIB. Synchronized karaoke-style lyric box renders smoothly beneath your card.
            </p>
          </div>

          <div className={styles.pillarCard}>
            <div className={styles.pillarIcon}>
              <Shield size={20} />
            </div>
            <h2 className={styles.pillarTitle}>One-Time Lifetime Upgrade</h2>
            <p className={styles.pillarDesc}>
              Say goodbye to perpetual monthly fees. If you ever choose to upgrade to Lifetime Premium, it is a single $6.99 one-time payment that unlocks 5 profile slots, custom badges, and image hosting forever.
            </p>
          </div>

          <div className={styles.pillarCard}>
            <div className={styles.pillarIcon}>
              <Palette size={20} />
            </div>
            <h2 className={styles.pillarTitle}>6 Flexible Profile Layouts</h2>
            <p className={styles.pillarDesc}>
              Pick from Classic, Portfolio, Scroll, Compact, Minimal, or Banner layouts at no cost. Switch your presentation style whenever you want with a single click.
            </p>
          </div>

          <div className={styles.pillarCard}>
            <div className={styles.pillarIcon}>
              <Sparkles size={20} />
            </div>
            <h2 className={styles.pillarTitle}>Custom Cursors & Particles</h2>
            <p className={styles.pillarDesc}>
              Upload a personalized cursor image and add ambient effects like sparkle trails, snow, rain, and glowing card auras completely free.
            </p>
          </div>
        </section>

        {/* Master Comparison Table showing ALL competitors */}
        <MasterComparisonTable />

        {/* FAQs */}
        <section className={styles.faqSection} aria-label="Frequently Asked Questions">
          <div className={styles.sectionHeader}>
            <h2 className={styles.sectionTitle}>Frequently Asked Questions</h2>
            <p className={styles.sectionSubtitle}>
              Common questions about comparing {competitor.name} and sinss.lol.
            </p>
          </div>
          <div className={styles.faqGrid}>
            {competitor.faqs.map((faq) => (
              <div key={faq.q} className={styles.faqItem}>
                <h3 className={styles.faqQuestion}>{faq.q}</h3>
                <p className={styles.faqAnswer}>{faq.a}</p>
              </div>
            ))}
          </div>
        </section>

        {/* Bottom CTA Banner */}
        <section className={styles.ctaBanner}>
          <h2>Ready to Switch to sinss.lol?</h2>
          <p>
            Transfer your existing profile from guns.lol in 30 seconds or claim a fresh handle today.
          </p>
          <div className={styles.ctaGroup}>
            <Link href="/transfer" className={styles.primaryBtn}>
              Import Profile (1-Click) <ArrowRight size={16} />
            </Link>
            <Link href="/claim" className={styles.secondaryBtn}>
              Claim Your Handle
            </Link>
          </div>
        </section>
      </main>
    </div>
  );
}
