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)

users = assets.users_collection()

factory = assets.user_factory()
factory.nick = "test"
factory.password = "12345678"

users.add(factory.result)

app = assets.reservation_app(connection, users)

print("App initialized.")
print()

print("Loading all reservations:")
print(app.get_by_product({
    "apikey": factory.apikey,
    "target_barcode": "123456789012",
}))
print(app.get_by_user({
    "apikey": factory.apikey,
    "email": "abc@noreply.com"
}))
print()

print("Giving back item:")
print(app.give_back_product({
    "apikey": factory.apikey,
    "target_barcode": "123456789012",
    "email": "abc@noreply.com"
}))
print("After:")
print(app.get_by_product({
    "apikey": factory.apikey,
    "target_barcode": "123456789012",
}))
print()
print("Rent product:")
print(app.rent_product({
    "apikey": factory.apikey,
    "target_barcode": "123456789012",
    "email": "lkj@noreply.com"
}))
print(app.rent_product({
    "apikey": factory.apikey,
    "target_barcode": "123456789012",
    "email": "asd@noreply.com",
    "phone_number": "+48 123456789"
}))
print("After:")
print(app.get_by_product({
    "apikey": factory.apikey,
    "target_barcode": "123456789012",
}))
print()

drop_database()

