package com.nbport.zgwl.utils;

import com.nbport.zgwl.entity.SysOrgEntity;
import com.nbport.zgwl.entity.SysUserEntity;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 登录用户获取组件
 *
 * 当前系统还没有真正接入统一认证，
 * 所以这里先返回一个 mock 用户，便于业务代码先跑通。
 * 后续接入真实系统时，直接把这里替换成真正的登录态获取逻辑即可。
 */
@Component
public class AdminSecurityManage {

    private static final String HEADER_MOCK_USER = "X-Mock-User";

    private final HttpServletRequest request;
    private final String defaultMockUser;

    public AdminSecurityManage(HttpServletRequest request,
                               @Value("${mock.security.current-user:admin}") String defaultMockUser) {
        this.request = request;
        this.defaultMockUser = defaultMockUser;
    }

    public SysUserEntity getUser() {
        String mockUser = request == null ? null : request.getHeader(HEADER_MOCK_USER);
        if (mockUser == null || mockUser.isBlank()) {
            mockUser = defaultMockUser;
        }
        if ("manage_admin".equalsIgnoreCase(mockUser)) {
            return buildManageAdmin();
        }
        if ("business_admin".equalsIgnoreCase(mockUser)) {
            return buildBusinessAdmin();
        }
        if ("business_user".equalsIgnoreCase(mockUser)) {
            return buildBusinessUser();
        }
        return buildSuperAdmin();
    }

    private SysUserEntity buildSuperAdmin() {
        return buildUser(
                1L,
                "admin",
                "13800000000",
                "admin@test.com",
                buildOrg("1", "浙江海港物流集团有限公司X"),
                "平台管理部",
                1L,
                "ALL",
                List.of("SUPER_ADMIN", "MANAGE_ADMIN", "BUSINESS_ADMIN")
        );
    }

    private SysUserEntity buildManageAdmin() {
        return buildUser(
                2L,
                "manage_admin",
                "13800000001",
                "manage_admin@test.com",
                buildOrg("1", "浙江海港物流集团有限公司X"),
                "财务部",
                1L,
                "COMPANY",
                List.of("MANAGE_ADMIN")
        );
    }

    private SysUserEntity buildBusinessAdmin() {
        return buildUser(
                3L,
                "business_admin",
                "13800000002",
                "business_admin@test.com",
                buildOrg("384", "江苏远洋新世纪2供应链有限公司"),
                "业务一部",
                1L,
                "COMPANY",
                List.of("BUSINESS_ADMIN")
        );
    }

    private SysUserEntity buildBusinessUser() {
        return buildUser(
                4L,
                "business_user",
                "13800000003",
                "business_user@test.com",
                buildOrg("384", "江苏远洋新世纪2供应链有限公司"),
                "业务一部",
                0L,
                "SELF",
                List.of("BUSINESS_USER")
        );
    }

    private SysUserEntity buildUser(Long id,
                                    String username,
                                    String mobilePhone,
                                    String email,
                                    SysOrgEntity orgEntity,
                                    String deptName,
                                    Long adminFlag,
                                    String dataPermission,
                                    List<String> roleCodes) {
        SysUserEntity user = new SysUserEntity();
        user.setId(id);
        user.setUsername(username);
        user.setMobilePhone(mobilePhone);
        user.setEmail(email);
        user.setSysOrgEntity(orgEntity);
        user.setDeptName(deptName);
        user.setAdminFlag(adminFlag);
        user.setDataPermission(dataPermission);
        user.setRoleCodes(roleCodes);
        return user;
    }

    private SysOrgEntity buildOrg(String orgId, String orgName) {
        SysOrgEntity orgEntity = new SysOrgEntity();
        orgEntity.setId(orgId);
        orgEntity.setOrgName(orgName);
        orgEntity.setContactPhone("13800000000");
        orgEntity.setContactEmail("admin@test.com");
        orgEntity.setContactAddress("宁波市北仑区明州路288号");
        orgEntity.setRegisteredAddress("宁波市北仑区明州路288号");
        orgEntity.setBankName("中国工商银行宁波分行");
        orgEntity.setBankAccount("330201000000000001");
        orgEntity.setLegalRepresentative("张三");
        orgEntity.setCreditCode("91330200MAZGWL0001");
        orgEntity.setInvoiceTitleOptions(List.of("浙港物流平台有限公司"));
        return orgEntity;
    }
}
