package com.ecep.contract.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ecep.contract.SpringApp;
import com.ecep.contract.ds.other.service.EmployeeService;
import com.ecep.contract.vo.EmployeeVo;

/**
 * 基础控制器，处理根路径请求
 */
@RestController
public class IndexController {

    /**
     * 处理根路径请求，返回系统状态信息
     */
    @GetMapping("/")
    public ResponseEntity<?> index() {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();

        Object principal = authentication.getPrincipal();
        if (principal instanceof UserDetails userDetails) {
            System.out.println(userDetails.getUsername());
            EmployeeService employeeService = SpringApp.getBean(EmployeeService.class);
            try {
                EmployeeVo employeeVo = employeeService.findByAccount(userDetails.getUsername());
                return ResponseEntity.ok(employeeVo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return ResponseEntity.ok(authentication);
    }

}