package com.usky.sas.controller.web;

import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.sas.service.SasMapService;
import com.usky.sas.service.vo.MapDeviceBindRequest;
import com.usky.sas.service.vo.MapListItem;
import com.usky.sas.service.vo.MapPageRequest;
import com.usky.sas.service.vo.MapSaveRequest;
import com.usky.sas.service.vo.MapSaveWithDevicesRequest;
import com.usky.sas.service.vo.MapTreeItem;
import com.usky.sas.service.vo.PicAddRequest;
import com.usky.sas.service.vo.PicSourceInfo;
import com.usky.sas.service.vo.PicSourcePageRequest;
import com.usky.sas.service.vo.PicSourceUpdateRequest;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
     * 电子地图配置接口
     *
     * 对应 API 文档：
     * GET /prod-api/service-sas/map
     * POST /prod-api/service-sas/map
     * PUT /prod-api/service-sas/map/{id}
     * DELETE /prod-api/service-sas/map/{id}
     * POST /prod-api/service-sas/map/{mapId}/device
     * DELETE /prod-api/service-sas/map/{mapId}/device/{deviceId}
     * GET /prod-api/service-sas/map/tree
     * POST /prod-api/service-sas/map/saveMapInfo
     * POST /prod-api/service-sas/map/pic  新增图片记录（不入库文件，仅 url/path）
     * GET  /prod-api/service-sas/map/pic/source  分页查询图片资源（可按 picType 筛选）
     * PUT  /prod-api/service-sas/map/pic/source/{id}  修改图片资源
     * DELETE /prod-api/service-sas/map/pic/source/{id}  删除图片资源
 */
@RestController
@RequestMapping("/map")
public class SasMapController {

    @Autowired
    private SasMapService sasMapService;

    @GetMapping
    public ApiResult<CommonPage<MapListItem>> page(MapPageRequest request) {
        return ApiResult.success(sasMapService.page(request));
    }

    @PostMapping
    public ApiResult<CreateMapResponse> create(@RequestBody MapSaveRequest request) {
        String id = sasMapService.create(request);
        CreateMapResponse resp = new CreateMapResponse();
        resp.setId(id);
        return ApiResult.success(resp);
    }

    @PutMapping("/{id}")
    public ApiResult<Void> update(@PathVariable("id") String id, @RequestBody MapSaveRequest request) {
        sasMapService.update(id, request);
        return ApiResult.success();
    }

    @DeleteMapping("/{id}")
    public ApiResult<Void> delete(@PathVariable("id") String id) {
        sasMapService.delete(id);
        return ApiResult.success();
    }

    @PostMapping("/{mapId}/device")
    public ApiResult<CreateMapDeviceResponse> bindDevice(@PathVariable("mapId") String mapId,
                                                         @RequestBody MapDeviceBindRequest request) {
        String id = sasMapService.bindDevice(mapId, request);
        CreateMapDeviceResponse resp = new CreateMapDeviceResponse();
        resp.setId(id);
        return ApiResult.success(resp);
    }

    @DeleteMapping("/{mapId}/device/{deviceId}")
    public ApiResult<Void> unbindDevice(@PathVariable("mapId") String mapId,
                                        @PathVariable("deviceId") String deviceId) {
        sasMapService.unbindDevice(mapId, deviceId);
        return ApiResult.success();
    }

    /**
     * 保存电子地图及设备点位（批量）
     */
    @PostMapping("/saveMapInfo")
    public ApiResult<Void> saveMapInfo(@RequestBody MapSaveWithDevicesRequest request) {
        sasMapService.saveMapInfo(request);
        return ApiResult.success();
    }

    /**
     * 获取地图导航树形列表
     */
    @GetMapping("/tree")
    public ApiResult<java.util.List<MapTreeItem>> getMapTree() {
        return ApiResult.success(sasMapService.getMapTree());
    }

    /**
     * 新增图片记录（不处理二进制文件上传，仅根据 url/path 写入 sas_pic）
     */
    @PostMapping("/pic")
    public ApiResult<AddPicResponse> addPic(@RequestBody PicAddRequest request) {
        String id = sasMapService.addPic(request);
        AddPicResponse resp = new AddPicResponse();
        resp.setId(id);
        return ApiResult.success(resp);
    }

    /**
     * 分页查询图片资源（sas_pic_source），可按 picType 筛选，每条带图片完整 URL
     */
    @GetMapping("/pic/source")
    public ApiResult<CommonPage<PicSourceInfo>> getPicSource(PicSourcePageRequest request) {
        return ApiResult.success(sasMapService.getPicSource(request));
    }

    /**
     * 修改图片资源（更新 sas_pic_source 及可选的关联 sas_pic.url/path）
     */
    @PutMapping("/pic/source/{id}")
    public ApiResult<Void> updatePicSource(@PathVariable("id") String id, @RequestBody PicSourceUpdateRequest request) {
        sasMapService.updatePicSource(id, request);
        return ApiResult.success();
    }

    /**
     * 删除图片资源（仅删除 sas_pic_source 记录）
     */
    @DeleteMapping("/pic/source/{id}")
    public ApiResult<Void> deletePicSource(@PathVariable("id") String id) {
        sasMapService.deletePicSource(id);
        return ApiResult.success();
    }

    @Data
    public static class CreateMapResponse {
        private String id;
    }

    @Data
    public static class CreateMapDeviceResponse {
        private String id;
    }

    @Data
    public static class AddPicResponse {
        private String id;
    }
}

