import Router from '@koa/router';
import { templatesService } from './templates.service';

const router = new Router();

/**
 * 获取模板分类列表
 * GET /api/templates
 */
router.get('/', async (ctx) => {
  try {
    const { category } = ctx.query as { category?: string };

    let templates;
    if (category) {
      templates = templatesService.getTemplatesByCategory(category);
    } else {
      templates = templatesService.getTemplates();
    }

    ctx.body = {
      code: 0,
      message: 'success',
      data: templates,
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '获取模板列表失败',
    };
  }
});

/**
 * 获取模板分类
 * GET /api/templates/categories
 */
router.get('/categories', async (ctx) => {
  try {
    const categories = templatesService.getTemplateCategories();
    ctx.body = {
      code: 0,
      message: 'success',
      data: categories,
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '获取分类失败',
    };
  }
});

/**
 * 获取单个模板
 * GET /api/templates/:id
 */
router.get('/:id', async (ctx) => {
  try {
    const { id } = ctx.params;
    const template = templatesService.getTemplateById(parseInt(id));

    if (!template) {
      ctx.body = {
        code: 404,
        message: '模板不存在',
      };
      return;
    }

    ctx.body = {
      code: 0,
      message: 'success',
      data: template,
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '获取模板失败',
    };
  }
});

/**
 * 创建模板
 * POST /api/templates
 */
router.post('/', async (ctx) => {
  try {
    const { name, category, content, description } = ctx.request.body as {
      name: string;
      category: string;
      content: string;
      description?: string;
    };

    if (!name || !category || !content) {
      ctx.body = {
        code: 400,
        message: '缺少必填字段',
      };
      return;
    }

    const template = templatesService.createTemplate({ name, category, content, description });
    ctx.body = {
      code: 0,
      message: 'success',
      data: template,
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '创建模板失败',
    };
  }
});

/**
 * 更新模板
 * PUT /api/templates/:id
 */
router.put('/:id', async (ctx) => {
  try {
    const { id } = ctx.params;
    const { name, category, content, description } = ctx.request.body as {
      name?: string;
      category?: string;
      content?: string;
      description?: string;
    };

    const template = templatesService.updateTemplate(parseInt(id), { name, category, content, description });

    if (!template) {
      ctx.body = {
        code: 404,
        message: '模板不存在',
      };
      return;
    }

    ctx.body = {
      code: 0,
      message: 'success',
      data: template,
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '更新模板失败',
    };
  }
});

/**
 * 删除模板
 * DELETE /api/templates/:id
 */
router.delete('/:id', async (ctx) => {
  try {
    const { id } = ctx.params;
    const success = templatesService.deleteTemplate(parseInt(id));

    if (!success) {
      ctx.body = {
        code: 404,
        message: '模板不存在',
      };
      return;
    }

    ctx.body = {
      code: 0,
      message: 'success',
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '删除模板失败',
    };
  }
});

export default router;