import type { APIRoute } from 'astro'; import { readFile, writeFile } from 'fs/promises'; import { join } from 'path'; const TASKS_PATH = join(process.cwd(), '..', 'palaces', 'manglasabang', 'palaceappsdata', 'palacetasks', 'tasks.json'); interface Task { id: string; text: string; done: boolean; created: string; } async function load(): Promise { try { return JSON.parse(await readFile(TASKS_PATH, 'utf-8')); } catch { return []; } } async function save(tasks: Task[]) { await writeFile(TASKS_PATH, JSON.stringify(tasks, null, 2) + '\n'); } export const GET: APIRoute = async () => { return new Response(JSON.stringify(await load()), { headers: { 'Content-Type': 'application/json' }, }); }; export const POST: APIRoute = async ({ request }) => { const body = await request.json(); const tasks = await load(); if (body.action === 'add') { tasks.push({ id: String(Date.now()), text: body.text, done: false, created: new Date().toISOString(), }); } else if (body.action === 'toggle') { const t = tasks.find(t => t.id === body.id); if (t) t.done = !t.done; } else if (body.action === 'delete') { const idx = tasks.findIndex(t => t.id === body.id); if (idx !== -1) tasks.splice(idx, 1); } await save(tasks); return new Response(JSON.stringify(tasks), { headers: { 'Content-Type': 'application/json' }, }); };