package org.cmh.backend.UserManagement.controller;

import jakarta.transaction.Transactional;
import org.cmh.backend.UserManagement.dto.TenantOptionsResponse;
import org.cmh.backend.UserManagement.model.Tenant;
import org.cmh.backend.UserManagement.model.User;
import org.cmh.backend.UserManagement.service.TenantManagementService;
import org.cmh.backend.Utils.JwtUtil;
import org.cmh.backend.Utils.JwtVerify;
import org.cmh.backend.authentication.model.UserHS;
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.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/tenant")
public class TenantManagementController {
    @Autowired
    private TenantManagementService tenantManagementService;
    @Autowired
    private UserService userService;

    private boolean isAdmin(String token) {
        UserHS user = userService.getUserByUsername(JwtUtil.extractUsername(token));
        return user.getSuperAdmin();
    }

    @PostMapping("/addTenant")
    @JwtVerify
    public Tenant addTenant(@RequestBody Tenant tenant, @RequestParam String token) {
        if (isAdmin(token)) {
            Tenant tenant1 = tenantManagementService.registerTenant(tenant);
            UserHS newUser = new UserHS();
            newUser.setUsername(tenant.getManager());
            newUser.setPassword(tenant.getSymbol());
            newUser.setRole("企业管理员");
            newUser.setTenant(tenant.getName());
            newUser.setGender("男");
            newUser.setNickname(tenant.getManager());
            newUser.setEmail(tenant.getContact());
            newUser.setDepartment("企业管理员");
            newUser.setCreatedAt(LocalDateTime.now());
            newUser.setPhoneNumber(tenant.getPhone());
            userService.addUser(newUser);
            System.out.println(tenant.toString());
            return tenant1;
        }
        return null;
    }

    @GetMapping("/getAllTenant")
    @JwtVerify
    public List<Tenant> getAll(@RequestParam String token) {
        if (isAdmin(token)) {
            return tenantManagementService.findAll();
        } else {
            return null;
        }
    }

    @GetMapping("/getTenantOptions")
    @JwtVerify
    public ResponseEntity<TenantOptionsResponse> getAllForNews(@RequestParam String token) {
        tenantManagementService.findAll();
        try {
            List<String> options = tenantManagementService.findAll().stream()
                    .map(Tenant::getName)
                    .toList();
            return new ResponseEntity<>(new TenantOptionsResponse(options, "请求成功"), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new TenantOptionsResponse(null, "请求失败" + e.getMessage()), HttpStatus.BAD_REQUEST);
        }
    }

    @PostMapping("/updateTenant")
    @JwtVerify
    public Tenant update(@RequestBody Tenant tenant, @RequestParam String token) {
        if (isAdmin(token)) {
            tenantManagementService.update(tenant);
            return tenant;
        }
        return null;
    }

    @PostMapping("/deleteTenant")
    @Transactional
    @JwtVerify
    public void delete(@RequestBody User user, @RequestParam String token) {
        if (isAdmin(token)){
            tenantManagementService.delete(user.getId());
        }
    }

}

