import { apiFetch } from "./api";
import type {
  Integration,
  Webhook,
  Notification,
  Presence,
  CreateIntegrationInput,
  CreateWebhookInput,
  UpdatePresenceInput,
  CreateNotificationInput
} from "./types-integrations";

// Integrations
export async function apiListIntegrations(workspaceSlug: string): Promise<Integration[]> {
  const res = await apiFetch(`/v1/integrations?workspaceSlug=${encodeURIComponent(workspaceSlug)}`);
  const data = await res.json();
  return data.data || [];
}

export async function apiCreateIntegration(input: CreateIntegrationInput): Promise<Integration> {
  const res = await apiFetch("/v1/integrations", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(input)
  });
  const data = await res.json();
  return data.data;
}

export async function apiDeleteIntegration(integrationId: string): Promise<void> {
  await apiFetch(`/v1/integrations/${integrationId}`, { method: "DELETE" });
}

// Webhooks
export async function apiListWebhooks(workspaceSlug: string): Promise<Webhook[]> {
  const res = await apiFetch(`/v1/webhooks?workspaceSlug=${encodeURIComponent(workspaceSlug)}`);
  const data = await res.json();
  return data.data || [];
}

export async function apiCreateWebhook(input: CreateWebhookInput): Promise<Webhook> {
  const res = await apiFetch("/v1/webhooks", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ ...input, events: JSON.stringify(input.events || []) })
  });
  const data = await res.json();
  return data.data;
}

export async function apiDeleteWebhook(webhookId: string): Promise<void> {
  await apiFetch(`/v1/webhooks/${webhookId}`, { method: "DELETE" });
}

// Notifications
export async function apiListNotifications(): Promise<Notification[]> {
  const res = await apiFetch("/v1/notifications");
  const data = await res.json();
  return data.data || [];
}

export async function apiMarkNotificationRead(notificationId: string): Promise<void> {
  await apiFetch(`/v1/notifications/${notificationId}/read`, { method: "POST" });
}

export async function apiMarkAllNotificationsRead(): Promise<void> {
  await apiFetch("/v1/notifications/read-all", { method: "POST" });
}

export async function apiUnreadNotificationCount(): Promise<number> {
  const res = await apiFetch("/v1/notifications/unread-count");
  const data = await res.json();
  return data.count || 0;
}

// Presence
export async function apiUpdatePresence(input: UpdatePresenceInput): Promise<Presence> {
  const res = await apiFetch("/v1/presence", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(input)
  });
  const data = await res.json();
  return data.data;
}

export async function apiListPresence(workspaceSlug: string, entityType?: string, entityId?: string): Promise<Presence[]> {
  let url = `/v1/presence?workspaceSlug=${encodeURIComponent(workspaceSlug)}`;
  if (entityType) url += `&entityType=${encodeURIComponent(entityType)}`;
  if (entityId) url += `&entityId=${encodeURIComponent(entityId)}`;
  const res = await apiFetch(url);
  const data = await res.json();
  return data.data || [];
}

export async function apiClearPresence(workspaceSlug: string): Promise<void> {
  await apiFetch(`/v1/presence?workspaceSlug=${encodeURIComponent(workspaceSlug)}`, { method: "DELETE" });
}

// Create notification
export async function apiCreateNotification(input: CreateNotificationInput): Promise<Notification> {
  const res = await apiFetch("/v1/notifications", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(input)
  });
  const data = await res.json();
  return data.data;
}
// OAuth
export async function apiConnectGoogleCalendar(workspaceSlug: string, redirect?: string): Promise<OAuthConnectResponse> {
  const params = new URLSearchParams({ workspaceSlug });
  if (redirect) params.append("redirect", redirect);
  const res = await apiFetch(`/v1/oauth/google-calendar/connect?${params.toString()}`);
  const data = await res.json();
  return data;
}

export async function apiConnectSlack(workspaceSlug: string, redirect?: string): Promise<OAuthConnectResponse> {
  const params = new URLSearchParams({ workspaceSlug });
  if (redirect) params.append("redirect", redirect);
  const res = await apiFetch(`/v1/oauth/slack/connect?${params.toString()}`);
  const data = await res.json();
  return data;
}

export async function apiDisconnectIntegration(integrationId: string): Promise<void> {
  await apiFetch(`/v1/integrations/${integrationId}/disconnect`, { method: "POST" });
}

import type { OAuthConnectResponse } from "./types-integrations";