from models.zone import Zone
import os


def test_zone():
    """Test Zone model CRUD operations."""
    if os.path.exists("Zone.json"):
        os.remove("Zone.json")

    zones = Zone()

    print("Testing create zone")
    zone_id = zones.create("test_zone", ["1", "2", "3"], ["preset1", "preset2"])
    print(f"Created zone with ID: {zone_id}")
    assert zone_id is not None
    assert zone_id in zones

    print("\nTesting read zone")
    zone = zones.read(zone_id)
    print(f"Read: {zone}")
    assert zone is not None
    assert zone["name"] == "test_zone"
    assert len(zone["names"]) == 3
    assert len(zone["presets"]) == 2

    print("\nTesting update zone")
    update_data = {
        "name": "updated_zone",
        "names": ["4", "5"],
        "presets": ["preset3"],
    }
    result = zones.update(zone_id, update_data)
    assert result is True
    updated = zones.read(zone_id)
    assert updated["name"] == "updated_zone"
    assert len(updated["names"]) == 2
    assert len(updated["presets"]) == 1

    print("\nTesting list zones")
    zone_list = zones.list()
    print(f"Zone list: {zone_list}")
    assert zone_id in zone_list

    print("\nTesting delete zone")
    deleted = zones.delete(zone_id)
    assert deleted is True
    assert zone_id not in zones

    print("\nTesting read after delete")
    zone = zones.read(zone_id)
    assert zone is None

    print("\nAll zone tests passed!")


if __name__ == "__main__":
    test_zone()
