/**
 * Perfil — Página de perfil do usuário
 *
 * Escopo atual: nome, email, logout, acesso ao admin.
 * HUB-INTEGRATION-POINT (Fase 7): quando o SSO ONFIDE chegar,
 * esta página exibirá o perfil unificado do HUB.
 * Nenhuma mudança estrutural será necessária — apenas o provider de auth muda.
 */

import MainLayout from "@/components/MainLayout";
import { useAuth } from "@/_core/hooks/useAuth";
import { Link } from "wouter";

export default function Perfil() {
  const { user, logout, loading } = useAuth();

  if (loading) {
    return (
      <MainLayout>
        <div className="flex items-center justify-center h-64">
          <div className="w-8 h-8 rounded-full border-2 border-sof-gold border-t-transparent animate-spin" />
        </div>
      </MainLayout>
    );
  }

  return (
    <MainLayout>
      <div
        className="min-h-full"
        style={{ background: "linear-gradient(180deg, #3d0a1e 0%, #2a0110 20%, #0a0005 100%)" }}
      >
        <div className="px-4 lg:px-8 py-8 max-w-lg mx-auto">

          {user ? (
            <>
              {/* Avatar */}
              <div className="flex flex-col items-center mb-10">
                <div
                  className="w-24 h-24 rounded-full flex items-center justify-center mb-4 text-3xl font-bold font-ui text-sof-borgonha shadow-xl"
                  style={{ background: "radial-gradient(circle at 38% 35%, #e8c547 0%, #d4af37 50%, #b8962e 100%)" }}
                >
                  {user.name?.charAt(0).toUpperCase() ?? "U"}
                </div>
                <h1 className="text-2xl font-bold font-ui text-white mb-1">{user.name}</h1>
                <p className="text-white/50 text-sm font-ui">{user.email}</p>
                <span
                  className="mt-2 px-3 py-1 rounded-full text-xs font-ui font-bold uppercase tracking-wider text-sof-borgonha"
                  style={{ background: "var(--sof-gold)" }}
                >
                  {user.role}
                </span>
              </div>

              {/* Ações */}
              <div className="space-y-3">

                {user.role === "admin" && (
                  <Link href="/admin">
                    <div
                      className="flex items-center gap-4 px-5 py-4 rounded-2xl cursor-pointer transition-all"
                      style={{ background: "rgba(212,175,55,0.08)", border: "1px solid rgba(212,175,55,0.2)" }}
                    >
                      <div
                        className="w-10 h-10 rounded-full flex items-center justify-center flex-shrink-0"
                        style={{ background: "rgba(212,175,55,0.15)" }}
                      >
                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.8} className="w-5 h-5 text-sof-gold">
                          <path strokeLinecap="round" strokeLinejoin="round" d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.324.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 011.37.49l1.296 2.247a1.125 1.125 0 01-.26 1.431l-1.003.827c-.293.24-.438.613-.431.992a6.759 6.759 0 010 .255c-.007.378.138.75.43.99l1.005.828c.424.35.534.954.26 1.43l-1.298 2.247a1.125 1.125 0 01-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.57 6.57 0 01-.22.128c-.331.183-.581.495-.644.869l-.213 1.28c-.09.543-.56.941-1.11.941h-2.594c-.55 0-1.02-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 01-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 01-1.369-.49l-1.297-2.247a1.125 1.125 0 01.26-1.431l1.004-.827c.292-.24.437-.613.43-.992a6.932 6.932 0 010-.255c.007-.378-.138-.75-.43-.99l-1.004-.828a1.125 1.125 0 01-.26-1.43l1.297-2.247a1.125 1.125 0 011.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.087.22-.128.332-.183.582-.495.644-.869l.214-1.281z" />
                          <path strokeLinecap="round" strokeLinejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                        </svg>
                      </div>
                      <div>
                        <p className="text-sm font-bold font-ui text-sof-gold">Painel Administrativo</p>
                        <p className="text-xs text-white/40">Gerenciar episódios, séries e apresentadores</p>
                      </div>
                      <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} className="w-4 h-4 text-white/30 ml-auto">
                        <path strokeLinecap="round" strokeLinejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5" />
                      </svg>
                    </div>
                  </Link>
                )}

                <Link href="/biblioteca">
                  <div
                    className="flex items-center gap-4 px-5 py-4 rounded-2xl cursor-pointer transition-all"
                    style={{ background: "rgba(255,255,255,0.04)", border: "1px solid rgba(255,255,255,0.08)" }}
                  >
                    <div
                      className="w-10 h-10 rounded-full flex items-center justify-center flex-shrink-0"
                      style={{ background: "rgba(255,255,255,0.08)" }}
                    >
                      <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.8} className="w-5 h-5 text-white/60">
                        <path strokeLinecap="round" strokeLinejoin="round" d="M3.75 12h16.5m-16.5 3.75h16.5M3.75 19.5h16.5M5.625 4.5h12.75a1.875 1.875 0 010 3.75H5.625a1.875 1.875 0 010-3.75z" />
                      </svg>
                    </div>
                    <div>
                      <p className="text-sm font-bold font-ui text-white">Minha Biblioteca</p>
                      <p className="text-xs text-white/40">Favoritos, histórico e playlists</p>
                    </div>
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} className="w-4 h-4 text-white/30 ml-auto">
                      <path strokeLinecap="round" strokeLinejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5" />
                    </svg>
                  </div>
                </Link>

                {/* Logout */}
                <button
                  onClick={() => logout()}
                  className="w-full flex items-center gap-4 px-5 py-4 rounded-2xl transition-all text-left"
                  style={{ background: "rgba(220,38,38,0.06)", border: "1px solid rgba(220,38,38,0.15)" }}
                >
                  <div
                    className="w-10 h-10 rounded-full flex items-center justify-center flex-shrink-0"
                    style={{ background: "rgba(220,38,38,0.1)" }}
                  >
                    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.8} className="w-5 h-5 text-red-400">
                      <path strokeLinecap="round" strokeLinejoin="round" d="M15.75 9V5.25A2.25 2.25 0 0013.5 3h-6a2.25 2.25 0 00-2.25 2.25v13.5A2.25 2.25 0 007.5 21h6a2.25 2.25 0 002.25-2.25V15M12 9l-3 3m0 0l3 3m-3-3h12.75" />
                    </svg>
                  </div>
                  <p className="text-sm font-bold font-ui text-red-400">Sair da conta</p>
                </button>

              </div>
            </>
          ) : (
            /* Usuário não logado */
            <div className="text-center py-16">
              <div
                className="w-24 h-24 rounded-full mx-auto mb-6 flex items-center justify-center"
                style={{ background: "rgba(255,255,255,0.05)", border: "1px solid rgba(255,255,255,0.1)" }}
              >
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={1.5} className="w-12 h-12 text-white/30">
                  <path strokeLinecap="round" strokeLinejoin="round" d="M15.75 6a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0zM4.501 20.118a7.5 7.5 0 0114.998 0A17.933 17.933 0 0112 21.75c-2.676 0-5.216-.584-7.499-1.632z" />
                </svg>
              </div>
              <h2 className="text-xl font-bold font-ui text-white mb-2">
                Faça login para acessar seu perfil
              </h2>
              <p className="text-white/40 text-sm mb-8">
                Acesse sua biblioteca, favoritos e histórico
              </p>
              <div className="flex gap-3 justify-center">
                <Link href="/entrar">
                  <button
                    className="px-6 py-3 rounded-full text-sm font-bold font-ui text-white transition-colors"
                    style={{ background: "rgba(255,255,255,0.08)", border: "1px solid rgba(255,255,255,0.15)" }}
                  >
                    Entrar
                  </button>
                </Link>
                <Link href="/cadastro">
                  <button
                    className="px-6 py-3 rounded-full text-sm font-bold font-ui text-sof-borgonha transition-colors"
                    style={{ background: "var(--sof-gold)" }}
                  >
                    Cadastrar
                  </button>
                </Link>
              </div>
            </div>
          )}

        </div>
      </div>
    </MainLayout>
  );
}
