import { useState } from "react";
import ProfileBody from "../components/ProfileBody";
import { useApp } from "../AppContext";
import { cn } from "../components/ui";
import type { WebLang } from "@/pages/web/types";

const LANGS: { id: WebLang; label: string }[] = [
  { id: "ar", label: "العربية" },
  { id: "en", label: "English" },
];

export default function Profile() {
  const { navigate, lang, setLang } = useApp();
  const [langOpen, setLangOpen] = useState(false);

  return (
    <div className="relative min-h-full flex flex-col bg-background-50">
      <ProfileBody
        onLogout={() => navigate("profileLogout")}
        onDelete={() => navigate("profileDelete")}
        onLanguage={() => setLangOpen(true)}
      />

      {/* Language picker — popup in normal flow (absolute inside the page) */}
      {langOpen && (
        <div
          className="absolute inset-0 z-40 flex items-center justify-center px-6 bg-foreground-950/40"
          onClick={() => setLangOpen(false)}
        >
          <div
            className="w-full max-w-[340px] rounded-[20px] bg-background-100 border border-background-200 p-5"
            onClick={(e) => e.stopPropagation()}
          >
            <div className="flex items-center gap-2.5">
              <span className="w-9 h-9 rounded-[12px] bg-background-200 flex items-center justify-center shrink-0">
                <i className="ri-translate-2 text-[18px] text-foreground-950 leading-none" />
              </span>
              <h3 className="text-[16px] font-bold text-foreground-950">تغيير اللغة</h3>
            </div>

            <div className="mt-4 space-y-2.5">
              {LANGS.map((l) => {
                const active = lang === l.id;
                return (
                  <button
                    key={l.id}
                    type="button"
                    onClick={() => {
                      setLang(l.id);
                      setLangOpen(false);
                    }}
                    className={cn(
                      "w-full h-[52px] px-4 rounded-[14px] border flex items-center justify-between cursor-pointer transition-colors",
                      active
                        ? "border-primary-500 bg-primary-50"
                        : "border-background-200 bg-background-50 hover:border-background-300",
                    )}
                  >
                    <span className="text-[14.5px] font-medium text-foreground-950">{l.label}</span>
                    {active && <i className="ri-check-line text-[20px] text-primary-600 leading-none" />}
                  </button>
                );
              })}
            </div>
          </div>
        </div>
      )}
    </div>
  );
}