"use client";

import { useQuery } from "@abe-kap/appshell-sdk/react";
import { isShellConfigured } from "./appshell";

/**
 * The signed-in user's effective CRM access, from crm.account.me. Drives navigation
 * gating: a user with no membership/permissions sees only Dashboard + Profile; members
 * see the areas their permissions allow.
 *
 * Every CRM permission id, granted to superadmins / owners. Used as the mock default
 * when the Shell isn't configured (local demo shows everything).
 */
export const ALL_CRM_PERMISSIONS = [
  "leads.manage", "pipeline.manage", "estimates.create", "dispatch.manage",
  "reports.view", "billing.view", "team.manage", "roles.manage", "settings.manage",
] as const;

export interface MyAccess {
  registered: boolean;
  isMember: boolean;
  roleSlugs: string[];
  permissions: string[];
  isSuperadmin: boolean;
  /** True while the access query is still resolving (nav stays minimal until then). */
  loading: boolean;
  can: (permission: string) => boolean;
}

interface MeDTO {
  registered: boolean;
  isMember: boolean;
  roleSlugs: string[];
  permissions: string[];
  isSuperadmin: boolean;
}

function useLiveAccess(): MyAccess {
  const { data, loading } = useQuery<MeDTO>("crm.account.me");
  const permissions = data?.permissions ?? [];
  return {
    registered: data?.registered ?? false,
    isMember: data?.isMember ?? false,
    roleSlugs: data?.roleSlugs ?? [],
    permissions,
    isSuperadmin: data?.isSuperadmin ?? false,
    loading,
    can: (p: string) => permissions.includes(p),
  };
}

function useMockAccess(): MyAccess {
  // No Shell (local demo): show everything so the mock UI is fully browsable.
  const permissions = [...ALL_CRM_PERMISSIONS];
  return {
    registered: true,
    isMember: true,
    roleSlugs: ["superadmin"],
    permissions,
    isSuperadmin: true,
    loading: false,
    can: () => true,
  };
}

export const useMyAccess: () => MyAccess = isShellConfigured() ? useLiveAccess : useMockAccess;
