import { useState } from "react";
import { useTranslation } from "react-i18next";
import { Lock, Plus } from "lucide-react";
import { useProfile } from "../contexts/ProfileContext";
import { useEntitlement } from "../hooks/useEntitlement";
import { isProfileCreationLocked } from "../shared/profileGate";
import { APP_NAME } from "../shared/constants";
import PinDialog from "../components/profile/PinDialog";
import ProfileFormModal from "../components/profile/ProfileFormModal";

export default function ProfileSelectionPage() {
  const { t } = useTranslation();
  const { profiles, switchProfile, updateProfile } = useProfile();
  // Multi-profile gate (Base+, #300). This page renders only when no active
  // profile resolves, so profile SELECTION stays free (never lock a user out
  // of all of their profiles — see decisions log); only the creation entry is
  // marked. The gate itself lives at the single creation point,
  // ProfileFormModal, which this page opens.
  const gate = useEntitlement("multi-profile");
  const creationLocked = isProfileCreationLocked(profiles.length, gate);
  const [pinProfileId, setPinProfileId] = useState<string | null>(null);
  const [showCreate, setShowCreate] = useState(false);

  const handleSelect = (profileId: string) => {
    const profile = profiles.find((p) => p.id === profileId);
    if (!profile) return;

    if (profile.pin_hash) {
      setPinProfileId(profileId);
    } else {
      switchProfile(profileId);
    }
  };

  const handlePinSuccess = async (rehashed?: string | null) => {
    if (pinProfileId) {
      if (rehashed) {
        try {
          await updateProfile(pinProfileId, { pin_hash: rehashed });
        } catch {
          // Best-effort rehash: don't block profile switch if persistence fails
        }
      }
      switchProfile(pinProfileId);
      setPinProfileId(null);
    }
  };

  const pinProfile = profiles.find((p) => p.id === pinProfileId);

  return (
    <div className="flex flex-col items-center justify-center min-h-screen bg-[var(--background)] p-8">
      <h1 className="text-3xl font-bold text-[var(--foreground)] mb-2">{APP_NAME}</h1>
      <p className="text-[var(--muted-foreground)] mb-10">{t("profile.select")}</p>

      <div className="grid grid-cols-2 sm:grid-cols-3 gap-4 max-w-lg w-full">
        {profiles.map((profile) => (
          <button
            key={profile.id}
            onClick={() => handleSelect(profile.id)}
            className="flex flex-col items-center gap-3 p-6 rounded-xl bg-[var(--card)] border border-[var(--border)] hover:border-[var(--primary)] transition-colors cursor-pointer"
          >
            <div
              className="w-14 h-14 rounded-full flex items-center justify-center text-white text-xl font-bold"
              style={{ backgroundColor: profile.color }}
            >
              {profile.name.charAt(0).toUpperCase()}
            </div>
            <span className="text-sm font-medium text-[var(--foreground)]">{profile.name}</span>
            {profile.pin_hash && (
              <Lock size={14} className="text-[var(--muted-foreground)]" />
            )}
          </button>
        ))}

        <button
          onClick={() => setShowCreate(true)}
          title={creationLocked ? t("nav.locked") : undefined}
          className={`flex flex-col items-center justify-center gap-3 p-6 rounded-xl border-2 border-dashed border-[var(--border)] hover:border-[var(--primary)] transition-colors cursor-pointer ${
            creationLocked ? "opacity-60" : ""
          }`}
        >
          <div className="w-14 h-14 rounded-full flex items-center justify-center bg-[var(--muted)]">
            {/* Locked-not-hidden: the entry stays visible with a lock; the
                modal's single creation gate shows the upsell on click. */}
            {creationLocked ? (
              <Lock size={24} className="text-[var(--muted-foreground)]" />
            ) : (
              <Plus size={24} className="text-[var(--muted-foreground)]" />
            )}
          </div>
          <span className="text-sm font-medium text-[var(--muted-foreground)]">
            {t("profile.create")}
          </span>
        </button>
      </div>

      {pinProfileId && pinProfile && (
        <PinDialog
          profileName={pinProfile.name}
          storedHash={pinProfile.pin_hash!}
          onSuccess={handlePinSuccess}
          onCancel={() => setPinProfileId(null)}
        />
      )}

      {showCreate && (
        <ProfileFormModal onClose={() => setShowCreate(false)} />
      )}
    </div>
  );
}
