/**
 * Types for @bytelyst/org-client.
 * Browser/React Native-safe — no Node.js dependencies.
 */

export interface OrganizationDoc {
  id: string;
  productId: string;
  name: string;
  slug: string;
  status: 'active' | 'disabled';
  ownerUserId: string;
  metadata?: Record<string, unknown>;
  createdAt: string;
  updatedAt: string;
}

export interface WorkspaceDoc {
  id: string;
  orgId: string;
  productId: string;
  name: string;
  slug: string;
  status: 'active' | 'archived';
  description?: string;
  metadata?: Record<string, unknown>;
  createdAt: string;
  updatedAt: string;
}

export interface MembershipDoc {
  id: string;
  orgId: string;
  productId: string;
  scope: 'org' | 'workspace';
  workspaceId?: string;
  userId: string;
  role: 'owner' | 'admin' | 'member' | 'viewer';
  status: 'active' | 'invited' | 'disabled';
  invitedBy?: string;
  createdAt: string;
  updatedAt: string;
}

export interface LicenseDoc {
  id: string;
  productId: string;
  key: string;
  userId: string;
  plan: 'free' | 'pro' | 'enterprise';
  status: 'active' | 'revoked' | 'expired';
  activatedAt: string | null;
  expiresAt: string | null;
  deviceIds: string[];
  maxDevices: number;
  createdAt: string;
  updatedAt: string;
}

export interface OrgClientConfig {
  /** Platform-service base URL (e.g. "http://localhost:4003/api"). */
  baseUrl: string;

  /** Product identifier sent as x-product-id header on every request. */
  productId: string;

  /** Returns a JWT access token (admin role required), or null. */
  getAccessToken: () => string | null;
}

export interface OrgClient {
  // Organizations
  listOrgs(query?: { status?: string; limit?: number }): Promise<OrganizationDoc[]>;
  createOrg(input: { name: string; slug: string; ownerUserId?: string }): Promise<OrganizationDoc>;
  getOrg(id: string): Promise<OrganizationDoc>;
  updateOrg(id: string, updates: Partial<OrganizationDoc>): Promise<OrganizationDoc>;

  // Workspaces
  listWorkspaces(orgId: string): Promise<WorkspaceDoc[]>;
  createWorkspace(
    orgId: string,
    input: { name: string; slug: string; description?: string }
  ): Promise<WorkspaceDoc>;
  updateWorkspace(
    orgId: string,
    workspaceId: string,
    updates: Partial<WorkspaceDoc>
  ): Promise<WorkspaceDoc>;

  // Memberships
  listMemberships(
    orgId: string,
    query?: { scope?: string; limit?: number }
  ): Promise<MembershipDoc[]>;
  addMember(
    orgId: string,
    input: { userId: string; role?: string; scope?: string; workspaceId?: string }
  ): Promise<MembershipDoc>;
  updateMember(
    orgId: string,
    membershipId: string,
    updates: { role?: string; status?: string }
  ): Promise<MembershipDoc>;

  // Licenses
  generateLicense(input: {
    userId: string;
    plan: string;
    maxDevices?: number;
  }): Promise<LicenseDoc>;
  activateLicense(input: { key: string; deviceId: string }): Promise<LicenseDoc>;
  deactivateLicense(input: { key: string; deviceId: string }): Promise<void>;
}
