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

export type ProjectDTO = {
  id: string
  name: string
  identifier: string
  icon: string
  background: string
  description: string
}

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

export type WorkspaceChannelDTO = {
  id: string
  projectId: string
  type: string
  title: string
  icon: string
  count: number
  url: string
  sortOrder: number
}

export type WorkspaceTagDTO = {
  id: string
  name: string
}

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

export type WorkspaceTaskDTO = {
  id: string
  projectId: string
  title: string
  summary: string
  completed: boolean
  owner: string
  due: string | null
  createdAt: string
  completedAt: string | null
  tagId: string | null
  tag: string
}

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

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

export type WorkspaceCronPlanDTO = {
  id: string
  projectId: string
  title: string
  schedule: string
  nextRun: string | null
  enabled: boolean
  lastResult: string
  owner: string
}

export type ProjectWorkspaceDTO = {
  project: WorkspaceProjectDTO
  channels: WorkspaceChannelDTO[]
  tags: WorkspaceTagDTO[]
  recentSessions: WorkspaceAISessionDTO[]
  inbox: WorkspaceInboxDTO[]
  tasks: WorkspaceTaskDTO[]
  aiSessions: WorkspaceAISessionDTO[]
  notesSources: WorkspaceNoteSourceDTO[]
  cronPlans: WorkspaceCronPlanDTO[]
}

export type TaskDTO = {
  id: string
  projectId: string
  title: string
  description: string
  status: string
  completed: boolean
  dueAt: string | null
  assigneeId: string | null
  tagId: string | null
  tag: string
  sourceInboxItemId: string | null
  createdAt: string
  updatedAt: string
}

export type SourceDTO = {
  id: string
  projectId: string
  kind: string
  title: string
  filePath: string
  createdAt: string
  updatedAt: string
}

export type CronPlanDTO = {
  id: string
  projectId: string
  title: string
  schedule: string
  nextRunAt: string | null
  enabled: boolean
  lastResult: string
  createdAt: string
  updatedAt: string
}

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

export type UpdateProjectInput = Partial<CreateProjectInput>

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

export type UpdateTaskInput = {
  title: string
  description?: string
  status?: string
  completed: boolean
  nextProjectId?: string
  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 fetchProjects(session: ApiSession) {
  return apiRequest<ProjectDTO[]>('/api/v1/projects', { token: session.token })
}

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

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

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

export async function updateProject(session: ApiSession, projectId: string, input: UpdateProjectInput) {
  return apiRequest<ProjectDTO>(`/api/v1/projects/${projectId}`, {
    method: 'PATCH',
    token: session.token,
    body: input,
  })
}

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

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

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

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

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