package kz.konturai.parser.validator;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.util.List;
import java.util.Map;

public class TargetAudienceValidator implements ConstraintValidator<ValidTargetAudience, Map<String, Object>> {

    private static final List<String> VALID_GENDERS = List.of("Женщины", "Мужчины");
    private static final List<String> VALID_AGE_RANGES = List.of("20-40", "25-45", "18-25", "40-60", "60+");
    private static final List<String> VALID_TYPES = List.of("Семьи", "Молодёжь", "Все подряд");

    @Override
    public void initialize(ValidTargetAudience constraintAnnotation) {
    }

    @Override
    public boolean isValid(Map<String, Object> value, ConstraintValidatorContext context) {
        if (value == null || value.isEmpty()) {
            return false;
        }

        // Проверяем наличие хотя бы одного поля
        boolean hasGenders = value.containsKey("genders") && value.get("genders") != null;
        boolean hasAgeRanges = value.containsKey("ageRanges") && value.get("ageRanges") != null;
        boolean hasTypes = value.containsKey("types") && value.get("types") != null;

        if (!hasGenders && !hasAgeRanges && !hasTypes) {
            return false;
        }

        // Валидация genders
        if (hasGenders) {
            Object gendersObj = value.get("genders");
            if (!(gendersObj instanceof List)) {
                return false;
            }
            @SuppressWarnings("unchecked")
            List<String> genders = (List<String>) gendersObj;
            if (genders.isEmpty()) {
                return false;
            }
            for (String gender : genders) {
                if (!VALID_GENDERS.contains(gender)) {
                    return false;
                }
            }
        }

        // Валидация ageRanges
        if (hasAgeRanges) {
            Object ageRangesObj = value.get("ageRanges");
            if (!(ageRangesObj instanceof List)) {
                return false;
            }
            @SuppressWarnings("unchecked")
            List<String> ageRanges = (List<String>) ageRangesObj;
            if (ageRanges.isEmpty()) {
                return false;
            }
            for (String ageRange : ageRanges) {
                if (!VALID_AGE_RANGES.contains(ageRange)) {
                    return false;
                }
            }
        }

        // Валидация types
        if (hasTypes) {
            Object typesObj = value.get("types");
            if (!(typesObj instanceof List)) {
                return false;
            }
            @SuppressWarnings("unchecked")
            List<String> types = (List<String>) typesObj;
            if (types.isEmpty()) {
                return false;
            }
            for (String type : types) {
                if (!VALID_TYPES.contains(type)) {
                    return false;
                }
            }
        }

        return true;
    }
}

