package com.crm.rely.backend.model.aspect;

import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.StatusConstants;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.constant.CustomApplyPassedValid;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class CustomApplyPassedValidAspect {

    @Before("@annotation(customApplyPassedValid)")
    public void doBefore(JoinPoint joinPoint, CustomApplyPassedValid customApplyPassedValid) throws Throwable {

        for (Object arg : joinPoint.getArgs()) {
            if (arg instanceof CustomInfoEntity) {
                CustomInfoEntity infoEntity = (CustomInfoEntity) arg;
                if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())) {
                    throw new ServiceException(Constants.NOT_PERMIT);
                }
            }
        }
    }
}
