package cn.skcks.matrix.v2.services.casbin.init;

import cn.skcks.matrix.v2.model.casbin.CasbinConstant;
import cn.skcks.matrix.v2.model.route.dto.RouteInfo;
import cn.skcks.matrix.v2.services.casbin.CasbinRegister;
import cn.skcks.matrix.v2.services.role.RoleService;
import groovy.util.logging.Slf4j;
import lombok.RequiredArgsConstructor;
import org.casbin.jcasbin.main.Enforcer;
import org.springframework.stereotype.Component;

import java.util.List;

import static cn.skcks.matrix.v2.services.casbin.CasbinService.*;
import static cn.skcks.matrix.v2.services.casbin.CasbinService.DEFAULT_SYSTEM;

@Slf4j
@Component
@RequiredArgsConstructor
public class LocationRecordRoleRegister implements CasbinRegister {
    public static final String LOCATION_RECORD_PERMISSION = "location_record";
    public static final String LOCATION_RECORD_PERMISSION_NAME = "定位记录";

    @Override
    public void register(Enforcer enforcer, List<RouteInfo> routes, RoleService roleService) {
        List<RouteInfo> matchRoutes = routes.stream()
                .filter(routeInfo -> routeInfo.requestUrl.startsWith("/location/record"))
                .toList();
        roleService.initRole(LOCATION_RECORD_PERMISSION, LOCATION_RECORD_PERMISSION_NAME, true);
        CasbinRegister.addPolicies(enforcer, CasbinConstant.ROLE_PREFIX + LOCATION_RECORD_PERMISSION, DEFAULT_SYSTEM, matchRoutes);
    }
}
