package com.usky.ems.controller.web;

import com.usky.common.core.bean.ApiResult;
import com.usky.ems.service.EmsModelService;
import com.usky.ems.service.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 能源能耗 - 模型/结构模块 API（项目层级树、能源类型、建筑/区域/网关/通道/设备/属性点位 CRUD）
 * 空间与建筑/区域/网关分别通过 base_space、base_build、base_area 及
 * base_space_build、base_space_area、base_space_gateway 关联；网关在 dmp_gateway。
 * 基础路径前缀：/model（网关再加 /prod-api/service-ems）
 */
@RestController
@RequestMapping("/model")
public class EmsModelController {

    @Autowired
    private EmsModelService emsModelService;

//    /**
//     * 获取项目层级树（建筑、区域、网关）
//     */
//    @GetMapping("/structure/tree")
//    public ApiResult<EmsStructureTreeNode> getStructureTree(
//            @RequestParam(required = false) Long projectId,
//            @RequestParam(required = false, defaultValue = "true") Boolean includeGateway) {
//        return ApiResult.success(emsModelService.getStructureTree(projectId, includeGateway));
//    }

    /**
     * 能源类型列表（与概览 /overview/item 同源：租户产品关联的能源类型）
     */
    @GetMapping("/energy-type/list")
    public ApiResult<List<EmsEnergyTypeVO>> getEnergyTypeList() {
        return ApiResult.success(emsModelService.getEnergyTypeList());
    }

    /**
     * 展示已关联的能源类型及其下属分项/产品列表
     * 数据来源：leo.ems_energy_item_code.energy_type 分组
     */
    @GetMapping("/energy-type/associated")
    public ApiResult<List<EnergyTypeWrapperProductVO>> showAssociatedEnergyTypes() {
        return ApiResult.success(emsModelService.showAssociatedEnergyTypes());
    }

    @PostMapping("/building")
    public ApiResult<EmsIdResponse> createBuilding(@RequestBody EmsModelSaveRequest request) {
        Long id = emsModelService.createBuilding(request);
        return ApiResult.success(new EmsIdResponse(id));
    }

    @PutMapping("/building/{id}")
    public ApiResult<Void> updateBuilding(@PathVariable Long id, @RequestBody EmsModelSaveRequest request) {
        emsModelService.updateBuilding(id, request);
        return ApiResult.success();
    }

    @DeleteMapping("/building/{id}")
    public ApiResult<Void> deleteBuilding(@PathVariable Long id) {
        emsModelService.deleteBuilding(id);
        return ApiResult.success();
    }

    @PostMapping("/region")
    public ApiResult<EmsIdResponse> createRegion(@RequestBody EmsModelSaveRequest request) {
        Long id = emsModelService.createRegion(request);
        return ApiResult.success(new EmsIdResponse(id));
    }

    @PutMapping("/region/{id}")
    public ApiResult<Void> updateRegion(@PathVariable Long id, @RequestBody EmsModelSaveRequest request) {
        emsModelService.updateRegion(id, request);
        return ApiResult.success();
    }

    @DeleteMapping("/region/{id}")
    public ApiResult<Void> deleteRegion(@PathVariable Long id) {
        emsModelService.deleteRegion(id);
        return ApiResult.success();
    }

    @PostMapping("/gateway")
    public ApiResult<EmsIdResponse> createGateway(@RequestBody EmsModelSaveRequest request) {
        String id = emsModelService.createGateway(request);
        return ApiResult.success(new EmsIdResponse(id));
    }

