import { createRoute, OpenAPIHono } from '@hono/zod-openapi'
import { ErrorSchema } from '../../../utils/errorHandler'
import { authMiddleware } from '../../../middleware/auth.middleware'
import { AuthContext } from '../../../types/context'
import { UserSchema } from '../../../modules/users/user.schema'

const UserResponseSchema = UserSchema.omit({
  password: true
});

const routeDef = createRoute({
  method: 'get',
  path: '/me',
  middleware: authMiddleware,
  responses: {
    200: {
      description: '获取当前用户信息成功',
      content: {
        'application/json': {
          schema: UserResponseSchema
        }
      }
    },
    401: {
      description: '未授权',
      content: {
        'application/json': {
          schema: ErrorSchema
        }
      }
    }
  }
})

const app = new OpenAPIHono<AuthContext>().openapi(routeDef, (c) => {
  const user = c.get('user')
  return c.json(user, 200)
})

export default app