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.SasEventTypeGroupService;
import com.usky.sas.service.vo.EventGroupInfo;
import com.usky.sas.service.vo.EventGroupPageRequest;
import com.usky.sas.service.vo.EventGroupSaveRequest;
import com.usky.sas.service.vo.SystemTypeCodeVO;
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;

import java.util.List;

/**
 * 事件组配置接口
 *
 * 对应 API 文档：
 * GET /prod-api/service-sas/event/group
 * POST /prod-api/service-sas/event/group
 * PUT /prod-api/service-sas/event/group/{id}
 * DELETE /prod-api/service-sas/event/group/{id}
 */
@RestController
@RequestMapping("/event/group")
public class SasEventGroupController {

    @Autowired
    private SasEventTypeGroupService eventTypeGroupService;

    /**
     * 事件组分页查询
     */
    @GetMapping
    public ApiResult<CommonPage<EventGroupInfo>> page(EventGroupPageRequest request) {
        return ApiResult.success(eventTypeGroupService.page(request));
    }

    /**
     * 获取设备事件类型列表（根据授权模块过滤）
     *
     * GET /prod-api/service-sas/event/group/getSystemTypeCodes
     */
    @GetMapping("/getSystemTypeCodes")
    public ApiResult<List<SystemTypeCodeVO>> getSystemTypeCodes() {
        return ApiResult.success(eventTypeGroupService.getSystemTypeCodes());
    }

    /**
     * 新增事件组
     */
    @PostMapping
    public ApiResult<CreateResponse> create(@RequestBody EventGroupSaveRequest request) {
        String id = eventTypeGroupService.create(request);
        CreateResponse resp = new CreateResponse();
        resp.setId(id);
        return ApiResult.success(resp);
    }

    /**
     * 编辑事件组
     */
    @PutMapping("/{id}")
    public ApiResult<Void> update(@PathVariable("id") String id, @RequestBody EventGroupSaveRequest request) {
        eventTypeGroupService.update(id, request);
        return ApiResult.success();
    }

    /**
     * 删除事件组
     */
    @DeleteMapping("/{id}")
    public ApiResult<Void> delete(@PathVariable("id") String id) {
        eventTypeGroupService.delete(id);
        return ApiResult.success();
    }

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

