"""Tests para validate_json_schema."""

import sys
import os

sys.path.insert(0, os.path.dirname(__file__))

from validate_json_schema import validate_json_schema


def test_schema_simple_con_types_y_required():
    schema = {
        "type": "object",
        "required": ["name", "age"],
        "properties": {
            "name": {"type": "string"},
            "age": {"type": "integer"},
        },
    }
    valid, errors = validate_json_schema({"name": "Alice", "age": 30}, schema)
    assert valid is True
    assert errors == []


def test_nested_object_validation():
    schema = {
        "type": "object",
        "properties": {
            "address": {
                "type": "object",
                "required": ["zip"],
                "properties": {
                    "zip": {"type": "string"},
                    "street": {"type": "string"},
                },
            }
        },
    }
    valid, errors = validate_json_schema(
        {"address": {"zip": 12345, "street": "Main St"}}, schema
    )
    assert valid is False
    assert any("$.address.zip" in e for e in errors)


def test_array_con_items_schema():
    schema = {
        "type": "array",
        "items": {"type": "string"},
        "minItems": 1,
        "maxItems": 3,
    }
    valid, errors = validate_json_schema(["a", "b"], schema)
    assert valid is True

    valid2, errors2 = validate_json_schema(["a", 2, "c"], schema)
    assert valid2 is False
    assert any("[1]" in e for e in errors2)


def test_pattern_validation_en_strings():
    schema = {
        "type": "object",
        "properties": {
            "email": {"type": "string", "pattern": r"^[^@]+@[^@]+\.[^@]+$"},
        },
    }
    valid, errors = validate_json_schema({"email": "user@example.com"}, schema)
    assert valid is True

    valid2, errors2 = validate_json_schema({"email": "not-an-email"}, schema)
    assert valid2 is False
    assert any("pattern" in e for e in errors2)


def test_numeric_ranges():
    schema = {
        "type": "integer",
        "minimum": 0,
        "maximum": 100,
    }
    valid, _ = validate_json_schema(50, schema)
    assert valid is True

    valid2, errors2 = validate_json_schema(150, schema)
    assert valid2 is False
    assert any("maximum" in e for e in errors2)


def test_multiples_errores_acumulados_con_paths_correctos():
    schema = {
        "type": "object",
        "required": ["name", "age", "email"],
        "properties": {
            "name": {"type": "string"},
            "age": {"type": "integer", "minimum": 0, "maximum": 120},
            "email": {"type": "string", "pattern": r"^[^@]+@[^@]+$"},
        },
    }
    data = {"name": 123, "age": -5, "email": "bad"}
    valid, errors = validate_json_schema(data, schema)
    assert valid is False
    # name falla type, age falla minimum, email falla pattern
    assert len(errors) >= 2
    paths = [e.split(":")[0] for e in errors]
    assert "$.name" in paths or any("$.name" in e for e in errors)


def test_schema_vacio_acepta_todo():
    valid, errors = validate_json_schema({"any": "thing"}, {})
    assert valid is True
    assert errors == []

    valid2, errors2 = validate_json_schema([1, 2, 3], {})
    assert valid2 is True
    assert errors2 == []


def test_data_none_contra_required_field():
    schema = {
        "type": "object",
        "required": ["name"],
        "properties": {
            "name": {"type": "string"},
        },
    }
    valid, errors = validate_json_schema({"name": None}, schema)
    assert valid is False
    assert any("name" in e for e in errors)
