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

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

/**
 * Business-owner change language — shown as a popup (dimmed profile page in
 * normal flow + centered card). Applied immediately, no admin review.
 */
export default function BizLanguage() {
  const { navigate, lang, setLang } = useApp();
  const [selected, setSelected] = useState<WebLang>(lang);

  const save = () => {
    setLang(selected);
    navigate("bizProfile");
  };

  return (
    <div className="relative min-h-full flex flex-col bg-background-50">
      {/* Dimmed profile page (in normal flow) */}
      <div className="flex-1 flex flex-col">
        <BizProfileBody onLogout={() => undefined} />
      </div>

      {/* Dark layer + centered popup (absolute, not fixed) */}
      <div className="absolute inset-0 z-40 flex items-center justify-center px-6 bg-foreground-950/40">
        <div className="w-full rounded-[20px] bg-background-100 border border-background-200 p-6">
          <h3 className="text-[17px] font-bold text-foreground-950 text-center">تغيير اللغة</h3>

          <div className="mt-5 space-y-2.5">
            {LANGS.map((l) => {
              const active = selected === l.id;
              return (
                <button
                  key={l.id}
                  type="button"
                  onClick={() => setSelected(l.id)}
                  className={cn(
                    "w-full h-[56px] px-4 rounded-[14px] border flex items-center justify-between cursor-pointer transition-colors",
                    active
                      ? "border-foreground-950 bg-background-50"
                      : "border-background-200 bg-background-50 hover:border-background-300",
                  )}
                >
                  <span className="text-[14.5px] font-medium text-foreground-950">{l.label}</span>
                  <span
                    className={cn(
                      "w-5 h-5 rounded-full border-2 flex items-center justify-center shrink-0",
                      active ? "border-foreground-950" : "border-background-300",
                    )}
                  >
                    {active && <span className="w-2.5 h-2.5 rounded-full bg-foreground-950" />}
                  </span>
                </button>
              );
            })}
          </div>

          <div className="mt-6 flex gap-3">
            <button
              type="button"
              onClick={save}
              className="flex-1 h-[52px] rounded-[14px] bg-primary-500 text-background-50 text-[14px] font-semibold cursor-pointer hover:bg-primary-600 whitespace-nowrap active:scale-[0.98] transition-transform"
            >
              حفظ
            </button>
            <button
              type="button"
              onClick={() => navigate("bizProfile")}
              className="flex-1 h-[52px] rounded-[14px] border border-background-300 text-foreground-900 text-[14px] font-semibold cursor-pointer hover:bg-background-200 whitespace-nowrap"
            >
              إلغاء
            </button>
          </div>
        </div>
      </div>
    </div>
  );
}