import apiClient from '../../../shared/services/apiClient';

export interface Task {
  id: number;
  title: string;
  description?: string;
  task_type: string;
  status: 'pending' | 'assigned' | 'in_progress' | 'completed' | 'cancelled' | 'overdue';
  priority: 'low' | 'medium' | 'high' | 'urgent';
  workflow_instance_id?: number;
  booking_id?: number;
  room_id?: number;
  assigned_to?: number;
  assigned_to_name?: string;
  created_by: number;
  created_by_name?: string;
  due_date?: string;
  completed_at?: string;
  estimated_duration_minutes?: number;
  actual_duration_minutes?: number;
  notes?: string;
  metadata?: Record<string, unknown>;
  comments?: TaskComment[];
  created_at: string;
  updated_at: string;
}

export interface TaskComment {
  id: number;
  task_id: number;
  user_id: number;
  user_name?: string;
  comment: string;
  created_at: string;
}

export interface CreateTaskRequest {
  title: string;
  description?: string;
  task_type?: string;
  priority?: string;
  workflow_instance_id?: number;
  booking_id?: number;
  room_id?: number;
  assigned_to?: number;
  due_date?: string;
  estimated_duration_minutes?: number;
  metadata?: Record<string, unknown>;
}

export interface UpdateTaskRequest {
  title?: string;
  description?: string;
  status?: string;
  priority?: string;
  assigned_to?: number;
  due_date?: string;
  notes?: string;
  actual_duration_minutes?: number;
}

export interface TaskStatistics {
  total: number;
  pending: number;
  assigned: number;
  in_progress: number;
  completed: number;
  overdue: number;
  cancelled: number;
  completion_rate: number;
  average_completion_time_minutes?: number;
}

const taskService = {
  // Task CRUD
  createTask: async (data: CreateTaskRequest) => {
    return apiClient.post<{ status: string; data: Task }>('/tasks/', data);
  },

  getTasks: async (params?: {
    assigned_to?: number;
    created_by?: number;
    status?: string;
    priority?: string;
    task_type?: string;
    booking_id?: number;
    room_id?: number;
    workflow_instance_id?: number;
    overdue_only?: boolean;
    skip?: number;
    limit?: number;
  }) => {
    return apiClient.get<{ status: string; data: Task[] }>('/tasks/', { params });
  },

  getMyTasks: async (status?: string) => {
    return apiClient.get<{ status: string; data: Task[] }>('/tasks/my-tasks', { params: { status } });
  },

  getTask: async (id: number) => {
    return apiClient.get<{ status: string; data: Task }>(`/tasks/${id}`);
  },

  updateTask: async (id: number, data: UpdateTaskRequest) => {
    return apiClient.put<{ status: string; data: Task }>(`/tasks/${id}`, data);
  },

  // Task Actions
  assignTask: async (taskId: number, userId: number) => {
    return apiClient.post<{ status: string; data: Task }>(`/tasks/${taskId}/assign`, { user_id: userId });
  },

  startTask: async (taskId: number) => {
    return apiClient.post<{ status: string; data: Task }>(`/tasks/${taskId}/start`);
  },

  completeTask: async (taskId: number, notes?: string) => {
    return apiClient.post<{ status: string; data: Task }>(`/tasks/${taskId}/complete`, { notes });
  },

  cancelTask: async (taskId: number, reason?: string) => {
    return apiClient.post<{ status: string; data: Task }>(`/tasks/${taskId}/cancel`, { reason });
  },

  // Task Comments
  addTaskComment: async (taskId: number, comment: string) => {
    return apiClient.post<{ status: string; data: TaskComment }>(`/tasks/${taskId}/comments`, { comment });
  },

  // Statistics
  getTaskStatistics: async (params?: {
    assigned_to?: number;
    start_date?: string;
    end_date?: string;
  }) => {
    return apiClient.get<{ status: string; data: TaskStatistics }>('/tasks/statistics/', { params });
  },
};

export default taskService;

