const swaggerJSDoc = require('swagger-jsdoc');

const swaggerDefinition = {
  openapi: '3.0.0',
  info: {
    title: '保险端口系统 API',
    version: '1.0.0',
    description: '保险端口系统后端API文档',
    contact: {
      name: '技术支持',
      email: 'support@insurance.com'
    },
    license: {
      name: 'MIT',
      url: 'https://opensource.org/licenses/MIT'
    }
  },
  servers: [
    {
      url: 'http://localhost:3000',
      description: '开发环境服务器'
    },
    {
      url: 'https://api.insurance.com',
      description: '生产环境服务器'
    }
  ],
  components: {
    securitySchemes: {
      bearerAuth: {
        type: 'http',
        scheme: 'bearer',
        bearerFormat: 'JWT',
        description: 'JWT令牌认证'
      },
      fixedTokenAuth: {
        type: 'apiKey',
        in: 'header',
        name: 'Authorization',
        description: '固定令牌认证，格式：Bearer <fixed_token>'
      }
    },
    schemas: {
      User: {
        type: 'object',
        properties: {
          id: { type: 'integer', description: '用户ID' },
          username: { type: 'string', description: '用户名' },
          email: { type: 'string', description: '邮箱' },
          phone: { type: 'string', description: '手机号' },
          status: { type: 'string', enum: ['active', 'inactive'], description: '用户状态' },
          fixed_token: { type: 'string', description: '固定令牌（用于API访问验证）', nullable: true },
          createdAt: { type: 'string', format: 'date-time', description: '创建时间' },
          updatedAt: { type: 'string', format: 'date-time', description: '更新时间' }
        }
      },
      Menu: {
        type: 'object',
        properties: {
          id: {
            type: 'integer',
            description: '菜单ID'
          },
          name: {
            type: 'string',
            description: '菜单名称'
          },
          key: {
            type: 'string',
            description: '菜单唯一标识'
          },
          path: {
            type: 'string',
            description: '路由路径'
          },
          icon: {
            type: 'string',
            description: '菜单图标'
          },
          parent_id: {
            type: 'integer',
            description: '父菜单ID'
          },
          component: {
            type: 'string',
            description: '组件路径'
          },
          order: {
            type: 'integer',
            description: '排序号'
          },
          status: {
            type: 'string',
            enum: ['active', 'inactive'],
            description: '菜单状态'
          },
          show: {
            type: 'boolean',
            description: '是否显示'
          },
          children: {
            type: 'array',
            items: {
              $ref: '#/components/schemas/Menu'
            },
            description: '子菜单列表'
          },
          created_at: {
            type: 'string',
            format: 'date-time',
            description: '创建时间'
          },
          updated_at: {
            type: 'string',
            format: 'date-time',
            description: '更新时间'
          }
        }
      },
      InsuranceApplication: {
        type: 'object',
        properties: {
          id: { type: 'integer', description: '申请ID' },
          applicantName: { type: 'string', description: '申请人姓名' },
          insuranceType: { type: 'string', description: '保险类型' },
          status: { type: 'string', enum: ['pending', 'approved', 'rejected'], description: '申请状态' },
          amount: { type: 'number', format: 'float', description: '保险金额' },
          createdAt: { type: 'string', format: 'date-time', description: '创建时间' }
        }
      },
      Policy: {
        type: 'object',
        properties: {
          id: { type: 'integer', description: '保单ID' },
          policyNumber: { type: 'string', description: '保单号' },
          insuranceType: { type: 'string', description: '保险类型' },
          premium: { type: 'number', format: 'float', description: '保费' },
          status: { type: 'string', enum: ['active', 'expired', 'cancelled'], description: '保单状态' },
          startDate: { type: 'string', format: 'date', description: '生效日期' },
          endDate: { type: 'string', format: 'date', description: '到期日期' }
        }
      },
      Claim: {
        type: 'object',
        properties: {
          id: { type: 'integer', description: '理赔ID' },
          claimNumber: { type: 'string', description: '理赔单号' },
          policyId: { type: 'integer', description: '关联保单ID' },
          amount: { type: 'number', format: 'float', description: '理赔金额' },
          status: { type: 'string', enum: ['pending', 'approved', 'paid', 'rejected'], description: '理赔状态' },
          createdAt: { type: 'string', format: 'date-time', description: '创建时间' }
        }
      },
      LivestockType: {
        type: 'object',
        properties: {
          id: { type: 'integer', description: '牲畜类型ID' },
          name: { type: 'string', description: '牲畜类型名称' },
          description: { type: 'string', description: '牲畜类型描述' },
          base_value: { type: 'number', format: 'float', description: '基础价值（单头）' },
          premium_rate: { type: 'number', format: 'float', description: '基础保费费率' },
          is_active: { type: 'boolean', description: '是否启用' },
          created_at: { type: 'string', format: 'date-time', description: '创建时间' },
          updated_at: { type: 'string', format: 'date-time', description: '更新时间' }
        }
      },
      LivestockPolicy: {
        type: 'object',
        properties: {
          id: { type: 'integer', description: '保单ID' },
          policy_no: { type: 'string', description: '保单编号' },
          farmer_name: { type: 'string', description: '农户姓名' },
          farmer_phone: { type: 'string', description: '农户电话' },
          farmer_id_card: { type: 'string', description: '农户身份证号' },
          farmer_address: { type: 'string', description: '农户地址' },
          livestock_type_id: { type: 'integer', description: '牲畜类型ID' },
          livestock_count: { type: 'integer', description: '牲畜数量' },
          unit_value: { type: 'number', format: 'float', description: '单头价值' },
          total_value: { type: 'number', format: 'float', description: '总保额' },
          premium_rate: { type: 'number', format: 'float', description: '保费费率' },
          premium_amount: { type: 'number', format: 'float', description: '保费金额' },
          start_date: { type: 'string', format: 'date', description: '保险开始日期' },
          end_date: { type: 'string', format: 'date', description: '保险结束日期' },
          policy_status: { type: 'string', enum: ['draft', 'active', 'expired', 'cancelled'], description: '保单状态' },
          payment_status: { type: 'string', enum: ['unpaid', 'paid', 'partial'], description: '支付状态' },
          payment_date: { type: 'string', format: 'date-time', description: '支付时间' },
          notes: { type: 'string', description: '备注' },
          created_at: { type: 'string', format: 'date-time', description: '创建时间' },
          updated_at: { type: 'string', format: 'date-time', description: '更新时间' }
        }
      },
      LivestockClaim: {
        type: 'object',
        properties: {
          id: { type: 'integer', description: '理赔ID' },
          claim_no: { type: 'string', description: '理赔编号' },
          policy_id: { type: 'integer', description: '保单ID' },
          claim_type: { type: 'string', enum: ['death', 'disease', 'accident', 'theft', 'natural_disaster'], description: '理赔类型' },
          incident_date: { type: 'string', format: 'date', description: '事故发生日期' },
          report_date: { type: 'string', format: 'date', description: '报案日期' },
          affected_count: { type: 'integer', description: '受损数量' },
          claim_amount: { type: 'number', format: 'float', description: '申请理赔金额' },
          approved_amount: { type: 'number', format: 'float', description: '批准理赔金额' },
          incident_description: { type: 'string', description: '事故描述' },
          evidence_files: { type: 'string', description: '证据文件（JSON格式）' },
          claim_status: { type: 'string', enum: ['pending', 'investigating', 'approved', 'rejected', 'paid'], description: '理赔状态' },
          review_notes: { type: 'string', description: '审核备注' },
          reviewed_at: { type: 'string', format: 'date-time', description: '审核时间' },
          payment_status: { type: 'string', enum: ['unpaid', 'paid'], description: '支付状态' },
          payment_date: { type: 'string', format: 'date-time', description: '支付时间' },
          payment_method: { type: 'string', description: '支付方式' },
          payment_reference: { type: 'string', description: '支付凭证号' },
          created_at: { type: 'string', format: 'date-time', description: '创建时间' },
          updated_at: { type: 'string', format: 'date-time', description: '更新时间' }
        }
      },
      InsuranceType: {
        type: 'object',
        properties: {
          id: { type: 'integer', description: '险种ID' },
          name: { type: 'string', description: '险种名称' },
          description: { type: 'string', description: '险种描述' },
          applicable_livestock: { type: 'string', description: '适用牲畜类型' },
          insurance_term: { type: 'integer', description: '保险期限（月）' },
          policy_form: { type: 'string', description: '保单形式' },
          coverage_amount_min: { type: 'number', format: 'decimal', description: '最低保额' },
          coverage_amount_max: { type: 'number', format: 'decimal', description: '最高保额' },
          premium_rate: { type: 'number', format: 'decimal', description: '保险费率' },
          service_area: { type: 'string', description: '服务区域' },
          add_time: { type: 'string', format: 'date-time', description: '添加时间' },
          on_sale_status: { type: 'boolean', description: '在售状态' },
          sort_order: { type: 'integer', description: '排序顺序' },
          remarks: { type: 'string', description: '备注' },
          status: { type: 'string', enum: ['active', 'inactive'], description: '险种状态' },
          created_by: { type: 'integer', description: '创建人ID' },
          updated_by: { type: 'integer', description: '更新人ID' },
          created_at: { type: 'string', format: 'date-time', description: '创建时间' },
          updated_at: { type: 'string', format: 'date-time', description: '更新时间' }
        }
      },
      CreateInsuranceTypeRequest: {
        type: 'object',
        required: ['name', 'coverage_amount_min', 'coverage_amount_max', 'premium_rate'],
        properties: {
          name: { type: 'string', description: '险种名称', minLength: 1, maxLength: 100 },
          description: { type: 'string', description: '险种描述' },
          applicable_livestock: { type: 'string', description: '适用牲畜类型', maxLength: 100 },
          insurance_term: { type: 'integer', description: '保险期限（月）', minimum: 1 },
          policy_form: { type: 'string', description: '保单形式', maxLength: 50 },
          coverage_amount_min: { type: 'number', format: 'decimal', description: '最低保额', minimum: 0 },
          coverage_amount_max: { type: 'number', format: 'decimal', description: '最高保额', minimum: 0 },
          premium_rate: { type: 'number', format: 'decimal', description: '保险费率', minimum: 0, maximum: 1 },
          service_area: { type: 'string', description: '服务区域' },
          add_time: { type: 'string', format: 'date-time', description: '添加时间' },
          on_sale_status: { type: 'boolean', description: '在售状态', default: true },
          sort_order: { type: 'integer', description: '排序顺序', default: 0 },
          remarks: { type: 'string', description: '备注' },
          status: { type: 'string', enum: ['active', 'inactive'], description: '险种状态', default: 'active' }
        }
      },
      UpdateInsuranceTypeRequest: {
        type: 'object',
        properties: {
          name: { type: 'string', description: '险种名称', minLength: 1, maxLength: 100 },
          description: { type: 'string', description: '险种描述' },
          applicable_livestock: { type: 'string', description: '适用牲畜类型', maxLength: 100 },
          insurance_term: { type: 'integer', description: '保险期限（月）', minimum: 1 },
          policy_form: { type: 'string', description: '保单形式', maxLength: 50 },
          coverage_amount_min: { type: 'number', format: 'decimal', description: '最低保额', minimum: 0 },
          coverage_amount_max: { type: 'number', format: 'decimal', description: '最高保额', minimum: 0 },
          premium_rate: { type: 'number', format: 'decimal', description: '保险费率', minimum: 0, maximum: 1 },
          service_area: { type: 'string', description: '服务区域' },
          add_time: { type: 'string', format: 'date-time', description: '添加时间' },
          on_sale_status: { type: 'boolean', description: '在售状态' },
          sort_order: { type: 'integer', description: '排序顺序' },
          remarks: { type: 'string', description: '备注' },
          status: { type: 'string', enum: ['active', 'inactive'], description: '险种状态' }
        }
      },
      UpdateInsuranceTypeStatusRequest: {
        type: 'object',
        properties: {
          status: { type: 'string', enum: ['active', 'inactive'], description: '险种状态' },
          on_sale_status: { type: 'boolean', description: '在售状态' }
        }
      },
      Pagination: {
        type: 'object',
        properties: {
          page: { type: 'integer', description: '当前页码' },
          limit: { type: 'integer', description: '每页数量' },
          total: { type: 'integer', description: '总记录数' }
        }
      },
      Error: {
        type: 'object',
        properties: {
          code: { type: 'integer', description: '错误码' },
          status: { type: 'string', description: '错误状态' },
          message: { type: 'string', description: '错误信息' },
          timestamp: { type: 'string', format: 'date-time', description: '时间戳' }
        }
      },
      FixedTokenInfo: {
        type: 'object',
        properties: {
          hasToken: { type: 'boolean', description: '是否已生成固定令牌' },
          tokenPreview: { type: 'string', description: '令牌预览（仅显示前8位）', nullable: true },
          createdAt: { type: 'string', format: 'date-time', description: '令牌创建时间', nullable: true }
        }
      },
      FixedTokenGenerated: {
        type: 'object',
        properties: {
          token: { type: 'string', description: '生成的固定令牌（仅在生成时返回完整令牌）' },
          message: { type: 'string', description: '操作结果信息' }
        }
      }
    },
    responses: {
      UnauthorizedError: {
        description: '认证失败',
        content: {
          'application/json': {
            schema: {
              $ref: '#/components/schemas/Error'
            },
            example: {
              code: 401,
              status: 'error',
              message: '未授权访问',
              timestamp: '2024-01-01T00:00:00.000Z'
            }
          }
        }
      },
      NotFoundError: {
        description: '资源不存在',
        content: {
          'application/json': {
            schema: {
              $ref: '#/components/schemas/Error'
            },
            example: {
              code: 404,
              status: 'error',
              message: '资源不存在',
              timestamp: '2024-01-01T00:00:00.000Z'
            }
          }
        }
      },
      ForbiddenError: {
        description: '权限不足',
        content: {
          'application/json': {
            schema: {
              $ref: '#/components/schemas/Error'
            },
            example: {
              code: 403,
              status: 'error',
              message: '权限不足',
              timestamp: '2024-01-01T00:00:00.000Z'
            }
          }
        }
      }
    }
  },
  tags: [
    { name: '认证', description: '用户认证相关接口' },
    { name: '用户管理', description: '用户管理相关接口' },
    { name: '险种管理', description: '保险险种管理相关接口' },
    { name: '保险申请', description: '保险申请管理相关接口' },
    { name: '保单管理', description: '保单管理相关接口' },
    { name: '理赔管理', description: '理赔管理相关接口' },
    { name: '系统管理', description: '系统管理相关接口' }
  ]
};

const options = {
  swaggerDefinition,
  apis: [
    './routes/*.js',
    './controllers/*.js'
  ]
};

const swaggerSpec = swaggerJSDoc(options);

module.exports = swaggerSpec;