import { useEffect, useState } from "react";
import { Link, NavLink } from "react-router-dom";
import { useAuth } from "../context/AuthContext";
import { useDocumentTitle } from "../hooks/useDocumentTitle";
import {
  fetchCreatorDashboard,
  type CreatorDashboardResponse,
} from "../api/creator-dashboard";
import { ApiError } from "../api/client";
import styles from "./CreatorDashboard.module.css";

function SidebarNav() {
  const linkClass = ({ isActive }: { isActive: boolean }) =>
    `${styles.sidebarLink}${isActive ? ` ${styles.sidebarLinkActive}` : ""}`;

  return (
    <nav className={styles.sidebar}>
      <NavLink to="/creator/dashboard" className={linkClass} end>
        <svg className={styles.sidebarIcon} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
          <rect x="3" y="3" width="7" height="7" rx="1" />
          <rect x="14" y="3" width="7" height="7" rx="1" />
          <rect x="3" y="14" width="7" height="7" rx="1" />
          <rect x="14" y="14" width="7" height="7" rx="1" />
        </svg>
        Dashboard
      </NavLink>
      <span
        className={`${styles.sidebarLink} ${styles.sidebarLinkDisabled}`}
        title="Coming soon"
        aria-disabled="true"
      >
        <svg className={styles.sidebarIcon} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
          <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
          <polyline points="14 2 14 8 20 8" />
        </svg>
        Content
      </span>
      <NavLink to="/creator/consent" className={linkClass}>
        <svg className={styles.sidebarIcon} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
          <rect x="3" y="11" width="18" height="11" rx="2" ry="2" />
          <path d="M7 11V7a5 5 0 0 1 10 0v4" />
        </svg>
        Consent
      </NavLink>
      <NavLink to="/creator/settings" className={linkClass}>
        <svg className={styles.sidebarIcon} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
          <circle cx="12" cy="12" r="3" />
          <path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z" />
        </svg>
        Settings
      </NavLink>
    </nav>
  );
}

export { SidebarNav };

/* ── Stat card ─────────────────────────────────────────────────────────────── */

function StatCard({ value, label }: { value: number; label: string }) {
  return (
    <div className={styles.statCard}>
      <span className={styles.statValue}>{value.toLocaleString()}</span>
      <span className={styles.statLabel}>{label}</span>
    </div>
  );
}

/* ── Helpers ────────────────────────────────────────────────────────────────── */

function formatDate(iso: string): string {
  return new Date(iso).toLocaleDateString(undefined, {
    year: "numeric",
    month: "short",
    day: "numeric",
  });
}

/** Map processing_status to a badge CSS class name (module-scoped). */
function statusBadgeClass(status: string): string {
  switch (status.toLowerCase()) {
    case "complete":
    case "completed":
      return styles.badgeComplete ?? "";
    case "processing":
      return styles.badgeProcessing ?? "";
    case "error":
    case "failed":
      return styles.badgeError ?? "";
    default:
      return styles.badgePending ?? "";
  }
}

/** Map topic_category to a badge CSS class name. */
function categoryBadgeClass(cat: string): string {
  const slug = cat.toLowerCase().replace(/[\s_]+/g, "-");
  const map: Record<string, string | undefined> = {
    "sound-design": styles.badgeCatSoundDesign,
    mixing: styles.badgeCatMixing,
    synthesis: styles.badgeCatSynthesis,
    arrangement: styles.badgeCatArrangement,
    workflow: styles.badgeCatWorkflow,
    mastering: styles.badgeCatMastering,
    "music-theory": styles.badgeCatMusicTheory,
  };
  return map[slug] ?? styles.badgeCatDefault ?? "";
}

/* ── Main component ────────────────────────────────────────────────────────── */

