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 PermissionService {

    private final PermissionRepository permissionRepository;

    public PermissionService(PermissionRepository permissionRepository) {
        this.permissionRepository = permissionRepository;
    }

    /**
     * 查询所有权限
     */
    @Transactional(readOnly = true)
    public List<SysPermission> findAll() {
        return permissionRepository.findAll();
    }

    /**
     * 根据ID查询权限
     */
    @Transactional(readOnly = true)
    public Optional<SysPermission> findById(Long id) {
        return permissionRepository.findById(id);
    }

    /**
     * 根据权限代码查询
     */
    @Transactional(readOnly = true)
    public Optional<SysPermission> findByPermissionCode(String permissionCode) {
        return permissionRepository.findByPermissionCode(permissionCode);
    }

    /**
     * 根据模块代码查询
     */
    @Transactional(readOnly = true)
    public List<SysPermission> findByModuleCode(String moduleCode) {
        return permissionRepository.findByModuleCode(moduleCode);
    }

    /**
     * 创建权限
     */
    public SysPermission save(SysPermission permission) {
        if (permissionRepository.existsByPermissionCode(permission.getPermissionCode())) {
            throw new IllegalArgumentException("权限代码已存在: " + permission.getPermissionCode());
        }
        if (permission.getStatus() == null) {
            permission.setStatus("ENABLED");
        }
        return permissionRepository.save(permission);
    }

    /**
     * 更新权限
     */
    public Optional<SysPermission> update(Long id, SysPermission permissionData) {
        return permissionRepository.findById(id)
                .map(existing -> {
                    if (permissionData.getPermissionName() != null) {
                        existing.setPermissionName(permissionData.getPermissionName());
                    }
                    if (permissionData.getDescription() != null) {
                        existing.setDescription(permissionData.getDescription());
                    }
                    if (permissionData.getStatus() != null) {
                        existing.setStatus(permissionData.getStatus());
                    }
                    if (permissionData.getSortOrder() != null) {
                        existing.setSortOrder(permissionData.getSortOrder());
                    }
                    return permissionRepository.save(existing);
                });
    }

    /**
     * 删除权限
     */
    public void delete(Long id) {
        permissionRepository.deleteById(id);
    }

    /**
     * 检查权限代码是否存在
     */
    @Transactional(readOnly = true)
    public boolean existsByPermissionCode(String permissionCode) {
        return permissionRepository.existsByPermissionCode(permissionCode);
    }
}
