"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const router_1 = __importDefault(require("@koa/router"));
const templates_service_1 = require("./templates.service");
const router = new router_1.default();
/**
 * 获取模板分类列表
 * GET /api/templates
 */
router.get('/', async (ctx) => {
    try {
        const { category } = ctx.query;
        let templates;
        if (category) {
            templates = templates_service_1.templatesService.getTemplatesByCategory(category);
        }
        else {
            templates = templates_service_1.templatesService.getTemplates();
        }
        ctx.body = {
            code: 0,
            message: 'success',
            data: templates,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '获取模板列表失败',
        };
    }
});
/**
 * 获取模板分类
 * GET /api/templates/categories
 */
router.get('/categories', async (ctx) => {
    try {
        const categories = templates_service_1.templatesService.getTemplateCategories();
        ctx.body = {
            code: 0,
            message: 'success',
            data: categories,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '获取分类失败',
        };
    }
});
/**
 * 获取单个模板
 * GET /api/templates/:id
 */
router.get('/:id', async (ctx) => {
    try {
        const { id } = ctx.params;
        const template = templates_service_1.templatesService.getTemplateById(parseInt(id));
        if (!template) {
            ctx.body = {
                code: 404,
                message: '模板不存在',
            };
            return;
        }
        ctx.body = {
            code: 0,
            message: 'success',
            data: template,
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '获取模板失败',
        };
    }
});
/**
 * 创建模板
 * POST /api/templates
 */
router.post('/', async (ctx) => {
    try {
        const { name, category, content, description } = ctx.request.body;
        if (!name || !category || !content) {
            ctx.body = {
                code: 400,
                message: '缺少必填字段',
            };
            return;
        }
        const template = templates_service_1.templatesService.createTemplate({ name, category, content, description });
        ctx.body = {
            code: 0,
            message: 'success',
            data: template,
        };
    }
    catch (error) {
        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;
        const template = templates_service_1.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) {
        ctx.body = {
            code: 400,
            message: error.message || '更新模板失败',
        };
    }
});
/**
 * 删除模板
 * DELETE /api/templates/:id
 */
router.delete('/:id', async (ctx) => {
    try {
        const { id } = ctx.params;
        const success = templates_service_1.templatesService.deleteTemplate(parseInt(id));
        if (!success) {
            ctx.body = {
                code: 404,
                message: '模板不存在',
            };
            return;
        }
        ctx.body = {
            code: 0,
            message: 'success',
        };
    }
    catch (error) {
        ctx.body = {
            code: 400,
            message: error.message || '删除模板失败',
        };
    }
});
exports.default = router;
