"""Tests for table sync type mapping."""

import pytest
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "backend"))

from sync.table_sync import map_column_type
from database.schema_reader import ColumnInfo


class TestSQLServerTypeMapping:
    def _col(self, data_type, precision=None, scale=None, max_length=None):
        return ColumnInfo(
            name="test",
            data_type=data_type,
            nullable=True,
            is_primary_key=False,
            max_length=max_length,
            numeric_precision=precision,
            numeric_scale=scale,
        )

    def test_nvarchar_to_text(self):
        assert map_column_type("nvarchar", "sqlserver", self._col("nvarchar")) == "TEXT"

    def test_varchar_to_text(self):
        assert map_column_type("varchar", "sqlserver", self._col("varchar")) == "TEXT"

    def test_int_to_integer(self):
        assert map_column_type("int", "sqlserver", self._col("int")) == "INTEGER"

    def test_bigint(self):
        assert map_column_type("bigint", "sqlserver", self._col("bigint")) == "BIGINT"

    def test_bit_to_boolean(self):
        assert map_column_type("bit", "sqlserver", self._col("bit")) == "BOOLEAN"

    def test_datetime_to_timestamp(self):
        assert map_column_type("datetime", "sqlserver", self._col("datetime")) == "TIMESTAMP"

    def test_money_to_numeric(self):
        assert map_column_type("money", "sqlserver", self._col("money")) == "NUMERIC(19,4)"

    def test_decimal_with_precision(self):
        col = self._col("decimal", precision=10, scale=2)
        result = map_column_type("decimal", "sqlserver", col)
        assert result == "NUMERIC(10,2)"

    def test_float_to_double(self):
        assert map_column_type("float", "sqlserver", self._col("float")) == "DOUBLE PRECISION"

    def test_uniqueidentifier_to_uuid(self):
        assert map_column_type("uniqueidentifier", "sqlserver", self._col("uniqueidentifier")) == "UUID"

    def test_unknown_type_defaults_to_text(self):
        assert map_column_type("some_weird_type", "sqlserver", self._col("some_weird_type")) == "TEXT"


class TestHANATypeMapping:
    def _col(self, data_type, precision=None, scale=None):
        return ColumnInfo(
            name="test",
            data_type=data_type,
            nullable=True,
            is_primary_key=False,
            max_length=None,
            numeric_precision=precision,
            numeric_scale=scale,
        )

    def test_nvarchar_to_text(self):
        assert map_column_type("nvarchar", "hana", self._col("nvarchar")) == "TEXT"

    def test_nclob_to_text(self):
        assert map_column_type("nclob", "hana", self._col("nclob")) == "TEXT"

    def test_integer(self):
        assert map_column_type("integer", "hana", self._col("integer")) == "INTEGER"

    def test_decimal_with_precision(self):
        col = self._col("decimal", precision=15, scale=3)
        result = map_column_type("decimal", "hana", col)
        assert result == "NUMERIC(15,3)"

    def test_tinyint_to_smallint(self):
        assert map_column_type("tinyint", "hana", self._col("tinyint")) == "SMALLINT"
