import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
import { z } from 'zod';
import { ErrorSchema } from '@/server/utils/errorHandler';
import { AppDataSource } from '@/server/data-source';
import { SystemSettingService } from '@/server/modules/settings/system-setting.service';

// 响应Schema
const RegisterStatusResponse = z.object({
  enabled: z.boolean().openapi({
    description: '首页注册功能是否启用',
    example: true
  })
});

// 路由定义
const routeDef = createRoute({
  method: 'get',
  path: '/register-status',
  responses: {
    200: {
      description: '成功获取首页注册状态',
      content: { 'application/json': { schema: RegisterStatusResponse } }
    },
    500: {
      description: '服务器错误',
      content: { 'application/json': { schema: ErrorSchema } }
    }
  }
});

// 路由实现
const app = new OpenAPIHono().openapi(routeDef, async (c) => {
  try {
    const service = new SystemSettingService(AppDataSource);
    const enabled = await service.getBooleanValue('home_register_enabled');
    
    return c.json({ enabled }, 200);
  } catch (error) {
    const { code = 500, message = '获取注册状态失败' } = error as Error & { code?: number };
    return c.json({ code, message }, code);
  }
});

export default app;