import { apiRequest, type ApiSession } from './client'

export type BackendProject = {
  ID?: number
  id?: number
  Name?: string
  name?: string
  Identifier?: string
  identifier?: string
  Icon?: string
  icon?: string
  Background?: string
  background?: string
  Description?: string
  description?: string
}

export type BackendWorkspaceProject = {
  id: number
  name: string
  identifier?: string
  icon?: string
  background?: string
  description: string
  initials: string
  unreadCount: number
}

export type BackendChannel = {
  id: string
  projectID: number
  type: string
  title: string
  icon: string
  count?: number
  url?: string
  sortOrder: number
}

export type BackendInboxMessage = {
  id: string
  source: string
  title: string
  summary: string
  status: string
  tag: string
  time: string
}

export type BackendTask = {
  id: string
  title: string
  summary: string
  completed: boolean
  owner: string
  due: string
  createdAt: string
  completedAt: string
  duration: string
  tag: string
}

export type BackendAISession = {
  id: string
  title: string
  summary: string
  updatedAt: string
  references: string[]
}

export type BackendNoteSource = {
  id: string
  kind: string
  title: string
  updatedAt: string
  tag: string
  source: string
}

export type BackendCronPlan = {
  id: string
  title: string
  schedule: string
  nextRun: string
  enabled: boolean
  lastResult: string
  owner: string
}

export type BackendProjectWorkspace = {
  project: BackendWorkspaceProject
  channels: BackendChannel[]
  tags: string[]
  recentSessions: BackendAISession[]
  inbox: BackendInboxMessage[]
  tasks: BackendTask[]
  aiSessions: BackendAISession[]
  notesSources: BackendNoteSource[]
  cronPlans: BackendCronPlan[]
}

export type BackendTag = {
  ID?: number
  id?: number
  Name?: string
  name?: string
}

export async function fetchProjects(session: ApiSession) {
  return apiRequest<BackendProject[]>('/api/projects', { token: session.token })
}

export async function fetchProjectWorkspace(session: ApiSession, projectId: string | number) {
  return apiRequest<BackendProjectWorkspace>(`/api/projects/${projectId}/workspace`, { token: session.token })
}

export async function fetchProjectTags(session: ApiSession, projectId: string | number) {
  return apiRequest<BackendTag[]>(`/api/projects/${projectId}/tags`, { token: session.token })
}

export type CreateProjectInput = {
  name: string
  identifier?: string
  icon?: string
  background?: string
  description?: string
}

export type CreateTaskInput = {
  title: string
  description?: string
  status?: string
  dueAt?: string
  tag?: string
}

export type UpdateTaskInput = {
  title: string
  description?: string
  completed: boolean
  nextProjectId?: number
  tag?: string
}

export type UploadSourceInput = {
  title?: string
  file: File
}

export type CreateCronPlanInput = {
  title: string
  schedule: string
  enabled: boolean
  nextRunAt?: string
}

export type CreateProjectTagInput = {
  name: string
}

export async function createProject(session: ApiSession, input: CreateProjectInput) {
  return apiRequest<BackendProject>('/api/projects', {
    method: 'POST',
    token: session.token,
    body: input,
  })
}

export async function createTask(session: ApiSession, projectId: string | number, input: CreateTaskInput) {
  return apiRequest<BackendTask>(`/api/projects/${projectId}/tasks`, {
    method: 'POST',
    token: session.token,
    body: input,
  })
}

export async function updateTask(session: ApiSession, projectId: string | number, taskId: string | number, input: UpdateTaskInput) {
  return apiRequest<BackendTask>(`/api/projects/${projectId}/tasks/${taskId}`, {
    method: 'PATCH',
    token: session.token,
    body: input,
  })
}

export async function uploadSource(session: ApiSession, projectId: string | number, input: UploadSourceInput) {
  const body = new FormData()
  body.append('file', input.file)
  if (input.title) body.append('title', input.title)
  return apiRequest<BackendNoteSource>(`/api/projects/${projectId}/sources`, {
    method: 'POST',
    token: session.token,
    body,
  })
}

export async function createCronPlan(session: ApiSession, projectId: string | number, input: CreateCronPlanInput) {
  return apiRequest<BackendCronPlan>(`/api/projects/${projectId}/cron-plans`, {
    method: 'POST',
    token: session.token,
    body: input,
  })
}

export async function createProjectTag(session: ApiSession, projectId: string | number, input: CreateProjectTagInput) {
  return apiRequest<BackendTag>(`/api/projects/${projectId}/tags`, {
    method: 'POST',
    token: session.token,
    body: input,
  })
}
