package com.eshore.gringotts.web.domain.controller;

import cn.hutool.core.util.ObjectUtil;
import com.eshore.gringotts.web.configuration.amis.AmisListResponse;
import com.eshore.gringotts.web.configuration.amis.AmisResponse;
import com.eshore.gringotts.web.domain.entity.User;
import com.eshore.gringotts.web.domain.service.UserService;
import java.time.LocalDateTime;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户管理
 *
 * @author lanyuanxiaoyao
 * @date 2024-11-15
 */
@RestController
@RequestMapping("/user_management")
public class UserManagementController {
    private static final Logger logger = LoggerFactory.getLogger(UserManagementController.class);

    private final UserService userService;

    public UserManagementController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/list")
    public AmisListResponse list() {
        return AmisListResponse.responseListData(userService.list().collect(UserListItem::new));
    }

    @GetMapping("/detail/{username}")
    public AmisResponse<UserDetail> detail(@PathVariable String username) {
        return AmisResponse.responseSuccess(new UserDetail(userService.detail(username)));
    }

    @PostMapping("/register")
    public void register(@RequestBody RegisterRequest request) {
        logger.info("Register request: {}", request);
        userService.registerFromAdministrator(request.username, request.password, request.role);
    }

    @PostMapping("/change_password")
    public AmisResponse<Object> changePassword(@RequestBody ChangePasswordRequest request) {
        userService.changePassword(request.oldPassword, request.newPassword);
        return AmisResponse.responseSuccess();
    }

    @GetMapping("/disable/{username}")
    public AmisResponse<Object> disable(@PathVariable String username) {
        userService.disable(username);
        return AmisResponse.responseSuccess();
    }

    @GetMapping("/enable/{username}")
    public AmisResponse<Object> enable(@PathVariable String username) {
        userService.enable(username);
        return AmisResponse.responseSuccess();
    }

    @GetMapping("/check/{username}")
    public AmisResponse<Object> check(@PathVariable String username) {
        userService.check(username);
        return AmisResponse.responseSuccess();
    }

    @Data
    public static final class ChangePasswordRequest {
        private String oldPassword;
        private String newPassword;
    }

    @Data
    public static final class RegisterRequest {
        private String username;
        private String password;
        private User.Role role;
    }

    @Data
    public static final class UserListItem {
        private Long id;
        private String username;
        private User.Role role;
        private User.State state;
        private LocalDateTime lastLoginTime;
        private LocalDateTime createdTime;

        public UserListItem(User user) {
            this.id = user.getId();
            this.username = user.getUsername();
            this.role = user.getRole();
            this.state = user.getState();
            this.lastLoginTime = user.getLastLoginTime();
            this.createdTime = user.getCreatedTime();
        }
    }

    @Data
    public static final class UserDetail {
        private String username;
        private User.Role role;
        private User.State state;
        private LocalDateTime lastLoginTime;
        private LocalDateTime createdTime;
        private String createdUsername;
        private LocalDateTime modifiedTime;
        private String modifiedUsername;
        private LocalDateTime checkedTime;
        private String checkedUsername;

        public UserDetail(User user) {
            this.username = user.getUsername();
            this.role = user.getRole();
            this.state = user.getState();
            this.lastLoginTime = user.getLastLoginTime();
            this.createdTime = user.getCreatedTime();
            this.createdUsername = user.getCreatedUser().getUsername();
            this.modifiedTime = user.getModifiedTime();
            this.modifiedUsername = user.getModifiedUser().getUsername();
            if (ObjectUtil.isNotNull(user.getCheckedUser())) {
                this.checkedTime = user.getCheckedTime();
                this.checkedUsername = user.getCheckedUser().getUsername();
            }
        }
    }
}
