from random import randint

import pytest
from numpy import array
from shapely.geometry import MultiLineString

from aequilibrae.project.database_connection import database_connection
from aequilibrae.transit.functions.get_srid import get_srid
from aequilibrae.transit.transit_elements import Route
from tests.aeq.transit.random_word import randomword


@pytest.fixture
def data_dict():
    return {
        "route_id": randomword(randint(0, 40)),
        "route_short_name": randomword(randint(0, 40)),
        "route_long_name": randomword(randint(0, 40)),
        "route_desc": randomword(randint(0, 40)),
        "route_type": randint(0, 13),
        "route_url": randomword(randint(0, 40)),
        "route_color": randomword(randint(0, 40)),
        "route_text_color": randomword(randint(0, 40)),
        "route_sort_order": randint(0, 2000),
        "agency_id": randint(0, 1000),
    }


def test__populate(data_dict):
    data = data_dict

    r = Route(1)
    r.populate(tuple(data.values()), list(data.keys()))
    data["route"] = data.pop("route_id")
    for key, val in r.__dict__.items():
        if key in data:
            assert val == data[key], "Route population with record failed"

    data[randomword(randint(1, 15))] = randomword(randint(1, 20))
    new_r = Route(1)
    with pytest.raises(KeyError):
        new_r.populate(tuple(data.values()), list(data.keys()))


def test_save_to_database(data_dict, build_gtfs_project):
    data = data_dict

    r = Route(1)
    r.srid = get_srid()
    r.populate(tuple(data.values()), list(data.keys()))
    r.shape = MultiLineString([array(((0.0, 0.0), (1.0, 2.0)))])

    sql = "Select agency_id, shortname, longname, description, route_type from routes where route=?"
    with database_connection("transit") as transit_conn:
        r.save_to_database(transit_conn)
        result = list(transit_conn.execute(sql, [data["route_id"]]).fetchone())

    expected = [
        data["agency_id"],
        data["route_short_name"],
        data["route_long_name"],
        data["route_desc"],
        data["route_type"],
    ]
    assert result == expected, "Saving route to the database failed"
