package com.writeoff.module.notification.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.module.notification.dto.SavePlatformNotifyGatewayRequest;
import com.writeoff.module.notification.dto.TestPlatformNotifyGatewayRequest;
import com.writeoff.module.notification.model.PlatformNotifyGatewayInfo;
import com.writeoff.module.notification.service.PlatformNotifyGatewayService;
import com.writeoff.module.notification.service.PlatformNotifyGatewayTestService;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.PermissionDomain;
import com.writeoff.security.RequirePermission;
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 javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/platform/notify-gateways")
public class PlatformNotifyGatewayController {
    private final PlatformNotifyGatewayService gatewayService;
    private final PlatformNotifyGatewayTestService testService;

    public PlatformNotifyGatewayController(PlatformNotifyGatewayService gatewayService,
                                           PlatformNotifyGatewayTestService testService) {
        this.gatewayService = gatewayService;
        this.testService = testService;
    }

    @GetMapping
    @RequirePermission(value = "platform.notify-gateway.read", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_NOTIFY_GATEWAY_LIST")
    public ApiResponse<List<PlatformNotifyGatewayInfo>> list() {
        return ApiResponse.success(gatewayService.list());
    }

    @PutMapping("/{channelCode}")
    @RequirePermission(value = "platform.notify-gateway.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_NOTIFY_GATEWAY_SAVE")
    public ApiResponse<PlatformNotifyGatewayInfo> save(@PathVariable("channelCode") String channelCode,
                                                       @RequestBody SavePlatformNotifyGatewayRequest request) {
        return ApiResponse.success(gatewayService.save(channelCode, request));
    }

    @PostMapping("/{channelCode}/test")
    @RequirePermission(value = "platform.notify-gateway.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_NOTIFY_GATEWAY_TEST")
    public ApiResponse<Map<String, Object>> test(@PathVariable("channelCode") String channelCode,
                                                 @RequestBody @Valid TestPlatformNotifyGatewayRequest request) {
        return ApiResponse.success(testService.test(channelCode, request));
    }
}
