import os
os.chdir(os.path.dirname(__file__))
import sys
sys.path.append("..")
sys.path.append("..\\Sources")
from Sources.Modules.Inventory.inventory import * #altered
import json
import Sources.main as main_func
import Sources.Model.model_manager as model_manager
item_id_to_delete = 0

import pytest
@pytest.fixture(scope="session", autouse=True)
def execute_before_any_test():

    main_func.init()
    persistence.wipeout_database()
    persistence.init()


def test_create_inventory_item() :

    Item = inventory_item(name="RB2011", type="router", brand="Mikrotik",note="saline RT 2, chez ashvin",
                          assign_date_epoch = 5000, assigned = True, owned = True,serial_number = "12345678",
                          wear_score = 100, tags = "#router #mikrotik #saline")



    created_item = inventory_item_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
    print(json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder))
    assert json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder) == '{"assign_date_epoch": 5000, "assigned": true, "brand": "Mikrotik", "contact_id": null, "custom_fields_json": null, "group_id": null, "id": 1, "name": "RB2011", "note": "saline RT 2, chez ashvin", "owned": true, "registry": null, "serial_number": "12345678", "site_id": null, "tags": "#router #mikrotik #saline", "type": "router", "wear_score": 100}'

def test_bad_create_inventory_item() :
    Item = inventory_item(name="RB2011", type="router", brand="Mikrotik",note="saline RT 2, chez ashvin",site_id = 500)

    created_item = None
    with pytest.raises(BaseException) :
        created_item = inventory_item_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))

    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
    print(json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder))

def test_modify_inventory_item() :
    Item = inventory_item(name="RB2011", type="router", brand="Mikrotik",note="saline RT 1, chez jesai",
                          assign_date_epoch = 5000, assigned = True, owned = True,serial_number = "12345678",
                          wear_score = 100, tags = "#router #mikrotik #saline")


    created_item = inventory_item_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))


    Item.name = "RB3011"
    Item.type = "MEGAROUTER"
    Item.serial_number = "546785"
    Item.assigned = False

    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))

    modifiedItem = inventory_item_update(created_item.id, json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))

    #encoding then decoding to remove "not json" compliant variable
    attributes_dict = json.loads(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder),
                                                     cls=model_manager.JsonAlchemyDecoder)

    for key in  attributes_dict:
        print("modified item value : " + str(modifiedItem.__dict__[key]))
        if key == "id" : # the id should be not checked
            continue
        assert modifiedItem.__dict__[key] == attributes_dict[key]

def test_delete_inventory_item() :
    with pytest.raises(BaseException) :
        inventory_item_delete(5456)

    inventory_item_delete(1)
    with pytest.raises(BaseException) :
        get_inventory_item(1)

def test_create_inventory_site() :

    Item = inventory_site(name = "saline 1", address = "31 rue de la Hess", gps_coordinates = "1264,542 - 75645,451", note = "chez jesai")


    created_item = inventory_site_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
    print(json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder))
    attributes_dict = json.loads(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder),
                                                     cls=model_manager.JsonAlchemyDecoder)


    get_inventory_site(1)

    for key in  attributes_dict:
        if key == "id" : # the id should be not checked
            continue
        assert created_item.__dict__[key] == attributes_dict[key]

def test_bad_create_inventory_site() :
    Item = inventory_site(name = "saline 1", address = "31 rue de la Hess", gps_coordinates = "1264,542 - 75645,451", note = "chez jesai", contact_id = 452634)


    created_item = None
    with pytest.raises(BaseException) :
        created_item = inventory_site_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))

    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
    print(json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder))

def test_modify_inventory_site() :
    Item = inventory_site(name = "saline 1", address = "31 rue de la Hess", gps_coordinates = "1264,542 - 75645,451", note = "chez jesai")


    created_item = inventory_site_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))


    Item.address = "56 rue de la mega Hess"
    Item.note = "chez jesai et ses parents"
    Item.tags = "#jesai #debranchelantenne"

    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))

    modifiedItem = inventory_site_update(created_item.id, json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))

    #encoding then decoding to remove "not json" compliant variable
    attributes_dict = json.loads(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder),
                                                     cls=model_manager.JsonAlchemyDecoder)

    for key in  attributes_dict:
        print("modified item value : " + str(modifiedItem.__dict__[key]))
        if key == "id" : # the id should be not checked
            continue
        assert modifiedItem.__dict__[key] == attributes_dict[key]

def test_delete_inventory_site() :
    with pytest.raises(BaseException) :
        inventory_site_delete(5456)

    inventory_site_delete(1)
    with pytest.raises(BaseException) :
        get_inventory_site(1)


