package cn.novalon.manage.db.repository;

import cn.novalon.manage.sys.core.domain.SysPermission;
import cn.novalon.manage.sys.core.repository.ISysPermissionRepository;
import cn.novalon.manage.db.converter.SysPermissionConverter;
import cn.novalon.manage.db.dao.SysPermissionDao;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 权限仓储实现类
 * 
 * @author 张翔
 * @date 2026-03-25
 */
@Repository
public class SysPermissionRepository implements ISysPermissionRepository {

    private final SysPermissionDao sysPermissionDao;
    private final SysPermissionConverter sysPermissionConverter;

    public SysPermissionRepository(SysPermissionDao sysPermissionDao, SysPermissionConverter sysPermissionConverter) {
        this.sysPermissionDao = sysPermissionDao;
        this.sysPermissionConverter = sysPermissionConverter;
    }

    @Override
    public Mono<SysPermission> findById(Long id) {
        return sysPermissionDao.findByIdAndDeletedAtIsNull(id)
                .map(sysPermissionConverter::toDomain);
    }

    @Override
    public Mono<SysPermission> findByIdIncludingDeleted(Long id) {
        return sysPermissionDao.findById(id)
                .map(sysPermissionConverter::toDomain);
    }

    @Override
    public Mono<SysPermission> save(SysPermission sysPermission) {
        return sysPermissionDao.save(sysPermissionConverter.toEntity(sysPermission))
                .map(sysPermissionConverter::toDomain);
    }

    @Override
    public Mono<Void> deleteById(Long id) {
        return sysPermissionDao.deleteById(id);
    }

    @Override
    public Flux<SysPermission> findAll() {
        return sysPermissionDao.findByDeletedAtIsNull()
                .map(sysPermissionConverter::toDomain);
    }

    @Override
    public Flux<SysPermission> findAll(Sort sort) {
        return sysPermissionDao.findByDeletedAtIsNull(sort)
                .map(sysPermissionConverter::toDomain);
    }

    @Override
    public Mono<SysPermission> findByPermissionCode(String permissionCode) {
        return sysPermissionDao.findByPermissionCodeAndDeletedAtIsNull(permissionCode)
                .map(sysPermissionConverter::toDomain);
    }

    @Override
    public Mono<Long> count() {
        return sysPermissionDao.countByDeletedAtIsNull();
    }

    @Override
    public Mono<Boolean> existsByPermissionCode(String permissionCode) {
        return sysPermissionDao.existsByPermissionCodeAndDeletedAtIsNull(permissionCode);
    }

    @Override
    public Mono<SysPermission> updatePermission(SysPermission permission) {
        return sysPermissionDao.save(sysPermissionConverter.toEntity(permission))
                .map(sysPermissionConverter::toDomain);
    }

    @Override
    public Flux<SysPermission> findByRoleId(Long roleId) {
        return sysPermissionDao.findByRoleId(roleId)
                .map(sysPermissionConverter::toDomain);
    }

    @Override
    public Flux<SysPermission> findByRoleIds(java.util.List<Long> roleIds) {
        return sysPermissionDao.findByRoleIds(roleIds)
                .map(sysPermissionConverter::toDomain);
    }
}