package com.writeoff.module.system.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.common.api.PageResult;
import com.writeoff.common.model.ImportResult;
import com.writeoff.module.system.dto.AssignUserRoleRequest;
import com.writeoff.module.system.dto.CreateUserRequest;
import com.writeoff.module.system.dto.ImportUsersRequest;
import com.writeoff.module.system.dto.ResetPasswordRequest;
import com.writeoff.module.system.model.SystemUser;
import com.writeoff.module.system.service.PlatformIamService;
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.RequestBody;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/platform/users")
public class PlatformUserController {
    private final PlatformIamService platformIamService;

    public PlatformUserController(PlatformIamService platformIamService) {
        this.platformIamService = platformIamService;
    }

    @GetMapping
    @RequirePermission(value = "platform.user.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_USER_LIST")
    public ApiResponse<PageResult<SystemUser>> list(@RequestParam(value = "keyword", required = false) String keyword) {
        return ApiResponse.success(platformIamService.listUsers(keyword));
    }

    @PostMapping
    @RequirePermission(value = "platform.user.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_USER_CREATE")
    public ApiResponse<SystemUser> create(@RequestBody @Valid CreateUserRequest request) {
        return ApiResponse.success(platformIamService.createUser(request));
    }

    @PostMapping("/import")
    @RequirePermission(value = "platform.user.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_USER_IMPORT")
    public ApiResponse<ImportResult> importUsers(@RequestBody @Valid ImportUsersRequest request) {
        return ApiResponse.success(platformIamService.importUsers(request.getUsers()));
    }

    @PutMapping("/{id}")
    @RequirePermission(value = "platform.user.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_USER_UPDATE")
    public ApiResponse<SystemUser> update(@PathVariable("id") Long id, @RequestBody @Valid CreateUserRequest request) {
        return ApiResponse.success(platformIamService.updateUser(id, request));
    }

    @PostMapping("/assign-role")
    @RequirePermission(value = "platform.user.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_USER_ASSIGN_ROLE")
    public ApiResponse<String> assignRole(@RequestBody @Valid AssignUserRoleRequest request) {
        platformIamService.assignRole(request);
        return ApiResponse.success("OK");
    }

    @PostMapping("/{id}/enable")
    @RequirePermission(value = "platform.user.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_USER_ENABLE")
    public ApiResponse<String> enable(@PathVariable("id") Long id) {
        platformIamService.enableUser(id);
        return ApiResponse.success("OK");
    }

    @PostMapping("/{id}/disable")
    @RequirePermission(value = "platform.user.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_USER_DISABLE")
    public ApiResponse<String> disable(@PathVariable("id") Long id) {
        platformIamService.disableUser(id);
        return ApiResponse.success("OK");
    }

    @PostMapping("/{id}/reset-password")
    @RequirePermission(value = "platform.user.manage", domain = PermissionDomain.PLATFORM, dataScope = DataScopeType.GLOBAL_READONLY, auditAction = "PLATFORM_USER_RESET_PASSWORD")
    public ApiResponse<String> resetPassword(@PathVariable("id") Long id, @RequestBody @Valid ResetPasswordRequest request) {
        platformIamService.resetPassword(id, request);
        return ApiResponse.success("OK");
    }
}
