import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
import { z } from '@hono/zod-openapi';
import { AreaService } from '../../modules/areas/area.service';
import { AreaLevel } from '../../modules/areas/area.entity';
import { AppDataSource } from '@d8d/shared-utils';

// 省份查询参数Schema
const getProvincesSchema = z.object({
  page: z.coerce.number<number>().int().min(1).default(1).openapi({
    example: 1,
    description: '页码'
  }),
  pageSize: z.coerce.number<number>().int().min(1).max(100).default(50).openapi({
    example: 50,
    description: '每页数量'
  })
});

// 城市查询参数Schema
const getCitiesSchema = z.object({
  provinceId: z.coerce.number<number>().int().positive('省份ID必须为正整数').openapi({
    example: 1,
    description: '省份ID'
  }),
  page: z.coerce.number<number>().int().min(1).default(1).openapi({
    example: 1,
    description: '页码'
  }),
  pageSize: z.coerce.number<number>().int().min(1).max(100).default(50).openapi({
    example: 50,
    description: '每页数量'
  })
});

// 区县查询参数Schema
const getDistrictsSchema = z.object({
  cityId: z.coerce.number<number>().int().positive('城市ID必须为正整数').openapi({
    example: 34,
    description: '城市ID'
  }),
  page: z.coerce.number<number>().int().min(1).default(1).openapi({
    example: 1,
    description: '页码'
  }),
  pageSize: z.coerce.number<number>().int().min(1).max(100).default(50).openapi({
    example: 50,
    description: '每页数量'
  })
});

// 省市区响应Schema
const areaResponseSchema = z.object({
  id: z.number(),
  name: z.string(),
  code: z.string(),
  level: z.number(),
  parentId: z.number().nullable()
});

// 省份列表响应Schema
const provincesResponseSchema = z.object({
  success: z.boolean(),
  data: z.object({
    provinces: z.array(areaResponseSchema),
    pagination: z.object({
      page: z.number(),
      pageSize: z.number(),
      total: z.number(),
      totalPages: z.number()
    })
  }),
  message: z.string()
});

// 城市列表响应Schema
const citiesResponseSchema = z.object({
  success: z.boolean(),
  data: z.object({
    cities: z.array(areaResponseSchema),
    pagination: z.object({
      page: z.number(),
      pageSize: z.number(),
      total: z.number(),
      totalPages: z.number()
    })
  }),
  message: z.string()
});

// 区县列表响应Schema
const districtsResponseSchema = z.object({
  success: z.boolean(),
  data: z.object({
    districts: z.array(areaResponseSchema),
    pagination: z.object({
      page: z.number(),
      pageSize: z.number(),
      total: z.number(),
      totalPages: z.number()
    })
  }),
  message: z.string()
});

// 错误响应Schema
const errorSchema = z.object({
  code: z.number(),
  message: z.string(),
  errors: z.array(z.object({
    path: z.array(z.string()),
    message: z.string()
  })).optional()
});

// 创建省份查询路由
const getProvincesRoute = createRoute({
  method: 'get',
  path: '/provinces',
  request: {
    query: getProvincesSchema
  },
  responses: {
    200: {
      description: '获取省份列表成功',
      content: {
        'application/json': { schema: provincesResponseSchema }
      }
    },
    500: {
      description: '获取省份列表失败',
      content: { 'application/json': { schema: errorSchema } }
    }
  }
});

// 创建城市查询路由
const getCitiesRoute = createRoute({
  method: 'get',
  path: '/cities',
  request: {
    query: getCitiesSchema
  },
  responses: {
    200: {
      description: '获取城市列表成功',
      content: {
        'application/json': { schema: citiesResponseSchema }
      }
    },
    400: {
      description: '参数错误',
      content: { 'application/json': { schema: errorSchema } }
    },
    500: {
      description: '获取城市列表失败',
      content: { 'application/json': { schema: errorSchema } }
    }
  }
});

// 创建区县查询路由
const getDistrictsRoute = createRoute({
  method: 'get',
  path: '/districts',
  request: {
    query: getDistrictsSchema
  },
  responses: {
    200: {
      description: '获取区县列表成功',
      content: {
        'application/json': { schema: districtsResponseSchema }
      }
    },
    400: {
      description: '参数错误',
      content: { 'application/json': { schema: errorSchema } }
    },
    500: {
      description: '获取区县列表失败',
      content: { 'application/json': { schema: errorSchema } }
    }
  }
});

const app = new OpenAPIHono()
  .openapi(getProvincesRoute, async (c) => {
    try {
      const { page, pageSize } = c.req.valid('query');
      const areaService = new AreaService(AppDataSource);

      // 获取所有省份数据
      const provinces = await areaService.getAreaTreeByLevel(AreaLevel.PROVINCE);

      // 分页
      const startIndex = (page - 1) * pageSize;
      const endIndex = startIndex + pageSize;
      const paginatedProvinces = provinces.slice(startIndex, endIndex);

      return c.json({
        success: true,
        data: {
          provinces: paginatedProvinces,
          pagination: {
            page,
            pageSize,
            total: provinces.length,
            totalPages: Math.ceil(provinces.length / pageSize)
          }
        },
        message: '获取省份列表成功'
      }, 200);
    } catch (error) {
      console.error('获取省份列表失败:', error);
      return c.json({
        code: 500,
        message: error instanceof Error ? error.message : '获取省份列表失败'
      }, 500);
    }
  })
  .openapi(getCitiesRoute, async (c) => {
    try {
      const { provinceId, page, pageSize } = c.req.valid('query');
      const areaService = new AreaService(AppDataSource);

      // 获取指定省份下的所有城市
      const allCities = await areaService.getAreaTreeByLevel(AreaLevel.CITY);
      const cities = allCities.filter(city => city.parentId === provinceId);

      // 分页
      const startIndex = (page - 1) * pageSize;
      const endIndex = startIndex + pageSize;
      const paginatedCities = cities.slice(startIndex, endIndex);

      return c.json({
        success: true,
        data: {
          cities: paginatedCities,
          pagination: {
            page,
            pageSize,
            total: cities.length,
            totalPages: Math.ceil(cities.length / pageSize)
          }
        },
        message: '获取城市列表成功'
      }, 200);
    } catch (error) {
      console.error('获取城市列表失败:', error);
      return c.json({
        code: 500,
        message: error instanceof Error ? error.message : '获取城市列表失败'
      }, 500);
    }
  })
  .openapi(getDistrictsRoute, async (c) => {
    try {
      const { cityId, page, pageSize } = c.req.valid('query');
      const areaService = new AreaService(AppDataSource);

      // 获取指定城市下的所有区县
      const allDistricts = await areaService.getAreaTreeByLevel(AreaLevel.DISTRICT);
      const districts = allDistricts.filter(district => district.parentId === cityId);

      // 分页
      const startIndex = (page - 1) * pageSize;
      const endIndex = startIndex + pageSize;
      const paginatedDistricts = districts.slice(startIndex, endIndex);

      return c.json({
        success: true,
        data: {
          districts: paginatedDistricts,
          pagination: {
            page,
            pageSize,
            total: districts.length,
            totalPages: Math.ceil(districts.length / pageSize)
          }
        },
        message: '获取区县列表成功'
      }, 200);
    } catch (error) {
      console.error('获取区县列表失败:', error);
      return c.json({
        code: 500,
        message: error instanceof Error ? error.message : '获取区县列表失败'
      }, 500);
    }
  });

export const areaRoutes = app;
export default app;