from backend.app.db.models.magic_plan import (
    MagicPlanDoorModel,
    MagicPlanFloorModel,
    MagicPlanPlanModel,
    MagicPlanRoomModel,
    MagicPlanWindowModel,
)

# --- MagicPlanPlan ---


def test_plan_table_name() -> None:
    assert MagicPlanPlanModel.__tablename__ == "magic_plan_plan"


def test_plan_has_magic_plan_uid_column() -> None:
    assert "magic_plan_uid" in MagicPlanPlanModel.__table__.columns


def test_plan_magic_plan_uid_is_unique() -> None:
    col = MagicPlanPlanModel.__table__.columns["magic_plan_uid"]
    assert (
        any(
            c.unique
            for c in MagicPlanPlanModel.__table__.constraints
            if hasattr(c, "columns")
            and "magic_plan_uid" in [cc.name for cc in c.columns]
        )
        or col.unique
    )


def test_plan_instantiation() -> None:
    plan = MagicPlanPlanModel(
        magic_plan_uid="uid-123", name="Test", address="1 High St", postcode="SW1A 1AA"
    )
    assert plan.magic_plan_uid == "uid-123"
    assert plan.name == "Test"
    assert plan.postcode == "SW1A 1AA"


# --- MagicPlanFloor ---


def test_floor_table_name() -> None:
    assert MagicPlanFloorModel.__tablename__ == "magic_plan_floor"


def test_floor_fk_column_name() -> None:
    assert "magic_plan_plan_id" in MagicPlanFloorModel.__table__.columns


def test_floor_has_level() -> None:
    floor = MagicPlanFloorModel(magic_plan_plan_id=1, level=0)
    assert floor.level == 0


# --- MagicPlanRoom ---


def test_room_table_name() -> None:
    assert MagicPlanRoomModel.__tablename__ == "magic_plan_room"


def test_room_fk_column_name() -> None:
    assert "magic_plan_floor_id" in MagicPlanRoomModel.__table__.columns


def test_room_has_measurement_columns() -> None:
    cols = MagicPlanRoomModel.__table__.columns
    assert "width_m" in cols
    assert "length_m" in cols
    assert "area_m2" in cols


def test_room_instantiation() -> None:
    room = MagicPlanRoomModel(
        magic_plan_floor_id=1, name="Kitchen", width_m=2.67, length_m=2.98, area_m2=7.95
    )
    assert room.name == "Kitchen"
    assert room.width_m == 2.67


# --- MagicPlanWindow ---


def test_window_table_name() -> None:
    assert MagicPlanWindowModel.__tablename__ == "magic_plan_window"


def test_window_fk_column_name() -> None:
    assert "magic_plan_room_id" in MagicPlanWindowModel.__table__.columns


def test_window_has_measurement_columns() -> None:
    cols = MagicPlanWindowModel.__table__.columns
    assert "width_m" in cols
    assert "height_m" in cols
    assert "area_m2" in cols
    assert "opening_type" in cols


def test_window_instantiation() -> None:
    window = MagicPlanWindowModel(
        magic_plan_room_id=1,
        width_m=1.4,
        height_m=1.2,
        area_m2=1.68,
        opening_type="casement",
    )
    assert window.opening_type == "casement"


# --- MagicPlanDoor ---


def test_door_table_name() -> None:
    assert MagicPlanDoorModel.__tablename__ == "magic_plan_door"


def test_door_fk_column_name() -> None:
    assert "magic_plan_room_id" in MagicPlanDoorModel.__table__.columns


def test_door_has_width_mm_and_type() -> None:
    cols = MagicPlanDoorModel.__table__.columns
    assert "width_mm" in cols
    assert "type" in cols


def test_door_instantiation() -> None:
    door = MagicPlanDoorModel(magic_plan_room_id=1, width_mm=0.79, type="hinged")
    assert door.width_mm == 0.79
    assert door.type == "hinged"
