package com.usky.ems.controller.web;

import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.ems.domain.DmpDevice;
import com.usky.ems.domain.DmpGateway;
import com.usky.ems.service.DmpGatewayService;
import com.usky.ems.service.vo.DmpGatewayDevicePageRequest;
import com.usky.ems.service.vo.DmpGatewayDetailResponse;
import com.usky.ems.service.vo.DmpGatewayListItem;
import com.usky.ems.service.vo.DmpGatewayPageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * dmp_gateway 网关维护 API（REST 风格对齐 IoT 模块 DmpDeviceInfoController 的增删改）
 * 基础路径：/dmp/gateway（网关前缀如 /prod-api/service-ems 依部署而定）
 */
@RestController
@RequestMapping("/gateway")
public class DmpGatewayController {

    @Autowired
    private DmpGatewayService dmpGatewayService;

    /**
     * 分页列表
     */
    @PostMapping("/list")
    public ApiResult<CommonPage<DmpGatewayListItem>> gatewayList(@RequestBody DmpGatewayPageRequest request) {
        return ApiResult.success(dmpGatewayService.pageGateways(request));
    }

    /**
     * 按网关 UUID 分页查询 dmp_device 列表
     */
    @PostMapping("/device/list")
    public ApiResult<CommonPage<DmpDevice>> gatewayDeviceList(@RequestBody DmpGatewayDevicePageRequest request) {
        return ApiResult.success(dmpGatewayService.pageGatewayDevices(request));
    }

    /**
     * 详情
     */
    @GetMapping("/{id}")
    public ApiResult<DmpGatewayDetailResponse> getGateway(@PathVariable Integer id) {
        return ApiResult.success(dmpGatewayService.getGatewayDetail(id));
    }

    /**
     * 新增
     */
    @PostMapping
    public ApiResult<Void> add(@RequestBody DmpGateway entity) {
        dmpGatewayService.add(entity);
        return ApiResult.success();
    }

    /**
     * 修改
     */
    @PutMapping
    public ApiResult<Void> edit(@RequestBody DmpGateway entity) {
        dmpGatewayService.update(entity);
        return ApiResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public ApiResult<Void> remove(@PathVariable("id") Integer id) {
        dmpGatewayService.remove(id);
        return ApiResult.success();
    }
}
