import { create } from 'zustand';

export interface OrgInfo {
  id: string;         // Logto org ID (for token scoping)
  name: string;
  slug?: string;
  tenantId?: string;  // DB tenant UUID (for API calls)
}

interface OrgState {
  currentOrgId: string | null;    // Logto org ID — used for getAccessToken(resource, orgId)
  currentTenantId: string | null; // DB UUID — used for API calls like /api/tenants/{id}
  organizations: OrgInfo[];
  scopes: Set<string>;
  username: string | null;
  setCurrentOrg: (orgId: string | null) => void;
  setOrganizations: (orgs: OrgInfo[]) => void;
  setScopes: (scopes: Set<string>) => void;
  setUsername: (name: string | null) => void;
}

export const useOrgStore = create<OrgState>((set, get) => ({
  currentOrgId: null,
  currentTenantId: null,
  organizations: [],
  scopes: new Set(),
  username: null,
  setUsername: (name) => set({ username: name }),
  setCurrentOrg: (orgId) => {
    const org = get().organizations.find((o) => o.id === orgId);
    set({ currentOrgId: orgId, currentTenantId: org?.tenantId ?? null });
  },
  setOrganizations: (orgs) => {
    const { currentOrgId } = get();
    const match = currentOrgId ? orgs.find((o) => o.id === currentOrgId) : null;
    set({
      organizations: orgs,
      // Re-resolve tenantId when org list is enriched (e.g., OrgResolver adds tenantId)
      currentTenantId: match?.tenantId ?? get().currentTenantId,
    });
  },
  setScopes: (scopes) => set({ scopes }),
}));
