package com.njcn.msgpush.module.push.controller.admin.channel;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.msgpush.framework.common.pojo.CommonResult;
import com.njcn.msgpush.module.push.controller.admin.channel.vo.ChannelProviderConfigReqVO;
import com.njcn.msgpush.module.push.dal.dataobject.channel.ChannelProviderConfigDO;
import com.njcn.msgpush.module.push.service.channel.ChannelProviderConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.njcn.msgpush.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 渠道服务商")
@Slf4j
@Validated
@RestController
@RequestMapping("/push/channel")
public class ChannelProviderConfigController {

    @Autowired
    private ChannelProviderConfigService channelProviderConfigService;

    @PostMapping("/page")
    @Operation(summary = "分页查询渠道服务商列表")
    @PreAuthorize("@ss.hasPermission('push:channel:page')")
    public CommonResult<Page<ChannelProviderConfigDO>> pageChannelProviderConfig(@Validated @RequestBody ChannelProviderConfigReqVO reqVO) {
        return success(channelProviderConfigService.getPage(reqVO));
    }

    @PostMapping("/add")
    @Operation(summary = "新增渠道服务商")
    @PreAuthorize("@ss.hasPermission('push:channel:add')")
    public CommonResult<Boolean> add(@Validated @RequestBody ChannelProviderConfigReqVO reqVO) {
        boolean res = channelProviderConfigService.save(BeanUtil.copyProperties(reqVO, ChannelProviderConfigDO.class));
        return success(res);
    }

    @PostMapping("/update")
    @Operation(summary = "更新渠道服务商")
    @PreAuthorize("@ss.hasPermission('push:channel:update')")
    public CommonResult<Boolean> update(@Validated @RequestBody ChannelProviderConfigReqVO reqVO) {
        boolean res = channelProviderConfigService.updateById(BeanUtil.copyProperties(reqVO, ChannelProviderConfigDO.class));
        return success(res);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除渠道服务商")
    @PreAuthorize("@ss.hasPermission('push:channel:delete')")
    @Parameter(name = "ids", description = "id 列表", required = true)
    public CommonResult<Boolean> delete(@RequestParam("ids") List<String> ids) {
        boolean res = channelProviderConfigService.deleteByIds(ids);
        return success(res);
    }

    @GetMapping("/toggle")
    @Operation(summary = "启用/禁用渠道服务商")
    @PreAuthorize("@ss.hasPermission('push:channel:toggle')")
    @Parameter(name = "id", description = "id", required = true)
    public CommonResult<Void> toggleEnableChannelProvider(@RequestParam("id") String id) {
        channelProviderConfigService.toggleEnableField(id);
        return success(null);
    }
}
