# Practical 6 – Cloud Security: Initialize User Database
# Run first, from the folder containing all practical files:
#   python -m pip install boto3 cryptography
#   python p6_cloudSec_init_db.py
# This creates cloud.db; no AWS account is needed for this step.

# Create sample.txt and add some confidential text.
# Initialize the database:
# python init_db.py
# Generate the encryption key:
# python generate_keys.py
# Create simulated cloud storage:
# python s3_setup.py
# Encrypt and upload the file:
# python cloud_encrypt_upload.py
# Enter admin123.
# Download and decrypt the file:
# python cloud_download_decrypt.py
# Enter user123 or admin123.
# Open downloaded_sample.txt and verify that it contains the original text.

import sqlite3

conn = sqlite3.connect("cloud.db")
cur = conn.cursor()

cur.execute("""
CREATE TABLE IF NOT EXISTS users (
    username TEXT PRIMARY KEY,
    role TEXT NOT NULL,
    token TEXT UNIQUE NOT NULL
)
""")

users = [
    ("alice", "admin", "admin123"),
    ("bob", "user", "user123")
]

cur.executemany(
    "INSERT OR REPLACE INTO users VALUES (?, ?, ?)",
    users
)

conn.commit()
conn.close()

print("User database initialized.")

# What this code does: creates cloud.db and inserts the example admin and
# user accounts with their access roles and tokens.