package com.yaoai.admin.adminuser;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.yaoai.admin.adminuser.dto.AdminUserCreateRequest;
import com.yaoai.admin.adminuser.dto.AdminUserQuery;
import com.yaoai.admin.adminuser.dto.AdminUserUpdateRequest;
import com.yaoai.admin.audit.annotation.AdminAudited;
import com.yaoai.admin.common.PageResult;
import com.yaoai.admin.user.dto.ResetPasswordResponse;
import com.yaoai.common.result.Result;
import com.yaoai.domain.entity.AdminUser;
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/admin-users")
@RequiredArgsConstructor
@SaCheckLogin(type = "admin")
@SaCheckRole(type = "admin", value = {"SUPER_ADMIN"})
public class AdminUserAdminController {

    private final AdminUserAdminService adminUserAdminService;

    @GetMapping
    public Result<PageResult<AdminUser>> list(@ModelAttribute AdminUserQuery query) {
        return Result.ok(adminUserAdminService.list(query));
    }

    @PostMapping
    @AdminAudited(action = "ADMIN_USER_CREATE", resource = "ADMIN_USER", resourceIdParam = "username")
    public Result<AdminUser> create(@RequestBody AdminUserCreateRequest request) {
        return Result.ok(adminUserAdminService.create(request));
    }

    @PatchMapping("/{id}")
    @AdminAudited(action = "ADMIN_USER_UPDATE", resource = "ADMIN_USER", resourceIdParam = "id")
    public Result<AdminUser> update(@PathVariable Long id, @RequestBody AdminUserUpdateRequest request) {
        return Result.ok(adminUserAdminService.update(id, request));
    }

    @PostMapping("/{id}/reset-password")
    @AdminAudited(action = "ADMIN_USER_RESET_PASSWORD", resource = "ADMIN_USER", resourceIdParam = "id")
    public Result<ResetPasswordResponse> resetPassword(@PathVariable Long id) {
        return Result.ok(adminUserAdminService.resetPassword(id));
    }
}
