import type {
  AcceptComplexInvitationsInput,
  AcceptComplexInvitationsResponse,
  CancelComplexInvitationResponse,
  Complex,
  ComplexUsersOverview,
  ComplexWithRole,
  CreateComplexPayload,
  CreateComplexResponse,
  InviteComplexUserInput,
  InviteComplexUserResponse,
  RevokeComplexUserInput,
  SelectComplexInput,
  UpdateComplexPayload,
  UpdateComplexResponse,
} from '@repo/api-contract';
import { http } from '../http';

export async function create(
  payload: CreateComplexPayload & { adminEmail?: string; userId?: string }
) {
  const response = await http.post<CreateComplexResponse>('/api/complexes', payload);
  return response.data;
}

export async function getById(id: string) {
  const response = await http.get<Complex>(`/api/complexes/${id}`);
  return response.data;
}

export async function getBySlug(slug: string) {
  const response = await http.get<Complex>(`/api/complexes/slug/${slug}`);
  return response.data;
}

export async function update(id: string, payload: UpdateComplexPayload) {
  const response = await http.patch<UpdateComplexResponse>(`/api/complexes/${id}`, payload);
  return response.data;
}

export async function listMine() {
  const response = await http.get<ComplexWithRole[]>('/api/complexes/mine');
  return response.data;
}

export async function getCurrent() {
  const response = await http.get<ComplexWithRole | null>('/api/complexes/me');
  return response.data;
}

export async function select(payload: SelectComplexInput) {
  const response = await http.post<ComplexWithRole>(
    '/api/complexes/select',
    JSON.stringify(payload),
    { headers: { 'Content-Type': 'application/json' } }
  );
  return response.data;
}

export async function listUsers(id: string) {
  const response = await http.get<ComplexUsersOverview>(`/api/complexes/${id}/users`);
  return response.data;
}

export async function inviteUser(id: string, payload: InviteComplexUserInput) {
  const response = await http.post<InviteComplexUserResponse>(
    `/api/complexes/${id}/users`,
    JSON.stringify(payload),
    { headers: { 'Content-Type': 'application/json' } }
  );
  return response.data;
}

export async function revokeUser(id: string, payload: RevokeComplexUserInput) {
  const response = await http.delete<{ ok: boolean }>(
    `/api/complexes/${id}/users/${payload.userId}`
  );
  return response.data;
}

export async function acceptPendingInvitations(payload: AcceptComplexInvitationsInput = {}) {
  const response = await http.post<AcceptComplexInvitationsResponse>(
    '/api/complexes/accept-pending',
    JSON.stringify(payload),
    { headers: { 'Content-Type': 'application/json' } }
  );
  return response.data;
}

export async function resendInvitation(id: string, invitationId: string) {
  const response = await http.post<InviteComplexUserResponse>(
    `/api/complexes/${id}/invitations/${invitationId}/resend`
  );
  return response.data;
}

export async function cancelInvitation(id: string, invitationId: string) {
  const response = await http.delete<CancelComplexInvitationResponse>(
    `/api/complexes/${id}/invitations/${invitationId}`
  );
  return response.data;
}
