import pathlib

current = pathlib.Path(__file__).parent
root = current.parent

import sys
sys.path.append(str(root))

import assets
import sqlmodel

def drop_database() -> None:
    db = pathlib.Path("./009-reservation.db")

    if db.is_file():
        db.unlink()

drop_database()

connection = sqlmodel.create_engine("sqlite:///009-reservation.db")
sqlmodel.SQLModel.metadata.create_all(connection)

builder = assets.product_factory()
builder.name = "Sample name"
builder.author = "Simple UwU Artist"
builder.description = "This is simple description"
builder.barcode = "123456789012"
builder.stock_count = 10

product = builder.result

print("Create first product:")
print(product)
print()

with assets.product_loader(connection) as loader:
    print("Inserting product...")
    loader.store(product)

    print("Inserted:")
    print(product)
    print()

with assets.product_loader(connection) as loader:
    print("Loading product to make reservation...")
    target = loader.get_by_barcode("123456789012")
    print("Loaded: ")
    print(target)

    before = target.on_stock

    reservation = assets \
    .reservation_factory() \
    .target(target) \
    .email("xyz@noreply.com") \
    .result()

    print(reservation)
    print()

with assets.reservation_loader(connection) as loader:
    print("Inserting it into database...")
    loader.store(reservation)
    print(reservation)

with assets.product_loader(connection) as loader:
    print("Loading product to make reservation...")
    target = loader.get_by_barcode("123456789012")
    print("Loaded: ")
    print(target)

    reservation = assets \
    .reservation_factory() \
    .target(target) \
    .email("abc@noreply.com") \
    .result()

    print(reservation)
    print()

with assets.reservation_loader(connection) as loader:
    print("Inserting it into database...")
    loader.store(reservation)
    print(reservation)

with assets.product_loader(connection) as loader:
    print("Loading product to check that on stock calc work...")
    target = loader.get_by_barcode("123456789012")
    after = target.on_stock
    
    print("Before: " + str(before))
    print("After: " + str(after))
    print()

    before = after

with assets.reservation_loader(connection) as loader:
    print("Loading reservation for product...")
    
    collection = loader \
    .get_by_target(target) \
    .by_email("abc@noreply.com") \
    .results()
    
    if len(collection) != 1:
        print("Fail, collection:")
        print(collection)
        raise Exception("Collection is not propertly filtered.")
    
    reservation = collection[0]

    print("Loaded.")
    print(reservation)
    print()
    
    print("Removing it from database...")
    loader.drop(reservation)
    print("Removed.")
    print()

with assets.product_loader(connection) as loader:
    print("Loading product to check that on stock calc work...")
    target = loader.get_by_barcode("123456789012")
    after = target.on_stock
    
    print("Before: " + str(before))
    print("After: " + str(after))
    print()

    print("Trying to drop product...")
    loader.drop(target)
    print("Removed.")

drop_database()

