package org.cmh.backend.UserManagement.controller;

import jakarta.transaction.Transactional;
import org.cmh.backend.OrganizationManagement.service.OrganizationService;
import org.cmh.backend.UserManagement.adpter.User2UserHS;
import org.cmh.backend.UserManagement.adpter.UserHS2User;
import org.cmh.backend.UserManagement.model.User;
import org.cmh.backend.UserManagement.service.UserManagementService;
import org.cmh.backend.Utils.JwtUtil;
import org.cmh.backend.Utils.JwtVerify;
import org.cmh.backend.authentication.dto.UserProfileResponse;
import org.cmh.backend.authentication.model.UserHS;
import org.cmh.backend.authentication.repository.UserRepository;
import org.cmh.backend.authentication.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserManagementController {
    @Autowired
    private UserManagementService userManagementService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserRepository userRepository;

    @PostMapping("/addUser")
    @JwtVerify
    public ResponseEntity<UserProfileResponse> addUser(@RequestBody User user,@RequestParam String token) {
        String username = JwtUtil.extractUsername(token);
        UserHS operationUser = userService.getUserByUsername(username);

        UserHS newuser = User2UserHS.convert(user,operationUser.getTenant());
        UserProfileResponse response = new UserProfileResponse(
                newuser.getUsername(),
                newuser.getNickname(),
                newuser.getGender(),
                newuser.getPhoneNumber(),
                newuser.getEmail(),
                newuser.getDepartment(),
                newuser.getRole(),
                newuser.getCreatedAt()
        );
        if (organizationService.getByName(newuser.getDepartment()) != null) {
            userService.addUser(newuser);
            return new ResponseEntity<>(response, HttpStatus.OK);
        } else {
            return null;
        }
    }


    @GetMapping("/getAll")
    @JwtVerify
    public List<User> getAll(@RequestParam String token) {
        String username = JwtUtil.extractUsername(token);
        UserHS userHS = userService.getUserByUsername(username);
        if (userHS.getSuperAdmin()) {
            return UserHS2User.convertList(userService.getAllUsers());
        } else {
            return UserHS2User.convertList(userService.getUsersByTenant(userHS.getTenant()));
        }
    }


    @PostMapping("/update")
    public ResponseEntity<UserProfileResponse> update(@RequestBody User user,@RequestParam String token) {
        String username = JwtUtil.extractUsername(token);
        UserHS operationUser = userService.getUserByUsername(username);
        UserHS reqUser = User2UserHS.convert(user, operationUser.getTenant());
        UserHS tarUser = userService.getUserByUsername(reqUser.getUsername());
        if (tarUser != null) {
//            tarUser.setNickname(reqUser.getNickname());
//            tarUser.setGender(reqUser.getGender());
//            tarUser.setPhoneNumber(reqUser.getPhoneNumber());
//            tarUser.setEmail(reqUser.getEmail());
//            tarUser.setRole(reqUser.getRole());
            reqUser.setId(tarUser.getId());
            UserProfileResponse response = new UserProfileResponse(
                    reqUser.getUsername(),
                    reqUser.getNickname(),
                    reqUser.getGender(),
                    reqUser.getPhoneNumber(),
                    reqUser.getEmail(),
                    reqUser.getDepartment(),
                    reqUser.getRole(),
                    reqUser.getCreatedAt()
            );
            if (organizationService.getByName(reqUser.getDepartment()) == null) {
                return new ResponseEntity<>(null, HttpStatus.OK);
            }
            userRepository.save(reqUser);
            return new ResponseEntity<>(response, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(null, HttpStatus.OK);
        }
    }


    @PostMapping("/delete")
    @Transactional
    public void delete(@RequestBody User user) {
        UserHS userHS = userService.getUserByUsername(user.getName());
        if (userHS != null) {
            //根据用户名删除用户
            userService.deleteUser(userHS.getUsername());
        }
    }
}

