/**
 * Caller-scoped endpoints — /api/v1/me/*.
 *
 * The me/orgs endpoint returns the user's NyxID org memberships (admin +
 * member only; viewer rows are filtered server-side). Used by the skill
 * create flow's owner picker and by the profile dropdown's org section.
 */

import { apiGet } from "./apiClient";

export interface MeIdentity {
  userId: string;
  email: string;
  displayName: string;
  roles: string[];
  permissions: string[];
}

/**
 * Read the caller's identity as the backend sees it — source of truth
 * when the OAuth id_token is missing email/name claims (e.g.
 * admin-created users).
 */
export async function fetchMe(): Promise<MeIdentity | null> {
  try {
    const res = await apiGet<MeIdentity>("/api/v1/me");
    return res.data ?? null;
  } catch {
    return null;
  }
}

export interface MyOrgMembership {
  /** The org's NyxID `user_id`. */
  userId: string;
  /** `"admin"` or `"member"` — Ornn filters out viewer server-side. */
  role: "admin" | "member";
  /** Human label for pickers / dropdowns. Falls back to userId on the server. */
  displayName: string;
}

export async function fetchMyOrgs(): Promise<MyOrgMembership[]> {
  const res = await apiGet<{ items: MyOrgMembership[] }>("/api/v1/me/orgs");
  return res.data?.items ?? [];
}

/**
 * NyxID catalog service the caller can tie a skill to:
 *   - `tier: "admin"`  → public/platform-wide service. Tying a skill
 *     here marks it a system skill and forces `isPrivate: false`.
 *   - `tier: "personal"` → private service the caller created.
 *     Tying here does not change the skill's privacy.
 */
export interface MyNyxidService {
  id: string;
  slug: string;
  label: string;
  description?: string | null;
  tier: "admin" | "personal";
}

export async function fetchMyNyxidServices(): Promise<MyNyxidService[]> {
  const res = await apiGet<{ items: MyNyxidService[] }>("/api/v1/me/nyxid-services");
  return res.data?.items ?? [];
}

/** Aggregate-summary row for a given org or user target. */
export interface GrantBucketOrg {
  id: string;
  displayName: string;
  skillCount: number;
}

export interface GrantBucketUser {
  userId: string;
  email: string;
  displayName: string;
  skillCount: number;
}

export interface GrantsSummary {
  orgs: GrantBucketOrg[];
  users: GrantBucketUser[];
}

/** "Which orgs/users have I shared my skills with?" */
export async function fetchMySkillGrantsSummary(): Promise<GrantsSummary> {
  const res = await apiGet<GrantsSummary>("/api/v1/me/skills/grants-summary");
  return res.data ?? { orgs: [], users: [] };
}

/** "Which orgs/users gave me access to their skills?" */
export async function fetchSharedSkillSourcesSummary(): Promise<GrantsSummary> {
  const res = await apiGet<GrantsSummary>("/api/v1/me/shared-skills/sources-summary");
  return res.data ?? { orgs: [], users: [] };
}
