import api from './api';

export type TaskStatus = 'PENDING' | 'IN_PROGRESS' | 'COMPLETED' | 'BLOCKED' | 'OVERDUE';

export interface Task {
    id: string;
    title: string;
    description?: string;
    status: TaskStatus;
    priority: string;
    assigneeId?: string;
    roomId?: string;
    batchId?: string;
    dueDate?: string;
    completedAt?: string;
    notes?: string;
    photos?: string[];
    assignee?: { id: string; name: string };
    room?: { name: string; type: string };
    batch?: { name: string; strain: string };
}

export interface CreateTaskData {
    title: string;
    description?: string;
    priority?: string;
    assigneeId?: string;
    roomId?: string;
    batchId?: string;
    dueDate: string;
    notes?: string;
    templateId?: string;
}

export const tasksApi = {
    getAll: async (filters?: { status?: TaskStatus; assigneeId?: string; roomId?: string; batchId?: string; startDate?: string; endDate?: string }) => {
        const response = await api.get<Task[]>('/tasks', { params: filters });
        return response.data;
    },

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

    update: async (id: string, data: Partial<CreateTaskData>) => {
        const response = await api.patch<Task>(`/tasks/${id}`, data);
        return response.data;
    },

    complete: async (id: string, result: { notes?: string; photos?: string[] }) => {
        const response = await api.post<Task>(`/tasks/${id}/complete`, result);
        return response.data;
    },

    delete: async (id: string) => {
        await api.delete(`/tasks/${id}`);
    }
};
