"""Tests for Doris/MySQL-compatible SQLAlchemy type mapping."""

import pytest
from sqlalchemy import BigInteger, DateTime, Float, Integer, String, Text
from sqlalchemy.dialects.mysql import TINYINT


def test_map_col_type_doris_bigint():
    from db_core.schema.sqlalchemy_gen import _map_col_type_doris

    result = _map_col_type_doris("BIGINT")
    assert isinstance(result, BigInteger)


def test_map_col_type_doris_integer():
    from db_core.schema.sqlalchemy_gen import _map_col_type_doris

    result = _map_col_type_doris("INTEGER")
    assert isinstance(result, Integer)


def test_map_col_type_doris_uuid():
    from db_core.schema.sqlalchemy_gen import _map_col_type_doris

    result = _map_col_type_doris("UUID")
    assert isinstance(result, String)
    assert result.length == 36


def test_map_col_type_doris_text():
    from db_core.schema.sqlalchemy_gen import _map_col_type_doris

    result = _map_col_type_doris("TEXT")
    assert isinstance(result, Text)


def test_map_col_type_doris_boolean():
    from db_core.schema.sqlalchemy_gen import _map_col_type_doris

    result = _map_col_type_doris("BOOLEAN")
    assert isinstance(result, TINYINT)


def test_map_col_type_doris_double():
    from db_core.schema.sqlalchemy_gen import _map_col_type_doris

    result = _map_col_type_doris("DOUBLE")
    assert isinstance(result, Float)


def test_map_col_type_doris_double_precision():
    from db_core.schema.sqlalchemy_gen import _map_col_type_doris

    result = _map_col_type_doris("DOUBLE PRECISION")
    assert isinstance(result, Float)


def test_map_col_type_doris_datetime():
    from db_core.schema.sqlalchemy_gen import _map_col_type_doris

    result = _map_col_type_doris("DATETIME")
    assert isinstance(result, DateTime)


def test_map_col_type_doris_timestamptz():
    from db_core.schema.sqlalchemy_gen import _map_col_type_doris

    result = _map_col_type_doris("TIMESTAMPTZ")
    assert isinstance(result, DateTime)


def test_map_col_type_doris_json():
    from db_core.schema.sqlalchemy_gen import _map_col_type_doris

    result = _map_col_type_doris("JSON")
    # Should be MySQL JSON type (or Text fallback)
    assert result is not None


def test_map_col_type_doris_jsonb():
    from db_core.schema.sqlalchemy_gen import _map_col_type_doris

    result = _map_col_type_doris("JSONB")
    # Should be MySQL JSON type (or Text fallback)
    assert result is not None


def test_map_col_type_doris_varchar():
    from db_core.schema.sqlalchemy_gen import _map_col_type_doris

    result = _map_col_type_doris("VARCHAR(255)")
    assert isinstance(result, String)
    assert result.length == 255


def test_map_col_type_doris_unknown_raises():
    from db_core.schema.sqlalchemy_gen import _map_col_type_doris

    with pytest.raises(ValueError, match="Unknown col_type"):
        _map_col_type_doris("UNKNOWN_TYPE_XYZ")


def test_map_col_type_doris_postgresql_original_still_works():
    """Sanity check: original PostgreSQL mapper still works."""
    from db_core.schema.sqlalchemy_gen import _map_col_type

    result = _map_col_type("BIGINT")
    assert isinstance(result, BigInteger)

    result = _map_col_type("UUID")
    # PostgreSQL UUID is different from Doris String(36)
    assert result is not None
