package com.yaoai.admin.user;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.yaoai.admin.audit.annotation.AdminAudited;
import com.yaoai.admin.common.PageResult;
import com.yaoai.admin.user.dto.ResetPasswordResponse;
import com.yaoai.admin.user.dto.UserQuery;
import com.yaoai.admin.user.dto.UserStatusRequest;
import com.yaoai.common.result.Result;
import com.yaoai.domain.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PatchMapping;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin-api/users")
@RequiredArgsConstructor
@SaCheckLogin(type = "admin")
public class UserAdminController {

    private final UserAdminService userAdminService;

    @GetMapping
    public Result<PageResult<User>> list(@ModelAttribute UserQuery query) {
        return Result.ok(userAdminService.list(query));
    }

    @GetMapping("/{id}")
    public Result<User> detail(@PathVariable Long id) {
        return Result.ok(userAdminService.detail(id));
    }

    @PatchMapping("/{id}/status")
    @AdminAudited(action = "USER_STATUS_CHANGE", resource = "USER", resourceIdParam = "id")
    @SaCheckRole(type = "admin", value = {"SUPER_ADMIN", "OPERATOR"}, mode = SaMode.OR)
    public Result<User> updateStatus(@PathVariable Long id, @RequestBody UserStatusRequest request) {
        return Result.ok(userAdminService.updateStatus(id, request));
    }

    @PostMapping("/{id}/kickout")
    @AdminAudited(action = "USER_KICKOUT", resource = "USER", resourceIdParam = "id")
    @SaCheckRole(type = "admin", value = {"SUPER_ADMIN", "OPERATOR"}, mode = SaMode.OR)
    public Result<Void> kickout(@PathVariable Long id) {
        userAdminService.kickout(id);
        return Result.ok();
    }

    @PostMapping("/{id}/reset-password")
    @AdminAudited(action = "USER_RESET_PASSWORD", resource = "USER", resourceIdParam = "id")
    @SaCheckRole(type = "admin", value = {"SUPER_ADMIN", "OPERATOR"}, mode = SaMode.OR)
    public Result<ResetPasswordResponse> resetPassword(@PathVariable Long id) {
        return Result.ok(userAdminService.resetPassword(id));
    }
}
