package com.writeoff.module.notification.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.common.api.PageResult;
import com.writeoff.module.notification.dto.BindNotificationPolicyEventsRequest;
import com.writeoff.module.notification.dto.CreateNotificationPolicyRequest;
import com.writeoff.module.notification.dto.UpdateNotificationPolicyRequest;
import com.writeoff.module.notification.model.NotificationPolicyInfo;
import com.writeoff.module.notification.service.NotificationPolicyService;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.RequirePermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/notification-policies")
public class NotificationPolicyController {
    private final NotificationPolicyService notificationPolicyService;

    public NotificationPolicyController(NotificationPolicyService notificationPolicyService) {
        this.notificationPolicyService = notificationPolicyService;
    }

    @GetMapping
    @RequirePermission(value = "notification.policy.read", dataScope = DataScopeType.TENANT, auditAction = "NOTIFICATION_POLICY_LIST")
    public ApiResponse<PageResult<NotificationPolicyInfo>> list(
            @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {
        return ApiResponse.success(notificationPolicyService.list(pageNo, pageSize));
    }

    @PostMapping
    @RequirePermission(value = "notification.policy.manage", dataScope = DataScopeType.TENANT, auditAction = "NOTIFICATION_POLICY_CREATE")
    public ApiResponse<NotificationPolicyInfo> create(@RequestBody @Valid CreateNotificationPolicyRequest request) {
        return ApiResponse.success(notificationPolicyService.create(request));
    }

    @PutMapping("/{id}")
    @RequirePermission(value = "notification.policy.manage", dataScope = DataScopeType.TENANT, auditAction = "NOTIFICATION_POLICY_UPDATE")
    public ApiResponse<NotificationPolicyInfo> update(@PathVariable("id") Long id,
                                                      @RequestBody @Valid UpdateNotificationPolicyRequest request) {
        return ApiResponse.success(notificationPolicyService.update(id, request));
    }

    @PostMapping("/{id}/events")
    @RequirePermission(value = "notification.policy.manage", dataScope = DataScopeType.TENANT, auditAction = "NOTIFICATION_POLICY_BIND_EVENTS")
    public ApiResponse<NotificationPolicyInfo> bindEvents(@PathVariable("id") Long id,
                                                          @RequestBody @Valid BindNotificationPolicyEventsRequest request) {
        return ApiResponse.success(notificationPolicyService.bindEvents(id, request.getEventCode()));
    }

    @PostMapping("/{id}/enable")
    @RequirePermission(value = "notification.policy.manage", dataScope = DataScopeType.TENANT, auditAction = "NOTIFICATION_POLICY_ENABLE")
    public ApiResponse<NotificationPolicyInfo> enable(@PathVariable("id") Long id) {
        return ApiResponse.success(notificationPolicyService.enable(id));
    }

    @PostMapping("/{id}/disable")
    @RequirePermission(value = "notification.policy.manage", dataScope = DataScopeType.TENANT, auditAction = "NOTIFICATION_POLICY_DISABLE")
    public ApiResponse<NotificationPolicyInfo> disable(@PathVariable("id") Long id) {
        return ApiResponse.success(notificationPolicyService.disable(id));
    }

    @PostMapping("/{id}/delete")
    @RequirePermission(value = "notification.policy.manage", dataScope = DataScopeType.TENANT, auditAction = "NOTIFICATION_POLICY_DELETE")
    public ApiResponse<String> delete(@PathVariable("id") Long id) {
        notificationPolicyService.softDelete(id);
        return ApiResponse.success("OK");
    }
}
