================================================================================
  Simple Question SQ1: Minimal Supabase REST API CRUD Verification
================================================================================

FULL QUESTION:
SQ1. Minimal Cloud Database Test: Connect to Supabase REST API, insert a test sensor record via HTTP POST, and retrieve records via HTTP GET.
Use this simpler program to verify that your Supabase credentials, URL, and table schema are functioning correctly before running the full project pipelines.

SHORT QUESTION:
Minimal Supabase REST Insert & Fetch Verification Script.

================================================================================
README & SETUP INSTRUCTIONS:
================================================================================
# SQ1: Minimal Supabase REST API Verification

## Purpose
Run this quick script to test and verify your Supabase database connection in 2 seconds.

## Requirements
```bash
pip install requests
```

## How to Run
```bash
python test_supabase.py
```

================================================================================
SOURCE CODE FILES:
================================================================================

1. test_supabase.py
--------------------------------------------------------------------------------
import os
import time
import requests

SUPABASE_URL = "https://gtbhqmcbztuvgsbhyzdg.supabase.co/rest/v1"
SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imd0YmhxbWNienR1dmdzYmh5emRnIiwicm9sZSI6ImFub24iLCJpYXQiOjE3OTM1ODMzNDksImV4cCI6MjEwOTE1OTM0OX0.dNmQlR39JRWqMW03iDIwYtg4dvJzPopTc_a8LdRHgyQ"
TABLE_NAME = "sensor_telemetry"

headers = {
    "apikey": SUPABASE_KEY,
    "Authorization": f"Bearer {SUPABASE_KEY}",
    "Content-Type": "application/json",
    "Prefer": "return=representation"
}

def test_insert():
    endpoint = f"{SUPABASE_URL}/{TABLE_NAME}"
    payload = {
        "device_id": "TEST-NODE-01",
        "temperature": 28.5,
        "humidity": 65.0,
        "light": 450.0,
        "aqi": 75,
        "timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
    }
    print(f"Testing POST to: {endpoint}")
    resp = requests.post(endpoint, headers=headers, json=payload, timeout=5)
    print(f"POST Status Code: {resp.status_code}")
    print(f"POST Response: {resp.text}")
    return resp.status_code in [200, 201]

def test_fetch():
    endpoint = f"{SUPABASE_URL}/{TABLE_NAME}?select=*&order=id.desc&limit=5"
    print(f"\nTesting GET from: {endpoint}")
    resp = requests.get(endpoint, headers=headers, timeout=5)
    print(f"GET Status Code: {resp.status_code}")
    if resp.status_code == 200:
        data = resp.json()
        print(f"Successfully retrieved {len(data)} records:")
        for r in data:
            print(" ", r)
        return True
    else:
        print(f"GET Response: {resp.text}")
        return False

if __name__ == "__main__":
    print("=" * 60)
    print("  SQ1: Supabase Cloud Database Quick Test")
    print("=" * 60)
    ok_insert = test_insert()
    ok_fetch = test_fetch()
    if ok_insert and ok_fetch:
        print("\n[SUCCESS] Supabase REST API connection is fully working!")
    else:
        print("\n[NOTE] Check if table exists and RLS policy allows anon access.")
--------------------------------------------------------------------------------
