import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
import { z } from 'zod';
import { AppDataSource } from '@/server/data-source';
import { TemplateService } from '@/server/modules/templates/template.service';

// 路由定义
const categoriesRoute = createRoute({
  method: 'get',
  path: '/categories',
  responses: {
    200: {
      description: '成功获取分类列表',
      content: { 
        'application/json': { 
          schema: z.array(z.string().openapi({ description: '分类名称', example: '合同' }))
        } 
      }
    },
    500: {
      description: '服务器错误',
      content: { 'application/json': { schema: z.object({ code: z.number(), message: z.string() }) } }
    }
  }
});

// 路由实现
const app = new OpenAPIHono().openapi(categoriesRoute, async (c) => {
  try {
    const service = new TemplateService(AppDataSource);
    const categories = await service.getCategories();
    return c.json(categories, 200);
  } catch (error) {
    const message = error instanceof Error ? error.message : '获取分类列表失败';
    return c.json({ code: 500, message }, 500);
  }
});

export default app;