export default function CreatorDashboard() {
  useDocumentTitle("Creator Dashboard");
  const { user } = useAuth();

  const [data, setData] = useState<CreatorDashboardResponse | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    let cancelled = false;
    setLoading(true);
    setError(null);

    fetchCreatorDashboard()
      .then((res) => {
        if (!cancelled) setData(res);
      })
      .catch((err) => {
        if (cancelled) return;
        if (err instanceof ApiError && err.status === 404) {
          // No creator profile linked — show friendly empty state
          setError("not_linked");
        } else {
          setError(err instanceof ApiError ? err.detail : "Failed to load dashboard");
        }
      })
      .finally(() => {
        if (!cancelled) setLoading(false);
      });

    return () => {
      cancelled = true;
    };
  }, []);

  return (
    <div className={styles.layout}>
      <SidebarNav />
      <div className={styles.content}>
        <h1 className={styles.welcome}>
          Welcome back{user?.display_name ? `, ${user.display_name}` : ""}
        </h1>

        {loading && <DashboardSkeleton />}

        {!loading && error === "not_linked" && (
          <div className={styles.emptyState}>
            <h2>No Creator Profile</h2>
            <p>Your account isn't linked to a creator profile yet. Contact an admin to get set up.</p>
          </div>
        )}

        {!loading && error && error !== "not_linked" && (
          <div className={styles.errorState}>
            <p>Could not load dashboard: {error}</p>
          </div>
        )}

        {!loading && !error && data && (
          <>
            {/* ── Stats row ──────────────────────────────────────────── */}
            <div className={styles.statsRow}>
              <StatCard value={data.video_count} label="Uploads" />
              <StatCard value={data.technique_count} label="Techniques" />
              <StatCard value={data.key_moment_count} label="Key Moments" />
              <StatCard value={data.search_impressions} label="Search Impressions" />
            </div>

            {/* ── Techniques ─────────────────────────────────────────── */}
            <section className={styles.section}>
              <h2 className={styles.sectionTitle}>Technique Pages</h2>
              {data.techniques.length === 0 ? (
                <p className={styles.emptyText}>No technique pages yet.</p>
              ) : (
                <>
                  {/* Desktop table */}
                  <div className={styles.tableWrap}>
                    <table className={styles.table}>
                      <thead>
                        <tr>
                          <th>Title</th>
                          <th>Category</th>
                          <th>Moments</th>
                          <th>Created</th>
                        </tr>
                      </thead>
                      <tbody>
                        {data.techniques.map((t) => (
                          <tr key={t.slug}>
                            <td>
                              <Link to={`/techniques/${t.slug}`} className={styles.link}>
                                {t.title}
                              </Link>
                            </td>
                            <td>
                              <span className={`${styles.badge} ${categoryBadgeClass(t.topic_category)}`}>
                                {t.topic_category}
                              </span>
                            </td>
                            <td>{t.key_moment_count}</td>
                            <td>{formatDate(t.created_at)}</td>
                          </tr>
                        ))}
                      </tbody>
                    </table>
                  </div>

                  {/* Mobile cards */}
                  <div className={styles.mobileCards}>
                    {data.techniques.map((t) => (
                      <div key={t.slug} className={styles.mobileCard}>
                        <Link to={`/techniques/${t.slug}`} className={styles.link}>
                          {t.title}
                        </Link>
                        <div className={styles.mobileCardMeta}>
                          <span className={`${styles.badge} ${categoryBadgeClass(t.topic_category)}`}>
                            {t.topic_category}
                          </span>
                          <span>{t.key_moment_count} moments</span>
                          <span>{formatDate(t.created_at)}</span>
                        </div>
                      </div>
                    ))}
                  </div>
                </>
              )}
            </section>

            {/* ── Videos ──────────────────────────────────────────────── */}
            <section className={styles.section}>
              <h2 className={styles.sectionTitle}>Source Videos</h2>
              {data.videos.length === 0 ? (
                <p className={styles.emptyText}>No videos uploaded yet.</p>
              ) : (
                <>
                  <div className={styles.tableWrap}>
                    <table className={styles.table}>
                      <thead>
                        <tr>
                          <th>Filename</th>
                          <th>Status</th>
                          <th>Uploaded</th>
                        </tr>
                      </thead>
                      <tbody>
                        {data.videos.map((v) => (
                          <tr key={v.filename}>
                            <td className={styles.filename}>{v.filename}</td>
                            <td>
                              <span className={`${styles.badge} ${statusBadgeClass(v.processing_status)}`}>
                                {v.processing_status}
                              </span>
                            </td>
                            <td>{formatDate(v.created_at)}</td>
                          </tr>
                        ))}
                      </tbody>
                    </table>
                  </div>

                  <div className={styles.mobileCards}>
                    {data.videos.map((v) => (
                      <div key={v.filename} className={styles.mobileCard}>
                        <span className={styles.filename}>{v.filename}</span>
                        <div className={styles.mobileCardMeta}>
                          <span className={`${styles.badge} ${statusBadgeClass(v.processing_status)}`}>
                            {v.processing_status}
                          </span>
                          <span>{formatDate(v.created_at)}</span>
                        </div>
                      </div>
                    ))}
                  </div>
                </>
              )}
            </section>
          </>
        )}
      </div>
    </div>
  );
}

/* ── Loading skeleton ──────────────────────────────────────────────────────── */

function DashboardSkeleton() {
  return (
    <div className={styles.skeleton}>
      <div className={styles.statsRow}>
        {[1, 2, 3, 4].map((i) => (
          <div key={i} className={`${styles.statCard} ${styles.skeletonPulse}`} />
        ))}
      </div>
      <div className={`${styles.skeletonBlock} ${styles.skeletonPulse}`} />
      <div className={`${styles.skeletonBlock} ${styles.skeletonPulse}`} />
    </div>
  );
}
