package com.luozhihui.project.controller;

import com.luozhihui.project.common.ApiResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.dao.DataAccessException;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class CityController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @PostMapping("/cityPicker")
    public ResponseEntity<Object> getCity(@RequestParam MultiValueMap<String, String> formData) {
        String type = formData.getFirst("type");
        if (type == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("请求中缺少 'type' 字段");
        }

        switch (type) {
            case "province":
                return getProvince();
            case "city":
                String provinceCode = formData.getFirst("province_code");
                if (provinceCode == null) {
                    return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("请求中缺少 'province_code' 字段");
                }
                return getCity(provinceCode);
            case "area":
                String cityCode = formData.getFirst("city_code");
                if (cityCode == null) {
                    return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("请求中缺少 'city_code' 字段");
                }
                return getArea(cityCode);
            default:
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("请求中的 'type' 字段无效");
        }
    }

    private ResponseEntity<Object> getProvince() {
        try {
            List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT * FROM `province`");
            if (result.isEmpty()) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("未找到省份数据");
            }
            return ResponseEntity.ok(new ApiResponse(result, 200, "获取成功"));
        } catch (DataAccessException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("获取省份数据时出错");
        }
    }

    private ResponseEntity<Object> getCity(String provinceCode) {
        try {
            String sql = "SELECT * FROM `city` WHERE city_code LIKE ?";
            String provinceCodePrefix = provinceCode.substring(0, 2) + "%";
            List<Map<String, Object>> result = jdbcTemplate.queryForList(sql, provinceCodePrefix);
            if (result.isEmpty()) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("未找到所提供省份代码的城市数据");
            }
            return ResponseEntity.ok(new ApiResponse(result, 200, "获取成功"));
        } catch (DataAccessException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("获取城市数据时出错");
        }
    }

    private ResponseEntity<Object> getArea(String cityCode) {
        try {
            String sql = "SELECT * FROM `area` WHERE area_code LIKE ?";
            String cityCodePrefix = cityCode.substring(0, 4) + "%";
            List<Map<String, Object>> result = jdbcTemplate.queryForList(sql, cityCodePrefix);
            if (result.isEmpty()) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("未找到所提供城市代码的区县数据");
            }
            result.forEach(row -> {
                String areaName = (String) row.get("area_name");
                row.put("area_name", areaName.trim());
            });
            return ResponseEntity.ok(new ApiResponse(result, 200, "获取成功"));
        } catch (DataAccessException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("获取区县数据时出错");
        }
    }
}
