import { defineMock } from '@umijs/max';

const tasks: any[] = [];
for (let i = 0; i < 20; i += 1) {
  tasks.push({
    id: `${i}`,
    title: `Task Name ${i}`,
    status: i % 3 === 0 ? 'todo' : i % 3 === 1 ? 'doing' : 'done',
    description: 'This is a description for the task.',
    priority: Math.floor(Math.random() * 3), // 0, 1, 2
    createdAt: new Date().toISOString(),
  });
}

export default defineMock({
  'GET /api/demo/tasks': (req, res) => {
    const { keyword, current = 1, pageSize = 20 } = req.query as any;
    let dataSource = [...tasks].slice().reverse();

    if (keyword) {
      dataSource = dataSource.filter((item) => item.title.includes(keyword));
    }

    const start = (current - 1) * pageSize;
    const end = start + pageSize;

    setTimeout(() => {
      res.json({
        success: true,
        data: dataSource.slice(start, end),
        total: dataSource.length,
      });
    }, 500);
  },
  'POST /api/demo/tasks': (req, res) => {
    res.json({ success: true, data: { ...req.body, id: `${tasks.length}` } });
  },
  'PUT /api/demo/tasks': (req, res) => {
    res.json({ success: true });
  },
  'DELETE /api/demo/tasks': (req, res) => {
    res.json({ success: true });
  },
});
