
CREATE TABLE IF NOT EXISTS cards(
    id SERIAL PRIMARY KEY,
    stroke_number INTEGER unique,
    surface_position INTEGER ARRAY,
    surface_load INTEGER ARRAY,
    downhole_position INTEGER ARRAY,
    downhole_load INTEGER ARRAY,
    ts_added TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS measurement_configs (
    id SERIAL PRIMARY KEY,
    m_name TEXT UNIQUE NOT NULL,
    units TEXT,
    store_delta REAL NOT NULL,
    store_time REAL NOT NULL,
    use_total BOOLEAN NOT NULL,
    use_average BOOLEAN NOT NULL,
    ts_added TIMESTAMP DEFAULT NOW()

);

CREATE TABLE IF NOT EXISTS measurements_num (
    id SERIAL PRIMARY KEY,
    m_id INTEGER references measurement_configs(id),
    val REAL NOT NULL,
    ts_added TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS measurements_text (
    id SERIAL PRIMARY KEY,
    m_id INTEGER references measurement_configs(id),
    val TEXT NOT NULL,
    ts_added TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS daily_totals (
    id SERIAL PRIMARY KEY,
    m_id INTEGER references measurement_configs(id),
    current_value REAL NOT NULL,
    max_value REAL NOT NULL,
    min_value REAL NOT NULL,
    average_value REAL,
    total_value REAL,
    ts_added TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS well_tests (
    id SERIAL PRIMARY KEY,
    test_completed_timestamp TIMESTAMP NOT NULL,
    test_hours REAL NOT NULL,
    total_bbl REAL NOT NULL,
    oil_bbl REAL,
    h2o_bbl REAL,
    gas_mcf REAL,
    k_factor REAL,
    oil_bbl_ratio REAL,
    h2o_bbl_ratio REAL,
    gas_mcf_ratio REAL,
    ts_added TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS fluid_shots (
    id SERIAL PRIMARY KEY,
    shot_timestamp TIMESTAMP,
    pump_intake_pressure REAL NOT NULL,
    fluid_level REAL NOT NULL,
    friction_estimate REAL NOT NULL,
    ts_added TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS device_statuses (
    id SERIAL PRIMARY KEY,
    status TEXT NOT NULL,
    initiator TEXT,
    status_timestamp TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS well_configs (
    id SERIAL PRIMARY KEY,
    taper_setup JSON NOT NULL,
    well_config JSON NOT NULL,
    ts_added TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS setpoints (
    id SERIAL PRIMARY KEY,
    param TEXT UNIQUE NOT NULL,
    setpoint_value REAL NOT NULL,
    ts_added TIMESTAMP DEFAULT NOW()
);