package com.writeoff.module.notification.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.common.api.PageResult;
import com.writeoff.module.notification.dto.CreateNotificationTextTemplateRequest;
import com.writeoff.module.notification.dto.UpdateNotificationTextTemplateRequest;
import com.writeoff.module.notification.model.NotificationTextTemplateInfo;
import com.writeoff.module.notification.service.NotificationTextTemplateService;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.RequirePermission;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/notification-text-templates")
public class NotificationTextTemplateController {
    private final NotificationTextTemplateService notificationTextTemplateService;

    public NotificationTextTemplateController(NotificationTextTemplateService notificationTextTemplateService) {
        this.notificationTextTemplateService = notificationTextTemplateService;
    }

    @GetMapping
    @RequirePermission(value = "notification.text-template.read", dataScope = DataScopeType.TENANT, auditAction = "NOTIFICATION_TEXT_TEMPLATE_LIST")
    public ApiResponse<PageResult<NotificationTextTemplateInfo>> list(
            @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {
        return ApiResponse.success(notificationTextTemplateService.list(pageNo, pageSize));
    }

    @PostMapping
    @RequirePermission(value = "notification.text-template.manage", dataScope = DataScopeType.TENANT, auditAction = "NOTIFICATION_TEXT_TEMPLATE_CREATE")
    public ApiResponse<NotificationTextTemplateInfo> create(@RequestBody @Valid CreateNotificationTextTemplateRequest request) {
        return ApiResponse.success(notificationTextTemplateService.create(request));
    }

    @PutMapping("/{id}")
    @RequirePermission(value = "notification.text-template.manage", dataScope = DataScopeType.TENANT, auditAction = "NOTIFICATION_TEXT_TEMPLATE_UPDATE")
    public ApiResponse<NotificationTextTemplateInfo> update(@PathVariable("id") Long id,
                                                            @RequestBody @Valid UpdateNotificationTextTemplateRequest request) {
        return ApiResponse.success(notificationTextTemplateService.update(id, request));
    }

    @PostMapping("/{id}/enable")
    @RequirePermission(value = "notification.text-template.manage", dataScope = DataScopeType.TENANT, auditAction = "NOTIFICATION_TEXT_TEMPLATE_ENABLE")
    public ApiResponse<NotificationTextTemplateInfo> enable(@PathVariable("id") Long id) {
        return ApiResponse.success(notificationTextTemplateService.enable(id));
    }

    @PostMapping("/{id}/disable")
    @RequirePermission(value = "notification.text-template.manage", dataScope = DataScopeType.TENANT, auditAction = "NOTIFICATION_TEXT_TEMPLATE_DISABLE")
    public ApiResponse<NotificationTextTemplateInfo> disable(@PathVariable("id") Long id) {
        return ApiResponse.success(notificationTextTemplateService.disable(id));
    }

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