import psycopg2
import uuid
import json

conn = psycopg2.connect(
    host="postgresql-genesis-u50607.vm.elestio.app",
    port=25432,
    user="postgres",
    password="etY0eog17tD-dDuj--IRH",
    dbname="postgres",
    connect_timeout=15
)
conn.autocommit = True
cur = conn.cursor()

cur.execute("SELECT schema_name FROM information_schema.schemata WHERE schema_name=%s", ("sunaiva",))
schema_exists = cur.fetchone() is not None
print("sunaiva schema exists:", schema_exists)

if not schema_exists:
    print("Creating sunaiva schema...")
    cur.execute("CREATE SCHEMA IF NOT EXISTS sunaiva")
    cur.execute("""CREATE TABLE IF NOT EXISTS sunaiva.users (
        id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
        email TEXT UNIQUE NOT NULL,
        password_hash TEXT NOT NULL DEFAULT chr(39) + chr(39),
        name TEXT NOT NULL DEFAULT chr(39) + "User" + chr(39),
        plan_tier TEXT NOT NULL DEFAULT chr(39) + "free" + chr(39),
        created_at TIMESTAMPTZ DEFAULT NOW(),
        updated_at TIMESTAMPTZ DEFAULT NOW()
    )""")