import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
import { z } from 'zod';
import { AppDataSource } from '@d8d/shared-utils';
import { PaymentService } from '../../services/payment.service.js';

// 支付状态查询路由定义
const paymentStatusRoute = createRoute({
  method: 'get',
  path: '/',
  request: {
    query: z.object({
      orderId: z.string().transform(val => parseInt(val)).pipe(z.number().int().positive())
    })
  },
  responses: {
    200: {
      description: '支付状态查询成功',
      content: { 'application/json': { schema: z.object({ paymentStatus: z.string() }) } }
    },
    400: {
      description: '参数错误',
      content: { 'application/json': { schema: z.object({ message: z.string() }) } }
    },
    500: {
      description: '服务器错误',
      content: { 'application/json': { schema: z.object({ message: z.string() }) } }
    }
  }
});

const app = new OpenAPIHono()
  .openapi(paymentStatusRoute, async (c) => {
    try {
      const { orderId } = c.req.valid('query');

      // 创建支付服务实例
      const paymentService = new PaymentService(AppDataSource);
      const paymentStatus = await paymentService.getPaymentStatus(orderId);

      return c.json({
        paymentStatus
      }, 200);
    } catch (error) {
      console.error('支付状态查询失败:', error);
      return c.json({
        message: error instanceof Error ? error.message : '支付状态查询失败'
      }, 500);
    }
  });

export default app;