import api from './index'

export type TodoSource = 'ai_kanban' | 'ai_chat' | 'manual'

export interface Todo {
  id: string
  title: string
  is_completed: boolean
  source: TodoSource
  source_detail: string | null
  todo_date: string
  completed_at: string | null
  created_at: string
  updated_at: string
}

export interface TodoListResponse {
  items: Todo[]
  total: number
  page: number
  page_size: number
}

export interface TodoSummary {
  date: string
  total: number
  completed: number
  pending: number
}

export const todoApi = {
  list(date?: string, page = 1, pageSize = 50) {
    return api.get<TodoListResponse>('/api/todos', {
      params: date ? { date_str: date, page, page_size: pageSize } : { page, page_size: pageSize },
    })
  },

  create(title: string) {
    return api.post<Todo>('/api/todos', { title })
  },

  update(id: string, data: { title?: string; is_completed?: boolean }) {
    return api.patch<Todo>(`/api/todos/${id}`, data)
  },

  delete(id: string) {
    return api.delete(`/api/todos/${id}`)
  },

  aiGenerate() {
    return api.post<TodoListResponse>('/api/todos/ai-generate')
  },

  summary(date?: string) {
    return api.get<TodoSummary>('/api/todos/summary', {
      params: date ? { date_str: date } : {},
    })
  },
}
