import { apiFetch } from "./api";
import type {
  Contact,
  Company,
  InboxItem,
  TimeEntry,
  SavedView,
  CreateContactInput,
  UpdateContactInput,
  CreateCompanyInput,
  UpdateCompanyInput,
  CreateInboxItemInput,
  CreateTimeEntryInput,
  UpdateTimeEntryInput,
  CreateSavedViewInput
} from "./types-crm";

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

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

export async function apiUpdateContact(contactId: string, input: UpdateContactInput): Promise<Contact> {
  const res = await apiFetch(`/v1/contacts/${contactId}`, {
    method: "PATCH",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(input)
  });
  const data = await res.json();
  return data.data;
}

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

export async function apiLinkContactToTask(contactId: string, taskId: string): Promise<void> {
  await apiFetch(`/v1/contacts/${contactId}/tasks/${taskId}`, { method: "POST" });
}

export async function apiUnlinkContactFromTask(contactId: string, taskId: string): Promise<void> {
  await apiFetch(`/v1/contacts/${contactId}/tasks/${taskId}`, { method: "DELETE" });
}

export async function apiLinkContactToEvent(contactId: string, eventId: string): Promise<void> {
  await apiFetch(`/v1/contacts/${contactId}/events/${eventId}`, { method: "POST" });
}

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

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

export async function apiUpdateCompany(companyId: string, input: UpdateCompanyInput): Promise<Company> {
  const res = await apiFetch(`/v1/companies/${companyId}`, {
    method: "PATCH",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(input)
  });
  const data = await res.json();
  return data.data;
}

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

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

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

export async function apiProcessInboxItem(itemId: string, entityType: string, entityId: string): Promise<void> {
  await apiFetch(`/v1/inbox/${itemId}/process`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ entityType, entityId })
  });
}

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

// Time entries
export async function apiListTimeEntries(workspaceSlug: string): Promise<TimeEntry[]> {
  const res = await apiFetch(`/v1/time-entries?workspaceSlug=${encodeURIComponent(workspaceSlug)}`);
  const data = await res.json();
  return data.data || [];
}

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

export async function apiUpdateTimeEntry(entryId: string, input: UpdateTimeEntryInput): Promise<TimeEntry> {
  const res = await apiFetch(`/v1/time-entries/${entryId}`, {
    method: "PATCH",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(input)
  });
  const data = await res.json();
  return data.data;
}

export async function apiDeleteTimeEntry(entryId: string): Promise<void> {
  await apiFetch(`/v1/time-entries/${entryId}`, { method: "DELETE" });
}

// Saved views
export async function apiListSavedViews(workspaceSlug: string, entityType: string): Promise<SavedView[]> {
  const res = await apiFetch(`/v1/saved-views?workspaceSlug=${encodeURIComponent(workspaceSlug)}&entityType=${encodeURIComponent(entityType)}`);
  const data = await res.json();
  return data.data || [];
}

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

export async function apiDeleteSavedView(viewId: string): Promise<void> {
  await apiFetch(`/v1/saved-views/${viewId}`, { method: "DELETE" });
}