package com.qqchen.deploy.backend.system.service.impl;

import com.qqchen.deploy.backend.system.converter.PermissionConverter;
import com.qqchen.deploy.backend.system.entity.Menu;
import com.qqchen.deploy.backend.system.entity.Permission;
import com.qqchen.deploy.backend.framework.annotation.ServiceType;
import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
import com.qqchen.deploy.backend.system.model.PermissionDTO;
import com.qqchen.deploy.backend.system.model.query.PermissionQuery;
import com.qqchen.deploy.backend.system.repository.IMenuRepository;
import com.qqchen.deploy.backend.system.repository.IPermissionRepository;
import com.qqchen.deploy.backend.system.service.IPermissionService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.qqchen.deploy.backend.framework.annotation.ServiceType.Type.DATABASE;

@Slf4j
@Service
@ServiceType(DATABASE)
public class PermissionServiceImpl extends BaseServiceImpl<Permission, PermissionDTO, PermissionQuery, Long> implements IPermissionService {

    @Resource
    private IPermissionRepository permissionRepository;

    @Resource
    private IMenuRepository menuRepository;

    @Resource
    private PermissionConverter permissionConverter;

    @Override
    public List<PermissionDTO> listAllEnabled() {
        List<Permission> permissions = permissionRepository.findAllEnabledOrderByMenuAndSort();
        List<Long> menuIds = permissions.stream().map(Permission::getMenuId).distinct().collect(Collectors.toList());
        Map<Long, String> menuNameMap = menuRepository.findAllById(menuIds).stream()
            .collect(Collectors.toMap(Menu::getId, Menu::getName));

        return permissions.stream().map(p -> convertToDTO(p, menuNameMap.get(p.getMenuId())))
            .collect(Collectors.toList());
    }

    @Override
    public List<PermissionDTO> listByMenuId(Long menuId) {
        List<Permission> permissions = permissionRepository.findByMenuIdAndEnabledTrue(menuId);
        String menuName = menuRepository.findById(menuId).map(Menu::getName).orElse(null);
        return permissions.stream().map(p -> convertToDTO(p, menuName)).collect(Collectors.toList());
    }

    @Override
    public List<PermissionDTO> listByIds(List<Long> ids) {
        List<Permission> permissions = permissionRepository.findByIdIn(ids);
        List<Long> menuIds = permissions.stream().map(Permission::getMenuId).distinct().collect(Collectors.toList());
        Map<Long, String> menuNameMap = menuRepository.findAllById(menuIds).stream()
            .collect(Collectors.toMap(Menu::getId, Menu::getName));

        return permissions.stream().map(p -> convertToDTO(p, menuNameMap.get(p.getMenuId())))
            .collect(Collectors.toList());
    }

    private PermissionDTO convertToDTO(Permission permission, String menuName) {
        PermissionDTO dto = permissionConverter.toDto(permission);
        dto.setMenuName(menuName);
        return dto;
    }
}