"use client";

import { useState, useEffect, FormEvent } from "react";
import { useAuth } from "@/lib/AuthContext";
import { useRouter } from "next/navigation";
import Link from "next/link";
import { getUserTier } from "@/lib/tiers";
import { PERSONAS } from "@/lib/personas";
import {
  User,
  Crown,
  Zap,
  LogOut,
  ChevronRight,
  Save,
  Sparkles,
  Shield,
  TrendingUp,
  ExternalLink,
  Gift,
} from "lucide-react";
import PremiumBadge from "@/components/PremiumBadge";
import ErrorFeedback from "@/components/ErrorFeedback";

const VALID_MADHABS = ["Hanafi", "Maliki", "Shafi'i", "Hanbali"];

export default function ProfilePage() {
  const { user, token, loading, logout, refreshUser } = useAuth();
  const router = useRouter();

  // Editable fields
  const [name, setName] = useState("");
  const [preferredName, setPreferredName] = useState("");
  const [coachPersona, setCoachPersona] = useState("");
  const [madhab, setMadhab] = useState("");
  const [coachingGoals, setCoachingGoals] = useState("");
  const [saving, setSaving] = useState(false);
  const [saveMessage, setSaveMessage] = useState("");

  useEffect(() => {
    if (!loading && !token) router.push("/auth");
  }, [loading, token, router]);

  useEffect(() => {
    if (user) {
      setName(user.name || "");
      setPreferredName(user.preferredName || "");
      setCoachPersona(user.coachPersona || "");
      setMadhab(user.madhab || "");
      setCoachingGoals(user.coachingGoals || "");
    }
  }, [user]);

  const tier = user ? getUserTier(user.isPremium, user.isPro) : "free";

  const tierColors = {
    free: { bg: "bg-gray-900/50", text: "text-gray-400", border: "border-gray-700/50", icon: User },
    premium: { bg: "bg-[#D4AF37]/10", text: "text-[#D4AF37]", border: "border-[#D4AF37]/30", icon: Sparkles },
    pro: { bg: "bg-purple-900/20", text: "text-purple-400", border: "border-purple-500/30", icon: Crown },
  };

  const tc = tierColors[tier];

  const handleSave = async (e: FormEvent) => {
    e.preventDefault();
    if (!token) return;
    setSaving(true);
    setSaveMessage("");

    try {
      const res = await fetch("/mobile/api/auth/profile", {
        method: "PATCH",
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${token}`,
        },
        body: JSON.stringify({
          name: name.trim() || undefined,
          preferredName: preferredName.trim() || undefined,
          coachPersona: coachPersona || undefined,
          madhab: madhab || undefined,
          coachingGoals: coachingGoals.trim() || undefined,
        }),
      });

      if (!res.ok) {
        const err = await res.json();
        throw new Error(err.error || "Save failed");
      }

      setSaveMessage("Profile updated successfully");
      await refreshUser();

      setTimeout(() => setSaveMessage(""), 3000);
    } catch (err: unknown) {
      const message =
        err instanceof Error ? err.message : "Failed to save profile";
      setSaveMessage(message);
    } finally {
      setSaving(false);
    }
  };

  const handleLogout = () => {
    logout();
    router.push("/auth");
  };

  if (loading) {
    return (
      <div className="min-h-dvh bg-[#0a0a0f] flex items-center justify-center">
        <div className="w-8 h-8 rounded-full border-2 border-[#D4AF37]/30 border-t-[#D4AF37] animate-spin" />
      </div>
    );
  }

  if (!user) return null;

  return (
    <div className="min-h-dvh bg-[#0a0a0f] pb-24">
      {/* Header */}
      <div className="px-4 pt-6 pb-2">
        <h1 className="text-xl font-bold text-white">Profile</h1>
      </div>

      <div className="px-4 space-y-5 animate-fade-in">
        {/* User Card */}
        <div className="bg-[#111118] border border-gray-800/60 rounded-2xl p-5">
          <div className="flex items-center gap-4">
            <div className="w-14 h-14 rounded-full bg-[#D4AF37]/15 flex items-center justify-center">
              <User size={24} className="text-[#D4AF37]" />
            </div>
            <div className="flex-1">
              <h2 className="text-lg font-bold text-white">
                {user.preferredName || user.name}
              </h2>
              <p className="text-sm text-gray-500">{user.email}</p>
            </div>
          </div>

          {/* Tier Badge */}
          <div
            className={`mt-4 flex items-center justify-between ${tc.bg} ${tc.border} border rounded-xl px-4 py-2.5`}
          >
            <div className="flex items-center gap-2">
              <tc.icon size={16} className={tc.text} />
              <span className={`text-sm font-semibold ${tc.text}`}>
                {tier === "free"
                  ? "Free"
                  : tier === "premium"
                    ? "Premium"
                    : "Pro"}
              </span>
              <PremiumBadge tier={tier} size="md" />
            </div>
            <div className="flex items-center gap-1.5">
              <Zap size={14} className="text-gray-600" />
              <span className="text-sm text-gray-400">
                {tier === "pro"
                  ? "Unlimited"
                  : `${user.dailyMsgCount}/10 AI today`}
              </span>
            </div>
          </div>

          {/* Premium/Pro Banner with Gradient */}
          {(tier === "premium" || tier === "pro") && (
            <div className={`mt-3 rounded-xl p-4 relative overflow-hidden ${
              tier === "pro"
                ? "bg-gradient-to-r from-purple-900/40 via-purple-900/20 to-gray-900/60 border border-purple-500/30"
                : "bg-gradient-to-r from-[#D4AF37]/20 via-yellow-900/15 to-gray-900/60 border border-[#D4AF37]/30"
            }`}>
              <div className={`absolute top-0 right-0 w-32 h-32 rounded-full blur-3xl ${
                tier === "pro" ? "bg-purple-500/10" : "bg-[#D4AF37]/10"
              }`} />
              <div className="relative flex items-center gap-3">
                <div className={`w-10 h-10 rounded-xl flex items-center justify-center ${
                  tier === "pro" ? "bg-purple-900/40" : "bg-[#D4AF37]/20"
                }`}>
                  {tier === "pro" ? (
                    <Crown size={20} className="text-purple-400" />
                  ) : (
                    <Sparkles size={20} className="text-[#D4AF37]" />
                  )}
                </div>
                <div className="flex-1">
                  <p className={`text-sm font-bold ${
                    tier === "pro" ? "text-purple-300" : "text-[#D4AF37]"
                  }`}>
                    {tier === "pro" ? "Pro Member" : "Premium Member"}
                  </p>
                  <p className="text-xs text-gray-500 mt-0.5">
                    {tier === "pro"
                      ? "Unlimited everything + 0% marketplace fees"
                      : "Enhanced AI + marketplace access"}
                  </p>
                </div>
              </div>
            </div>
          )}

          {/* FLH Earning Multiplier */}
          {(tier === "premium" || tier === "pro") && (
            <div className="mt-3 flex items-center justify-between bg-gradient-to-r from-[#D4AF37]/10 to-[#D4AF37]/5 border border-[#D4AF37]/20 rounded-xl px-4 py-3">
              <div className="flex items-center gap-2">
                <TrendingUp size={16} className="text-[#D4AF37]" />
                <span className="text-sm font-medium text-gray-300">
                  FLH Earning Multiplier
                </span>
              </div>
              <span className="text-lg font-bold text-[#D4AF37]">
                2x
              </span>
            </div>
          )}

          {/* FLH Balance */}
          <div className="mt-3 flex items-center justify-between bg-[#1a1a24] rounded-xl px-4 py-2.5">
            <div className="flex items-center gap-2">
              <span className="text-sm font-medium text-gray-400">
                FLH Balance
              </span>
            </div>
            <span className="text-lg font-bold text-[#D4AF37]">
              {user.flhBalance.toLocaleString()}
            </span>
          </div>
        </div>

        {/* Edit Profile Form */}
        <div className="bg-[#111118] border border-gray-800/60 rounded-2xl p-5">
          <h3 className="text-sm font-semibold text-white mb-4">
            Edit Profile
          </h3>

          {saveMessage === "Profile updated successfully" && (
            <div className="mb-4 p-3 rounded-xl text-sm text-center bg-emerald-900/20 border border-emerald-800/40 text-emerald-400">
              {saveMessage}
            </div>
          )}
          {saveMessage && saveMessage !== "Profile updated successfully" && (
            <ErrorFeedback error={saveMessage} kind="default" onRetry={() => setSaveMessage("")} context="profile" />
          )}

          <form onSubmit={handleSave} className="space-y-4">
            {/* Name */}
            <div>
              <label className="block text-xs font-medium text-gray-500 mb-1.5 uppercase tracking-wider">
                Name
              </label>
              <input
                type="text"
                value={name}
                onChange={(e) => setName(e.target.value)}
                className="w-full bg-[#1a1a24] border border-gray-800/60 rounded-xl px-4 py-4 text-white placeholder-gray-600 focus:outline-none focus:border-[#D4AF37]/50 focus:ring-1 focus:ring-[#D4AF37]/30 transition min-h-[48px]"
              />
            </div>

            {/* Preferred Name */}
            <div>
              <label className="block text-xs font-medium text-gray-500 mb-1.5 uppercase tracking-wider">
                Preferred Name
              </label>
              <input
                type="text"
                value={preferredName}
                onChange={(e) => setPreferredName(e.target.value)}
                placeholder="How Nur AI should address you"
                className="w-full bg-[#1a1a24] border border-gray-800/60 rounded-xl px-4 py-4 text-white placeholder-gray-600 focus:outline-none focus:border-[#D4AF37]/50 focus:ring-1 focus:ring-[#D4AF37]/30 transition min-h-[48px]"
              />
            </div>

            {/* Coach Persona */}
            <div>
              <label className="block text-xs font-medium text-gray-500 mb-1.5 uppercase tracking-wider">
                Coach Persona
              </label>
              <select
                value={coachPersona}
                onChange={(e) => setCoachPersona(e.target.value)}
                className="w-full bg-[#1a1a24] border border-gray-800/60 rounded-xl px-4 py-4 text-white focus:outline-none focus:border-[#D4AF37]/50 focus:ring-1 focus:ring-[#D4AF37]/30 transition appearance-none min-h-[48px]"
              >
                <option value="">Default</option>
                {PERSONAS.map((p) => (
                  <option key={p.id} value={p.id}>
                    {p.icon} {p.name}
                  </option>
                ))}
              </select>
            </div>

            {/* Madhab */}
            <div>
              <label className="block text-xs font-medium text-gray-500 mb-1.5 uppercase tracking-wider">
                Madhab (School of Thought)
              </label>
              <select
                value={madhab}
                onChange={(e) => setMadhab(e.target.value)}
                className="w-full bg-[#1a1a24] border border-gray-800/60 rounded-xl px-4 py-4 text-white focus:outline-none focus:border-[#D4AF37]/50 focus:ring-1 focus:ring-[#D4AF37]/30 transition appearance-none min-h-[48px]"
              >
                <option value="">Not specified</option>
                {VALID_MADHABS.map((m) => (
                  <option key={m} value={m}>
                    {m}
                  </option>
                ))}
              </select>
            </div>

            {/* Coaching Goals */}
            <div>
              <label className="block text-xs font-medium text-gray-500 mb-1.5 uppercase tracking-wider">
                Coaching Goals
              </label>
              <textarea
                value={coachingGoals}
                onChange={(e) => setCoachingGoals(e.target.value)}
                placeholder="What would you like to work on?"
                rows={3}
                className="w-full bg-[#1a1a24] border border-gray-800/60 rounded-xl px-4 py-4 text-white placeholder-gray-600 focus:outline-none focus:border-[#D4AF37]/50 focus:ring-1 focus:ring-[#D4AF37]/30 transition resize-none min-h-[48px]"
              />
            </div>

            <button
              type="submit"
              disabled={saving}
              className="w-full flex items-center justify-center gap-2 py-4 rounded-xl bg-[#D4AF37] text-[#0a0a0f] font-semibold text-sm active:opacity-80 disabled:opacity-50 transition min-h-[48px]"
            >
              {saving ? (
                <>
                  <span className="w-4 h-4 rounded-full border-2 border-[#0a0a0f]/30 border-t-[#0a0a0f] animate-spin" />
                  Saving...
                </>
              ) : (
                <>
                  <Save size={16} />
                  Save Changes
                </>
              )}
            </button>
          </form>
        </div>

        {/* Account Info */}
        <div className="bg-[#111118] border border-gray-800/60 rounded-2xl p-5">
          <h3 className="text-sm font-semibold text-white mb-3">
            Account Details
          </h3>
          <div className="space-y-2 text-sm">
            <div className="flex justify-between">
              <span className="text-gray-500">Member since</span>
              <span className="text-gray-300">
                {new Date(user.createdAt ?? "").toLocaleDateString("en-MY", {
                  year: "numeric",
                  month: "long",
                  day: "numeric",
                })}
              </span>
            </div>
            {user.trialEndsAt && (
              <div className="flex justify-between">
                <span className="text-gray-500">Trial ends</span>
                <span className="text-gray-300">
                  {new Date(user.trialEndsAt).toLocaleDateString("en-MY", {
                    year: "numeric",
                    month: "long",
                    day: "numeric",
                  })}
                </span>
              </div>
            )}
            {user.madhab && (
              <div className="flex justify-between">
                <span className="text-gray-500">Madhab</span>
                <span className="text-gray-300">{user.madhab}</span>
              </div>
            )}
          </div>

          {/* Subscription Management */}
          {(tier === "premium" || tier === "pro") && (
            <div className="mt-4 pt-3 border-t border-gray-800/60">
              <Link
                href="/upgrade"
                className="flex items-center justify-between py-3 px-4 rounded-xl bg-[#D4AF37]/10 border border-[#D4AF37]/20 active:bg-[#D4AF37]/20 transition"
              >
                <div className="flex items-center gap-2">
                  <ExternalLink size={14} className="text-[#D4AF37]" />
                  <span className="text-xs font-medium text-[#D4AF37]">
                    Manage Subscription
                  </span>
                </div>
                <ChevronRight size={14} className="text-[#D4AF37]" />
              </Link>
              {user.trialEndsAt && (
                <div className="mt-2 flex items-center gap-2 px-3 py-2.5">
                  <span className="text-xs text-gray-600">
                    Trial ends{" "}
                    {new Date(user.trialEndsAt).toLocaleDateString("en-MY", {
                      month: "short",
                      day: "numeric",
                    })}
                  </span>
                  <span className="text-xs text-gray-700">·</span>
                  <span className="text-xs text-amber-400 font-medium">
                    {Math.max(0, Math.ceil((new Date(user.trialEndsAt).getTime() - Date.now()) / (1000 * 60 * 60 * 24)))} days remaining
                  </span>
                </div>
              )}
            </div>
          )}
        </div>

        {/* Refer & Earn */}
        <Link
          href="/refer"
          className="w-full flex items-center justify-between py-3.5 px-5 rounded-xl bg-[#D4AF37]/10 border border-[#D4AF37]/20 text-[#D4AF37] active:bg-[#D4AF37]/20 transition min-h-[48px]"
        >
          <div className="flex items-center gap-2">
            <Gift size={16} />
            <span className="text-sm font-medium">Refer & Earn FLH</span>
          </div>
          <ChevronRight size={16} />
        </Link>

        {/* Logout */}
        <button
          onClick={handleLogout}
          className="w-full flex items-center justify-between py-3.5 px-5 rounded-xl bg-red-900/10 border border-red-800/30 text-red-400 active:bg-red-900/20 transition min-h-[48px]"
        >
          <div className="flex items-center gap-2">
            <LogOut size={16} />
            <span className="text-sm font-medium">Sign Out</span>
          </div>
          <ChevronRight size={16} />
        </button>

        <div className="h-4" />
      </div>
    </div>
  );
}
