from flask_bcrypt import Bcrypt
import os
import sqlite3

if os.path.isfile("sms.db"):
    os.unlink("sms.db")

flask_bcrypt = Bcrypt()

con = sqlite3.connect("sms.db")
cur = con.cursor()

# ID columns are not necessary - SQLite by default sets ROWID as INTEGER PRIMARY KEY which auto increments
cur.execute("CREATE TABLE devices(access_key, secret_key_hash, type, name)")

# type                : INCOMING, OUTGOING
# local_phone_number  : a SECONDARY device SIM's phone number
# remote_phone_number : a phone number or shortcode of the other party
# TODO add column for sender's access key
cur.execute("CREATE TABLE messages(content, ts_received, ts_sent, type, local_phone_number, remote_phone_number)")
cur.execute("CREATE TABLE sim_events(sim_id, ts, note, cost, currency)")
cur.execute("CREATE TABLE sim_cards(phone_number, device_access_key)")
cur.execute("CREATE TABLE message_queue(content, sender_access_key, local_phone_number, remote_phone_number)")

pw1_hash = flask_bcrypt.generate_password_hash('pw1').decode('utf-8')
pw2_hash = flask_bcrypt.generate_password_hash('pw2').decode('utf-8')
cur.execute("""
    INSERT INTO devices VALUES
    ('test_access_key', ?, 'PRIMARY', 'test-primary'),
    ('test_access_key2', ?, 'SECONDARY', 'test-secondary')
""", (pw1_hash, pw2_hash))

cur.execute("""
    INSERT INTO sim_cards VALUES
    ('+420123456789', 'test_access_key'),
    ('+421000111222', 'test_access_key'),
    ('+422999888777', 'test_access_key2')
""")

cur.execute("""
    INSERT INTO messages VALUES
    ('how are you?', 1000, 999, 'INCOMING', '+420123456789', '+10005558888'),
    ('i am fine', 2000, 1999, 'OUTGOING', '+420123456789', '+10005558888')
""")

con.commit()
con.close()

