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("./002-product.db")

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

drop_database()

connection = sqlmodel.create_engine("sqlite:///002-product.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"

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("Get by name:")
    print(loader.get_by_name("Sample name"))
    print()

    print("Get by barcode:")
    print(loader.get_by_barcode("123456789012"))
    print()

with assets.product_loader(connection) as loader:
    print("Updating:")
    
    builder = assets.product_factory(loader.get_by_barcode("123456789012"))
    builder.stock_count = 200
    product = builder.result
    loader.store(product)

    print("After update:")
    print(loader.get_by_barcode("123456789012"))
    print()

with assets.product_loader(connection) as loader:
    print("Get from stock:")
    
    builder = assets.product_factory(loader.get_by_barcode("123456789012"))
    builder.get_from_stock()

    product = builder.result
    loader.store(product)

    print(loader.get_by_barcode("123456789012"))
    print()

with assets.product_loader(connection) as loader:
    print("Delete from database:")

    loader.drop(product)
    
    print(loader.get_by_barcode("123456789012"))
    print()

drop_database()
