import sqlite3
import config

conn = sqlite3.connect(config.DB_FILE)
c = conn.cursor()

c.execute("""
             CREATE TABLE "exchange" (
             "id"	INTEGER,
             "name"	TEXT UNIQUE,
             "url"	TEXT,
             PRIMARY KEY("id")
             );
        """)
c.execute("""
          INSERT INTO exchange_name (name, url)

                VALUES
                ('binance_spot','https://www.binance.com/en/trade'),
                ('binance_futures','https://www.binance.com/en/futures'),
                ('binance_coin','https://www.binance.com/en/delivery'),
                ('alpaca','https://app.alpaca');
          """)

c.execute("""
             CREATE TABLE "markets" (
             "id"	INTEGER,
             "exchange_id"	INTEGER,
             "symbol"	TEXT NOT NULL,
             FOREIGN KEY("exchange_id") REFERENCES "exchange"("id"),
             PRIMARY KEY("id")
             );
        """)
c.execute("""
                CREATE TABLE "users" (
                "id"	INTEGER,
                "user_name"	TEXT NOT NULL,
                "status"	TEXT NOT NULL,
                "chart_views"	TEXT,
                "email"	TEXT,
                "active_exchanges"	TEXT,
                "configured_exchanges"	TEXT,
                "password"	TEXT NOT NULL,
                "api_keys"	TEXT,
                "signin_time"	INTEGER,
                PRIMARY KEY("id")
            );          
        """)
# Create indicator table
c.execute("""
            CREATE TABLE "indicators" (
            "id"	INTEGER,
            "name"	TEXT NOT NULL UNIQUE,
            "creator"	INTEGER,
            visible"	TEXT,
            "source"	TEXT,
            "properties"	TEXT,
            FOREIGN KEY("creator") REFERENCES "users"("id"),
            PRIMARY KEY("id" AUTOINCREMENT)
        );
""")
# Create the default user.
c.execute("""
                INSERT INTO "main"."users"("id", "user_name", "status",
                                           "chart_views", "email",
                                           "active_exchanges", "configured_exchanges", "password",
                                           "api_keys", "signin_time"
                )
                VALUES (NULL, 'guest', 'logged_in',
                        "{'exchange': 'alpaca', 'timeframe': '5m', 'market': 'BTC/USD'}", "user@email.com",
                        "['alpaca']", "['alpaca']","'$2b$13$lDstHtNBZEHB09t2XSMJG.PA4Nz0JYdV55QCbqIWrR8KRdqejTFxa'",
                        NULL, NULL
                );
         """)
# Create a default indicators.
c.execute("""
                INSERT INTO "main"."indicators"
                ("name", "creator", "visible", "kind", "source", "properties")
                VALUES (
                    "EMA 5", 1, "True", "EMA",
                    '{"symbol": "BTC/USD", "timeframe": "5m", "exchange_name": "alpaca"}',
                    '{"period": 5, "color": "#241571", "value": 0}'
                );
        """)
c.execute("""
                INSERT INTO "main"."indicators"
                ("name", "creator", "visible", "kind", "source", "properties")
                VALUES (
                    "vol", 1, "True", "Volume",
                    '{"symbol": "BTC/USD", "timeframe": "5m", "exchange_name": "alpaca"}',
                    '{"value": 0}'
                );
        """)
conn.commit()
conn.close()
