package io.apicurio.registry.rules.validity;

import io.apicurio.registry.content.TypedContent;
import io.apicurio.registry.protobuf.rules.validity.ProtobufContentValidator;
import io.apicurio.registry.rest.v3.beans.ArtifactReference;
import io.apicurio.registry.rules.violation.RuleViolationException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Tests the Protobuf content validator.
 */
public class ProtobufContentValidatorTest extends ArtifactUtilProviderTestBase {

    @Test
    public void testValidProtobufSchema() throws Exception {
        TypedContent content = resourceToTypedContentHandle("protobuf-valid.proto");
        ProtobufContentValidator validator = new ProtobufContentValidator();
        validator.validate(ValidityLevel.SYNTAX_ONLY, content, Collections.emptyMap());
    }

    @Test
    public void testInvalidProtobufSchema() throws Exception {
        TypedContent content = resourceToTypedContentHandle("protobuf-invalid.proto");
        ProtobufContentValidator validator = new ProtobufContentValidator();
        Assertions.assertThrows(RuleViolationException.class, () -> {
            validator.validate(ValidityLevel.SYNTAX_ONLY, content, Collections.emptyMap());
        });
    }

    @Test
    public void testValidateProtobufWithImports() throws Exception {
        TypedContent mode = resourceToTypedContentHandle("mode.proto");
        TypedContent tableInfo = resourceToTypedContentHandle("table_info.proto");
        ProtobufContentValidator validator = new ProtobufContentValidator();
        validator.validate(ValidityLevel.SYNTAX_ONLY, tableInfo,
                Collections.singletonMap("sample/mode.proto", mode));
    }

    @Test
    public void testDuplicateTagNumbers() throws Exception {
        TypedContent content = resourceToTypedContentHandle("protobuf-duplicate-tags.proto");
        ProtobufContentValidator validator = new ProtobufContentValidator();
        Assertions.assertThrows(RuleViolationException.class, () -> {
            validator.validate(ValidityLevel.SYNTAX_ONLY, content, Collections.emptyMap());
        });
    }

    @Test
    public void testNegativeTagNumber() throws Exception {
        TypedContent content = resourceToTypedContentHandle("protobuf-negative-tag.proto");
        ProtobufContentValidator validator = new ProtobufContentValidator();
        Assertions.assertThrows(RuleViolationException.class, () -> {
            validator.validate(ValidityLevel.SYNTAX_ONLY, content, Collections.emptyMap());
        });
    }

    @Test
    public void testInvalidFieldType() throws Exception {
        TypedContent content = resourceToTypedContentHandle("protobuf-invalid-type.proto");
        ProtobufContentValidator validator = new ProtobufContentValidator();
        Assertions.assertThrows(RuleViolationException.class, () -> {
            validator.validate(ValidityLevel.SYNTAX_ONLY, content, Collections.emptyMap());
        });
    }

    @Test
    public void testValidProtobufSchemaWithFullValidation() throws Exception {
        TypedContent content = resourceToTypedContentHandle("protobuf-valid.proto");
        ProtobufContentValidator validator = new ProtobufContentValidator();
        // Should not throw - valid schema passes semantic validation
        validator.validate(ValidityLevel.FULL, content, Collections.emptyMap());
    }

    @Test
    public void testDuplicateTagNumbersWithDependencies() throws Exception {
        TypedContent mode = resourceToTypedContentHandle("mode.proto");
        TypedContent content = resourceToTypedContentHandle("protobuf-duplicate-tags-with-import.proto");
        ProtobufContentValidator validator = new ProtobufContentValidator();
        Assertions.assertThrows(RuleViolationException.class, () -> {
            validator.validate(ValidityLevel.SYNTAX_ONLY, content,
                    Collections.singletonMap("sample/mode.proto", mode));
        });
    }

    @Test
    public void testValidProto2WithNestedTypes() throws Exception {
        // Test proto2 schema with nested message types, enums, and default values
        // This schema is similar to what integration tests use
        TypedContent content = resourceToTypedContentHandle("protobuf-tutorial.proto");
        ProtobufContentValidator validator = new ProtobufContentValidator();
        // Should not throw - valid proto2 schema with nested types
        validator.validate(ValidityLevel.FULL, content, Collections.emptyMap());
    }

    @Test
    public void testValidProto3UuidSchema() throws Exception {
        // Test proto3 schema similar to what serdes tests register
        TypedContent content = resourceToTypedContentHandle("protobuf-uuid-simple.proto");
        ProtobufContentValidator validator = new ProtobufContentValidator();
        // Should not throw - valid proto3 schema
        validator.validate(ValidityLevel.FULL, content, Collections.emptyMap());
    }

    @Test
    public void testValidateReferences() throws Exception {
        TypedContent content = resourceToTypedContentHandle("protobuf-valid-with-refs.proto");
        ProtobufContentValidator validator = new ProtobufContentValidator();

        // Properly map both required references - success.
        {
            List<ArtifactReference> references = new ArrayList<>();
            references.add(ArtifactReference.builder().groupId("default").artifactId("message2.proto")
                    .version("1.0").name("message2.proto").build());
            references.add(ArtifactReference.builder().groupId("default").artifactId("message3.proto")
                    .version("1.1").name("message3.proto").build());
            validator.validateReferences(content, references);
        }

        // Don't map either of the required references - failure.
        Assertions.assertThrows(RuleViolationException.class, () -> {
            List<ArtifactReference> references = new ArrayList<>();
            validator.validateReferences(content, references);
        });

        // Only map one of the two required refs - failure.
        Assertions.assertThrows(RuleViolationException.class, () -> {
            List<ArtifactReference> references = new ArrayList<>();
            references.add(ArtifactReference.builder().groupId("default").artifactId("message2.proto")
                    .version("1.0").name("message2.proto").build());
            validator.validateReferences(content, references);
        });

        // Only map one of the two required refs - failure.
        Assertions.assertThrows(RuleViolationException.class, () -> {
            List<ArtifactReference> references = new ArrayList<>();
            references.add(ArtifactReference.builder().groupId("default").artifactId("message2.proto")
                    .version("1.0").name("message2.proto").build());
            references.add(ArtifactReference.builder().groupId("default").artifactId("message4.proto")
                    .version("4.0").name("message4.proto").build());
            validator.validateReferences(content, references);
        });
    }

}
