DROP TABLE IF EXISTS telemetry CASCADE;
DROP TABLE IF EXISTS system_status CASCADE;
DROP TABLE IF EXISTS settings CASCADE;
DROP TABLE IF EXISTS users CASCADE;

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL,
    is_admin BOOLEAN DEFAULT FALSE
);

CREATE TABLE settings (
    id SERIAL PRIMARY KEY,
    key VARCHAR(50) UNIQUE NOT NULL,
    value VARCHAR(100) NOT NULL
);

CREATE TABLE telemetry (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    temperature_ext DOUBLE PRECISION,
    humidity_ext DOUBLE PRECISION,
    system_status_msg VARCHAR(100),

    voltage_pv DOUBLE PRECISION,
    current_pv DOUBLE PRECISION,
    power_pv DOUBLE PRECISION,
    luminosity DOUBLE PRECISION,

    voltage_battery DOUBLE PRECISION,
    current_battery DOUBLE PRECISION,
    battery_temp DOUBLE PRECISION,
    battery_level DOUBLE PRECISION,
    battery_alert VARCHAR(100)
);

INSERT INTO users (username, password, is_admin)
VALUES ('admin', 'admin123', TRUE);

INSERT INTO settings (key, value)
VALUES
    ('min_battery_voltage', '11'),
    ('max_battery_temperature', '60'),
    ('min_solar_power', '5');
