const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');

// Swagger 配置选项
const options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: '结伴客系统 API',
      version: '1.0.0',
      description: '结伴客系统 - 旅行结伴与动物认领平台',
      contact: {
        name: '技术支持',
        email: 'support@jiebanke.com',
        url: 'https://www.jiebanke.com'
      },
      license: {
        name: 'MIT',
        url: 'https://opensource.org/licenses/MIT'
      }
    },
    servers: [
      {
        url: 'https://webapi.jiebanke.com/api/v1',
        description: '开发环境'
      },
      {
        url: 'https://webapi.jiebanke.com/api/v1',
        description: '生产环境'
      }
    ],
    components: {
      securitySchemes: {
        BearerAuth: {
          type: 'http',
          scheme: 'bearer',
          bearerFormat: 'JWT'
        }
      },
      schemas: {
        // 通用响应模型
        ApiResponse: {
          type: 'object',
          properties: {
            success: {
              type: 'boolean',
              description: '请求是否成功'
            },
            code: {
              type: 'integer',
              description: '状态码'
            },
            message: {
              type: 'string',
              description: '消息描述'
            },
            data: {
              type: 'object',
              description: '业务数据'
            },
            timestamp: {
              type: 'string',
              format: 'date-time',
              description: '响应时间戳'
            }
          }
        },
        // 错误响应模型
        ErrorResponse: {
          type: 'object',
          properties: {
            success: {
              type: 'boolean',
              example: false
            },
            code: {
              type: 'integer',
              example: 400
            },
            message: {
              type: 'string',
              example: '请求参数错误'
            },
            error: {
              type: 'string',
              example: '详细错误信息'
            },
            timestamp: {
              type: 'string',
              format: 'date-time'
            }
          }
        },
        // 管理员模型
        Admin: {
          type: 'object',
          properties: {
            id: {
              type: 'integer',
              example: 1
            },
            username: {
              type: 'string',
              example: 'admin'
            },
            email: {
              type: 'string',
              example: 'admin@jiebanke.com'
            },
            nickname: {
              type: 'string',
              example: '管理员'
            },
            avatar: {
              type: 'string',
              example: 'https://www.jiebanke.com/avatar.jpg'
            },
            role: {
              type: 'string',
              example: 'super_admin'
            },
            status: {
              type: 'string',
              example: 'active'
            },
            last_login: {
              type: 'string',
              format: 'date-time',
              example: '2023-01-01T00:00:00Z'
            },
            created_at: {
              type: 'string',
              format: 'date-time',
              example: '2023-01-01T00:00:00Z'
            },
            updated_at: {
              type: 'string',
              format: 'date-time',
              example: '2023-01-01T00:00:00Z'
            }
          }
        },
        // 系统统计数据模型
        SystemStats: {
          type: 'object',
          properties: {
            userCount: {
              type: 'integer',
              description: '用户总数'
            },
            merchantCount: {
              type: 'integer',
              description: '商家数量'
            },
            travelCount: {
              type: 'integer',
              description: '旅行计划数'
            },
            animalCount: {
              type: 'integer',
              description: '动物数量'
            },
            orderCount: {
              type: 'integer',
              description: '订单总数'
            },
            todayUserCount: {
              type: 'integer',
              description: '今日新增用户数'
            },
            todayOrderCount: {
              type: 'integer',
              description: '今日新增订单数'
            }
          }
        },
        // 用户统计数据模型
        UserStats: {
          type: 'object',
          properties: {
            total: {
              type: 'integer',
              description: '用户总数'
            },
            byType: {
              type: 'array',
              description: '按类型统计',
              items: {
                type: 'object',
                properties: {
                  user_type: {
                    type: 'string'
                  },
                  count: {
                    type: 'integer'
                  }
                }
              }
            },
            byDate: {
              type: 'array',
              description: '按日期统计',
              items: {
                type: 'object',
                properties: {
                  date: {
                    type: 'string'
                  },
                  count: {
                    type: 'integer'
                  }
                }
              }
            }
          }
        },
        // 订单统计数据模型
        OrderStats: {
          type: 'object',
          properties: {
            total: {
              type: 'integer',
              description: '订单总数'
            },
            totalAmount: {
              type: 'number',
              description: '订单总金额'
            },
            byStatus: {
              type: 'array',
              description: '按状态统计',
              items: {
                type: 'object',
                properties: {
                  status: {
                    type: 'string'
                  },
                  count: {
                    type: 'integer'
                  }
                }
              }
            },
            byDate: {
              type: 'array',
              description: '按日期统计',
              items: {
                type: 'object',
                properties: {
                  date: {
                    type: 'string'
                  },
                  count: {
                    type: 'integer'
                  },
                  amount: {
                    type: 'number'
                  }
                }
              }
            }
          }
        },
        // 系统信息模型
        SystemInfo: {
          type: 'object',
          properties: {
            nodeVersion: {
              type: 'string',
              description: 'Node.js版本'
            },
            platform: {
              type: 'string',
              description: '运行平台'
            },
            arch: {
              type: 'string',
              description: '系统架构'
            },
            uptime: {
              type: 'integer',
              description: '运行时间(秒)'
            },
            databaseVersion: {
              type: 'string',
              description: '数据库版本'
            }
          }
        }
      }
    },
    security: [
      {
        BearerAuth: []
      }
    ]
  },
  apis: [
    './src/routes/*.js',
    './src/docs/*.js'
  ]
};

module.exports = swaggerJsdoc(options);