import { ref } from "vue"; import { defineStore } from "pinia"; import { apiGet, apiPost, apiPut, apiPatch, apiDelete } from "@/api/client"; import { useToastStore } from "@/stores/toast"; import type { Task, TaskStatus, TaskPriority, StartPlanningResult } from "@/types/task"; // Single-task + mutation surface. The list/filter/sort/pagination surface that // backed the removed /tasks list view was dropped in the 2026-06-02 drift-audit // cleanup (no consumers — ProjectView's kanban keeps its own local task list). export const useTasksStore = defineStore("tasks", () => { const currentTask = ref(null); const loading = ref(false); async function fetchTask(id: number) { loading.value = true; try { currentTask.value = await apiGet(`/api/tasks/${id}`); } catch (e) { useToastStore().show("Failed to load task", "error"); throw e; } finally { loading.value = false; } } async function createTask(data: { title: string; body: string; tags?: string[]; status?: TaskStatus; priority?: TaskPriority; due_date?: string | null; project_id?: number | null; milestone_id?: number | null; parent_id?: number | null; recurrence_rule?: Record | null; }): Promise { try { return await apiPost("/api/tasks", data); } catch (e) { useToastStore().show("Failed to create task", "error"); throw e; } } async function updateTask( id: number, data: Partial< Pick > ): Promise { try { const task = await apiPut(`/api/tasks/${id}`, data); if (currentTask.value?.id === id) { currentTask.value = task; } return task; } catch (e) { useToastStore().show("Failed to update task", "error"); throw e; } } async function patchStatus(id: number, status: TaskStatus): Promise { try { const task = await apiPatch(`/api/tasks/${id}/status`, { status }); if (currentTask.value?.id === id) { currentTask.value = task; } return task; } catch (e) { useToastStore().show("Failed to update task status", "error"); throw e; } } async function deleteTask(id: number) { try { await apiDelete(`/api/tasks/${id}`); if (currentTask.value?.id === id) { currentTask.value = null; } } catch (e) { useToastStore().show("Failed to delete task", "error"); throw e; } } async function startPlanning( projectId: number, title: string ): Promise { try { return await apiPost("/api/tasks/planning", { project_id: projectId, title, }); } catch (e) { useToastStore().show("Failed to start planning", "error"); throw e; } } return { currentTask, loading, fetchTask, createTask, updateTask, patchStatus, deleteTask, startPlanning, }; });