import sqlite3


# Function for Convert Binary Data
# to Human Readable Format
def convertToBinaryData(filename):

    # Convert binary format to images
    # or files data
    with open(filename, "rb") as file:
        blobData = file.read()
    return blobData


def insertBLOB(name: int, photo):
    try:

        # Using connect method for establishing
        # a connection
        sqliteConnection = sqlite3.connect("database.db")
        cursor = sqliteConnection.cursor()
        print("Connected to SQLite")

        # insert query
        sqlite_insert_blob_query = (
            """ INSERT INTO Images (note, likes, dislikes, albumID, image) VALUES (?, ?, ?, ?, ?)"""
        )

        # Converting human readable file into
        # binary data
        empPhoto = convertToBinaryData(photo)

        # Convert data into tuple format
        data_tuple = ("some note", 0, 0, name, empPhoto)

        # using cursor object executing our query
        cursor.execute(sqlite_insert_blob_query, data_tuple)
        sqliteConnection.commit()
        print("Image and file inserted successfully as a BLOB into a table")
        cursor.close()

    except sqlite3.Error as error:
        print("Failed to insert blob data into sqlite table", error)

    finally:
        if sqliteConnection:
            sqliteConnection.close()
            print("the sqlite connection is closed")


insertBLOB(
    1, r"/home/ethanscharlie/Documents/Projects/Code/2026/Web/PhotoDumpGallery/Database/images/Ethan/DemoOne/board.JPG"
)
insertBLOB(
    1, r"/home/ethanscharlie/Documents/Projects/Code/2026/Web/PhotoDumpGallery/Database/images/Ethan/DemoOne/wheels.JPG"
)

insertBLOB(
    2, r"/home/ethanscharlie/Documents/Projects/Code/2026/Web/PhotoDumpGallery/Database/images/Ethan/DemoTwo/comp.JPG"
)
insertBLOB(
    2, r"/home/ethanscharlie/Documents/Projects/Code/2026/Web/PhotoDumpGallery/Database/images/Ethan/DemoTwo/dog.JPG"
)

insertBLOB(
    3, r"/home/ethanscharlie/Documents/Projects/Code/2026/Web/PhotoDumpGallery/Database/images/Fish/FishDemo/ban.JPG"
)
insertBLOB(
    3, r"/home/ethanscharlie/Documents/Projects/Code/2026/Web/PhotoDumpGallery/Database/images/Fish/FishDemo/fan.JPG"
)
