package com.mosquito.project.permission;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 角色Service - 角色管理核心业务逻辑
 */
@Service
@Transactional
public class RoleService {

    private final RoleRepository roleRepository;

    public RoleService(RoleRepository roleRepository) {
        this.roleRepository = roleRepository;
    }

    /**
     * 查询所有角色
     */
    @Transactional(readOnly = true)
    public List<SysRole> findAll() {
        return roleRepository.findAll();
    }

    /**
     * 根据ID查询角色
     */
    @Transactional(readOnly = true)
    public Optional<SysRole> findById(Long id) {
        return roleRepository.findById(id);
    }

    /**
     * 根据角色代码查询
     */
    @Transactional(readOnly = true)
    public Optional<SysRole> findByRoleCode(String roleCode) {
        return roleRepository.findByRoleCodeAndDeleted(roleCode, 0);
    }

    /**
     * 创建角色
     */
    public SysRole save(SysRole role) {
        // 检查角色代码是否已存在
        if (roleRepository.existsByRoleCode(role.getRoleCode())) {
            throw new IllegalArgumentException("角色代码已存在: " + role.getRoleCode());
        }
        // 设置默认值
        if (role.getDeleted() == null) {
            role.setDeleted(0);
        }
        if (role.getStatus() == null) {
            role.setStatus("ENABLED");
        }
        if (role.getIsCore() == null) {
            role.setIsCore(0);
        }
        return roleRepository.save(role);
    }

    /**
     * 更新角色
     */
    public Optional<SysRole> update(Long id, SysRole roleData) {
        return roleRepository.findById(id)
                .map(existingRole -> {
                    // 更新字段
                    if (roleData.getRoleName() != null) {
                        existingRole.setRoleName(roleData.getRoleName());
                    }
                    if (roleData.getRoleLevel() != null) {
                        existingRole.setRoleLevel(roleData.getRoleLevel());
                    }
                    if (roleData.getDataScope() != null) {
                        existingRole.setDataScope(roleData.getDataScope());
                    }
                    if (roleData.getDescription() != null) {
                        existingRole.setDescription(roleData.getDescription());
                    }
                    if (roleData.getStatus() != null) {
                        existingRole.setStatus(roleData.getStatus());
                    }
                    if (roleData.getIsCore() != null) {
                        existingRole.setIsCore(roleData.getIsCore());
                    }
                    return roleRepository.save(existingRole);
                });
    }

    /**
     * 删除角色（软删除）
     */
    public void delete(Long id) {
        roleRepository.findById(id)
                .ifPresent(role -> {
                    role.setDeleted(1);
                    role.setStatus("DISABLED");
                    roleRepository.save(role);
                });
    }

    /**
     * 检查角色代码是否存在
     */
    @Transactional(readOnly = true)
    public boolean existsByRoleCode(String roleCode) {
        return roleRepository.existsByRoleCode(roleCode);
    }
}