    @PutMapping("/gateway/{id}")
    public ApiResult<Void> updateGateway(@PathVariable String id, @RequestBody EmsModelSaveRequest request) {
        emsModelService.updateGateway(id, request);
        return ApiResult.success();
    }

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

//    @PostMapping("/channel")
//    public ApiResult<EmsIdResponse> createChannel(@RequestBody EmsModelSaveRequest request) {
//        Long id = emsModelService.createChannel(request);
//        return ApiResult.success(new EmsIdResponse(id));
//    }
//
//    @PutMapping("/channel/{id}")
//    public ApiResult<Void> updateChannel(@PathVariable Long id, @RequestBody EmsModelSaveRequest request) {
//        emsModelService.updateChannel(id, request);
//        return ApiResult.success();
//    }
//
//    @DeleteMapping("/channel/{id}")
//    public ApiResult<Void> deleteChannel(@PathVariable Long id) {
//        emsModelService.deleteChannel(id);
//        return ApiResult.success();
//    }
//
//    @PostMapping("/device")
//    public ApiResult<EmsIdResponse> createDevice(@RequestBody EmsModelSaveRequest request) {
//        String id = emsModelService.createDevice(request);
//        return ApiResult.success(new EmsIdResponse(id));
//    }
//
//    @PutMapping("/device/{id}")
//    public ApiResult<Void> updateDevice(@PathVariable String id, @RequestBody EmsModelSaveRequest request) {
//        emsModelService.updateDevice(id, request);
//        return ApiResult.success();
//    }
//
//    @DeleteMapping("/device/{id}")
//    public ApiResult<Void> deleteDevice(@PathVariable String id) {
//        emsModelService.deleteDevice(id);
//        return ApiResult.success();
//    }
//
//    @PostMapping("/attribute-point")
//    public ApiResult<EmsIdResponse> createAttributePoint(@RequestBody EmsModelSaveRequest request) {
//        Long id = emsModelService.createAttributePoint(request);
//        return ApiResult.success(new EmsIdResponse(id));
//    }
//
//    @PutMapping("/attribute-point/{id}")
//    public ApiResult<Void> updateAttributePoint(@PathVariable Long id, @RequestBody EmsModelSaveRequest request) {
//        emsModelService.updateAttributePoint(id, request);
//        return ApiResult.success();
//    }
//
//    @DeleteMapping("/attribute-point/{id}")
//    public ApiResult<Void> deleteAttributePoint(@PathVariable Long id) {
//        emsModelService.deleteAttributePoint(id);
//        return ApiResult.success();
//    }

    /**
     * 网关-通道-设备树（中间面板）
     * GET /prod-api/service-ems/model/gateway-device/tree（与类上 /model 前缀组合，勿再嵌套 /model）
     * @param spaceId 空间ID（可选，不传返回全部网关）
     * @param keyword 网关名称关键字（可选，模糊）
     */
    @GetMapping("/gateway-device/tree")
    public ApiResult<List<EmsGatewayDeviceTreeNode>> getGatewayDeviceTree(
            @RequestParam(required = false) Long spaceId,
            @RequestParam(required = false) String keyword) {
        return ApiResult.success(emsModelService.getGatewayDeviceTree(spaceId, keyword));
    }

    /**
     * 属性点位列表（右侧面板）
     * GET /prod-api/service-ems/model/attribute-point/list
     * @param deviceId 设备ID（必填）
     */
    @GetMapping("/attribute-point/list")
    public ApiResult<List<EmsAttributePointVO>> getAttributePointList(
            @RequestParam String deviceId) {
        return ApiResult.success(emsModelService.getAttributePointList(deviceId));
    }

    /**
     * 空间绑定网关（一个空间绑定一个或多个网关）
     */
    @PostMapping("/space/gateway/bind")
    public ApiResult<Void> bindSpaceGateways(@RequestBody SpaceGatewayBindRequest request) {
        emsModelService.bindSpaceGateways(request.getSpaceId(), request.getGatewayUuids());
        return ApiResult.success();
    }

    /**
     * 空间解绑网关（一个空间解绑一个或多个网关）
     */
    @PostMapping("/space/gateway/unbind")
    public ApiResult<Void> unbindSpaceGateways(@RequestBody SpaceGatewayBindRequest request) {
        emsModelService.unbindSpaceGateways(request.getSpaceId(), request.getGatewayUuids());
        return ApiResult.success();
    }

    /**
     * 根据空间ID查询网关列表
     */
    @GetMapping("/space/{spaceId}/gateway/list")
    public ApiResult<List<DmpGatewayDetailResponse>> getGatewayListBySpaceId(@PathVariable Long spaceId) {
        return ApiResult.success(emsModelService.getGatewayListBySpaceId(spaceId));
    }


}
