-- WellNuo Database Schema for Supabase
-- Generated from eluxnetworks.net PostgreSQL dump
-- Date: 2025-12-17

-- =============================================
-- TABLE: deployments (patients/deployments)
-- =============================================
CREATE TABLE IF NOT EXISTS deployments (
    deployment_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    time_edit REAL,
    user_edit INTEGER,
    time_zone_s TEXT,
    persons INTEGER,
    gender INTEGER,
    race INTEGER,
    born INTEGER,
    pets INTEGER,
    context TEXT,
    alarm_details TEXT
);

-- =============================================
-- TABLE: devices (sensors/devices)
-- =============================================
CREATE TABLE IF NOT EXISTS devices (
    device_id INTEGER PRIMARY KEY,
    device_mac TEXT NOT NULL,
    well_id INTEGER,
    description TEXT,
    location INTEGER,
    close_to TEXT,
    radar_threshold TEXT,
    fw_version TEXT,
    hw_version TEXT,
    ble_scan_period INTEGER,
    ble_scan_duration INTEGER,
    temperature_calib TEXT,
    humidity_calib TEXT,
    reporting_period_s INTEGER,
    reboot_time REAL,
    led_schema TEXT,
    alert_details TEXT,
    other TEXT,
    group_id INTEGER,
    UNIQUE(well_id, device_mac)
);

-- =============================================
-- TABLE: person_details (users)
-- =============================================
CREATE TABLE IF NOT EXISTS person_details (
    user_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    role_ids TEXT,
    access_to_deployments TEXT,
    email TEXT,
    user_name TEXT,
    first_name TEXT,
    last_name TEXT,
    address_street TEXT,
    address_city TEXT,
    address_zip TEXT,
    address_state TEXT,
    address_country TEXT,
    time_edit REAL,
    user_edit INTEGER,
    phone_number TEXT,
    picture TEXT,
    key TEXT  -- password field
);

-- =============================================
-- TABLE: deployment_details
-- =============================================
CREATE TABLE IF NOT EXISTS deployment_details (
    deployment_id INTEGER PRIMARY KEY,
    beneficiary_id INTEGER,
    caretaker_id INTEGER,
    owner_id INTEGER,
    installer_id INTEGER,
    devices TEXT,  -- JSON array of device MACs
    address_street TEXT,
    address_city TEXT,
    address_zip TEXT,
    address_state TEXT,
    address_country TEXT,
    wifis TEXT,
    lat REAL,
    lng REAL,
    gps_age INTEGER,
    note TEXT,
    floor_plan TEXT,
    overlapps TEXT
);

-- =============================================
-- TABLE: deployment_history
-- =============================================
CREATE TABLE IF NOT EXISTS deployment_history (
    id INTEGER PRIMARY KEY,
    time REAL,
    proximity TEXT
);

-- =============================================
-- TABLE: disclaimers
-- =============================================
CREATE TABLE IF NOT EXISTS disclaimers (
    id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    first_name TEXT,
    last_name TEXT,
    user_name TEXT,
    email TEXT,
    devices TEXT,
    date TIMESTAMPTZ,
    policy_version TEXT
);

-- =============================================
-- INDEXES (for performance)
-- =============================================
CREATE INDEX IF NOT EXISTS idx_devices_well_id ON devices(well_id);
CREATE INDEX IF NOT EXISTS idx_person_details_email ON person_details(email);
CREATE INDEX IF NOT EXISTS idx_person_details_user_name ON person_details(user_name);
CREATE INDEX IF NOT EXISTS idx_deployment_details_beneficiary ON deployment_details(beneficiary_id);

-- =============================================
-- Enable Row Level Security (RLS) for Supabase
-- =============================================
ALTER TABLE deployments ENABLE ROW LEVEL SECURITY;
ALTER TABLE devices ENABLE ROW LEVEL SECURITY;
ALTER TABLE person_details ENABLE ROW LEVEL SECURITY;
ALTER TABLE deployment_details ENABLE ROW LEVEL SECURITY;
ALTER TABLE deployment_history ENABLE ROW LEVEL SECURITY;
ALTER TABLE disclaimers ENABLE ROW LEVEL SECURITY;

-- Note: You'll need to create RLS policies based on your auth requirements
-- Example:
-- CREATE POLICY "Users can view their own data" ON person_details
--     FOR SELECT USING (auth.uid()::text = user_id::text);
