import apiClient from "./apiClient";

export type AuditLog = {
  event_id: string;
  timestamp: string;
  user_id: string;
  event_type: string;
  status: string;
  ip_address: string;
  user_agent: string;
  device_id?: string;
  details?: string;
};

export type AuditLogListResponse = {
  items: AuditLog[];
  limit: number;
  cursor?: string;
  next_cursor?: string;
};

export type TenantSummary = {
  id: string;
  name: string;
  slug: string;
  description: string;
  status: string;
  createdAt: string;
  updatedAt: string;
};

export type TenantCreateRequest = {
  name: string;
  slug?: string;
  description?: string;
  status?: string;
};

export type TenantListResponse = {
  items: TenantSummary[];
  limit: number;
  offset: number;
  total: number;
};

export type TenantUpdateRequest = {
  name?: string;
  slug?: string;
  description?: string;
  status?: string;
};

export async function fetchAuditLogs(limit = 50, cursor?: string) {
  const { data } = await apiClient.get<AuditLogListResponse>("/v1/audit", {
    params: { limit, cursor },
  });
  return data;
}

export async function fetchTenants(limit = 50, offset = 0) {
  const { data } = await apiClient.get<TenantListResponse>(
    "/v1/admin/tenants",
    {
      params: { limit, offset },
    },
  );
  return data;
}

export async function fetchTenant(tenantId: string) {
  const { data } = await apiClient.get<TenantSummary>(
    `/v1/admin/tenants/${tenantId}`,
  );
  return data;
}

export async function createTenant(payload: TenantCreateRequest) {
  const { data } = await apiClient.post<TenantSummary>(
    "/v1/admin/tenants",
    payload,
  );
  return data;
}

export async function updateTenant(
  tenantId: string,
  payload: TenantUpdateRequest,
) {
  const { data } = await apiClient.put<TenantSummary>(
    `/v1/admin/tenants/${tenantId}`,
    payload,
  );
  return data;
}

export async function deleteTenant(tenantId: string) {
  await apiClient.delete(`/v1/admin/tenants/${tenantId}`);
}