def test_create_inventory_contact() :

    Item = inventory_contact(contact_person = "Jesai Dourdaine", contact_mail = "antenne@gmail.com",
                             contact_number = "0262991200", note = "chez jesai",tags = "#pasfiable")


    created_item = inventory_contact_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))
    print(json.dumps(created_item, cls=model_manager.JsonAlchemyEncoder))
    attributes_dict = json.loads(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder),
                                                     cls=model_manager.JsonAlchemyDecoder)


    get_inventory_contact(1)

    for key in  attributes_dict:
        if key == "id" : # the id should be not checked
            continue
        assert created_item.__dict__[key] == attributes_dict[key]

def test_modify_inventory_contact() :
    Item = inventory_contact(contact_person = "Jesai Dour", contact_mail = "antenne@gmail.com",
                             contact_number = "0262991200", note = "chez jesai",tags = "#pasfiable")

    created_item = inventory_contact_create(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))


    Item.contact_person = "Jesai Dourdaine"
    Item.note = "un gars moyennement fiable"
    Item.tags = "#jesai #debranchelantenne #pasfiable"

    print(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))

    modifiedItem = inventory_contact_update(created_item.id, json.dumps(Item, cls=model_manager.JsonAlchemyEncoder))

    #encoding then decoding to remove "not json" compliant variable
    attributes_dict = json.loads(json.dumps(Item, cls=model_manager.JsonAlchemyEncoder),
                                                     cls=model_manager.JsonAlchemyDecoder)

    for key in  attributes_dict:
        print("modified item value : " + str(modifiedItem.__dict__[key]))
        if key == "id" : # the id should be not checked
            continue
        assert modifiedItem.__dict__[key] == attributes_dict[key]

def test_delete_inventory_contact() :
    with pytest.raises(BaseException) :
        inventory_contact_delete(5456)

    inventory_contact_delete(1)
    with pytest.raises(BaseException) :
        get_inventory_contact(1)



def test_create_inventory_group() :

    Item1 = inventory_item(name="RB2011", type="router", brand="Mikrotik",note="saline RT 2, chez ashvin",
                          assign_date_epoch = 5000, assigned = True, owned = True,serial_number = "12345678",
                          wear_score = 100, tags = "#router #mikrotik #saline")

    Item2 = inventory_item(name="RB4011-UAS", type="router", brand="Mikrotik",note="saline RT 2, chez ashvin",
                          assign_date_epoch = 5000, assigned = True, owned = True,serial_number = "12345678",
                          wear_score = 100, tags = "#router #mikrotik #saline")

    group1 = inventory_group(name = "Saline 2 routers", note = "tout les routeur chez ashvin")


    created_item1 = inventory_item_create(json.dumps(Item1, cls=model_manager.JsonAlchemyEncoder))
    created_item2 = inventory_item_create(json.dumps(Item2, cls=model_manager.JsonAlchemyEncoder))

    created_group1 = inventory_group_create([],json.dumps(group1, cls=model_manager.JsonAlchemyEncoder))

    inventory_group_add_item(created_group1.id,created_item1.id)
    inventory_group_add_item(created_group1.id,created_item2.id)
    global tmp_id1
    global tmp_id2
    global tmp_id_gr
    tmp_id_gr = created_group1.id
    tmp_id1 = created_item1.id
    tmp_id2 = created_item2.id

    print("INTERNAL GROUP ID : " + str(tmp_id_gr))

    for item in get_all_inventory_items() :
        if item.id == created_item1.id or item.id == created_item2.id :
            if not item.group_id == created_group1.id :
                raise BaseException("group id is not set correctly for this item")



def test_delete_item_from_group() :
    print("EXTERNAL GROUP ID : " + str(tmp_id_gr))
    group = get_inventory_group(tmp_id_gr)

    inventory_group_delete_item(group.id,tmp_id2) #deleting item 2

    for item in get_all_inventory_items() :
        if item.id == tmp_id2:
            if item.group_id == group.id :
                raise BaseException("item not deleted from group")


def test_add_item_from_group() :
    group = get_inventory_group(tmp_id_gr)

    inventory_group_add_item(group.id,tmp_id2) #deleting item 2

    for item in get_all_inventory_items() :
        if item.id == tmp_id2:
            if not item.group_id == group.id :
                raise BaseException("item not added to group")


def test_delete_group() :

    inventory_group_delete(tmp_id_gr)

    with pytest.raises(BaseException) :
        get_inventory_group(tmp_id_gr)

    for item in get_all_inventory_items() :
        if item.id == tmp_id2 or item.id == tmp_id1:
            if item.group_id == tmp_id_gr :
                raise BaseException("group not deleted")
