import { fetchWithAuth } from "./client";

export type MemberRole = "admin" | "member" | "viewer";
export type MemberStatus = "active" | "pending" | "inactive";

export interface OrganizationMember {
  id: string;
  user_id: string;
  name: string;
  email: string;
  avatar_url?: string;
  role: MemberRole;
  status: MemberStatus;
  joined_at: string;
  created_at: string;
  updated_at: string;
}

export interface OrganizationInfo {
  id: string;
  name: string;
  member_count: number;
  created_at: string;
  updated_at: string;
}

export interface InviteMemberPayload {
  email: string;
  role: MemberRole;
}

export interface UpdateMemberRolePayload {
  role: MemberRole;
}

export const organizationApi = {
  getInfo: (token: string) =>
    fetchWithAuth("/api/v1/organization/info", {}, token) as Promise<OrganizationInfo>,

  listMembers: (token: string, params?: string) =>
    fetchWithAuth(
      `/api/v1/organization/members/${params ? `?${params}` : ""}`,
      {},
      token
    ) as Promise<OrganizationMember[]>,

  inviteMember: (token: string, data: InviteMemberPayload) =>
    fetchWithAuth(
      "/api/v1/organization/members/invite",
      { method: "POST", body: JSON.stringify(data) },
      token
    ) as Promise<OrganizationMember>,

  updateMemberRole: (token: string, memberId: string, data: UpdateMemberRolePayload) =>
    fetchWithAuth(
      `/api/v1/organization/members/${memberId}/role`,
      { method: "PUT", body: JSON.stringify(data) },
      token
    ) as Promise<OrganizationMember>,

  removeMember: (token: string, memberId: string) =>
    fetchWithAuth(
      `/api/v1/organization/members/${memberId}`,
      { method: "DELETE" },
      token
    ),
};
