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("./003-product_app.db")

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

drop_database()

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

users = assets.users_collection()

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

users.add(factory.result)

app = assets.product_app(connection, users)

print("App initialized.")

create = app.create({
    "barcode": "123456789012",
    "name": "Sample",
    "description": "This is sample name.",
    "author": "John Snow",
    "image": "https://uuu.owo.pl",
    "stock_count": "10",
    "apikey": factory.apikey
})

print("Create:")
print(create)
print()

select = app.get_barcode("123456789012")

print("Select by barcode:")
print(select)
print()

select = app.get_name("Sample")

print("Select by name:")
print(select)
print()

create = app.create({
    "barcode": "210987654321",
    "name": "Second sample",
    "description": "This is sample item second.",
    "author": "other",
    "image": "https://test.pl",
    "stock_count": "20",
    "apikey": factory.apikey
})

print("Create second:")
print(create)
print()

alls = app.all()

print("Select all:")
print(alls)
print()

modify = app.update({
    "barcode": "210987654321",
    "target_barcode": "210987654321",
    "name": "Second sample",
    "description": "This is sample item second.",
    "author": "other",
    "image": "https://test.pl",
    "stock_count": "20",
    "apikey": factory.apikey
})

print("Modify:")
print(modify)
print()

drop_database()
