package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.CommonPushChannelConfig;
import shop.alien.store.service.CommonPushChannelConfigService;

@Api(tags = {"推送渠道配置管理"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/commonPushChannelConfig")
@RequiredArgsConstructor
public class CommonPushChannelConfigController {

    private final CommonPushChannelConfigService commonPushChannelConfigService;

    @ApiOperation("新增推送渠道配置")
    @ApiOperationSupport(order = 1)
    @PostMapping("/add")
    public R<String> add(@RequestBody CommonPushChannelConfig config) {
        return commonPushChannelConfigService.add(config);
    }

    @ApiOperation("根据主键删除（逻辑删除）")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/deleteById")
    public R<String> deleteById(@RequestParam Long id) {
        return commonPushChannelConfigService.deleteById(id);
    }

    @ApiOperation("更新推送渠道配置")
    @ApiOperationSupport(order = 3)
    @PostMapping("/update")
    public R<String> update(@RequestBody CommonPushChannelConfig config) {
        return commonPushChannelConfigService.update(config);
    }

    @ApiOperation("根据主键查询")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/getById")
    public R<CommonPushChannelConfig> getById(@RequestParam Long id) {
        return commonPushChannelConfigService.getInfoById(id);
    }

    @ApiOperation("分页列表")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "channelCode", value = "渠道编码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "platform", value = "平台", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "enable", value = "是否启用", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/list")
    public R<IPage<CommonPushChannelConfig>> list(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String channelCode,
            @RequestParam(required = false) String platform,
            @RequestParam(required = false) Integer enable) {
        return commonPushChannelConfigService.list(pageNum, pageSize, channelCode, platform, enable);
    }

    @ApiOperation("启用/停用渠道")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "enable", value = "0-停用 1-启用", dataType = "Integer", paramType = "query", required = true)
    })
    @PostMapping("/toggleEnable")
    public R<String> toggleEnable(@RequestParam Long id, @RequestParam Integer enable) {
        return commonPushChannelConfigService.toggleEnable(id, enable);
    }

    @ApiOperation("连通性测试")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", paramType = "query", required = true)
    })
    @PostMapping("/testConnect")
    public R<String> testConnect(@RequestParam Long id) {
        return commonPushChannelConfigService.testConnect(id);
    }
}
