import { api } from '../api'

export interface Task {
  id: string
  taskNumber: string
  projectId?: string
  project?: any
  phaseId?: string
  title: string
  description?: string
  assignedToId?: string
  assignedTo?: any
  priority: string // LOW, MEDIUM, HIGH, CRITICAL
  status: string // PENDING, IN_PROGRESS, REVIEW, COMPLETED, CANCELLED
  progress: number
  startDate?: string
  dueDate?: string
  completedAt?: string
  estimatedHours?: number
  actualHours?: number
  tags?: string[]
  createdAt: string
  updatedAt: string
}

export interface CreateTaskData {
  projectId?: string
  title: string
  description?: string
  assignedToId?: string
  priority?: string
  status?: string
  progress?: number
  startDate?: string
  dueDate?: string
  estimatedHours?: number
  tags?: string[]
}

export interface UpdateTaskData extends Partial<CreateTaskData> {
  progress?: number
  completedAt?: string
  actualHours?: number
}

export interface TaskFilters {
  search?: string
  projectId?: string
  assignedToId?: string
  priority?: string
  status?: string
  page?: number
  pageSize?: number
}

export interface TasksResponse {
  tasks: Task[]
  total: number
  page: number
  pageSize: number
  totalPages: number
}

export const tasksAPI = {
  // Get all tasks with filters and pagination
  getAll: async (filters: TaskFilters = {}): Promise<TasksResponse> => {
    const params = new URLSearchParams()
    if (filters.search) params.append('search', filters.search)
    if (filters.projectId) params.append('projectId', filters.projectId)
    if (filters.assignedToId) params.append('assignedToId', filters.assignedToId)
    if (filters.priority) params.append('priority', filters.priority)
    if (filters.status) params.append('status', filters.status)
    if (filters.page) params.append('page', filters.page.toString())
    if (filters.pageSize) params.append('pageSize', filters.pageSize.toString())

    const response = await api.get(`/projects/tasks?${params.toString()}`)
    const { data, pagination } = response.data
    return {
      tasks: data || [],
      total: pagination?.total || 0,
      page: pagination?.page || 1,
      pageSize: pagination?.pageSize || 20,
      totalPages: pagination?.totalPages || 0,
    }
  },

  // Get single task by ID
  getById: async (id: string): Promise<Task> => {
    const response = await api.get(`/projects/tasks/${id}`)
    return response.data.data
  },

  // Create new task
  create: async (data: CreateTaskData): Promise<Task> => {
    const response = await api.post('/projects/tasks', data)
    return response.data.data
  },

  // Update existing task
  update: async (id: string, data: UpdateTaskData): Promise<Task> => {
    const response = await api.put(`/projects/tasks/${id}`, data)
    return response.data.data
  },

  // Delete task
  delete: async (id: string): Promise<void> => {
    await api.delete(`/projects/tasks/${id}`)
  }
}

// Projects API
export interface Project {
  id: string
  projectNumber: string
  name: string
  nameAr?: string
  description?: string
  type?: string
  status: string
  priority?: string
  progress?: number
  startDate?: string
  endDate?: string
  budget?: number
  estimatedCost?: number
  createdAt: string
  updatedAt: string
}

export interface CreateProjectData {
  name: string
  description?: string
  type: string
  startDate: string
  endDate?: string
  estimatedCost?: number
  status?: string
  priority?: string
  progress?: number
}

export interface UpdateProjectData extends Partial<CreateProjectData> {}

export const projectsAPI = {
  getAll: async (): Promise<Project[]> => {
    const response = await api.get('/projects/projects')
    return response.data.data
  },

  create: async (data: CreateProjectData): Promise<Project> => {
    const response = await api.post('/projects/projects', data)
    return response.data.data
  },

  update: async (id: string, data: UpdateProjectData): Promise<Project> => {
    const response = await api.put(`/projects/projects/${id}`, data)
    return response.data.data
  },

  delete: async (id: string): Promise<void> => {
    await api.delete(`/projects/projects/${id}`)
  },
}
