<?php
require_once __DIR__ . '/common.php';
// Think this is depreciated
header('Content-Type: application/json');
$dbconn = getDBConnection();

// Default endpoint: Get current point outages from newpower table
if (empty(array_diff_key($_GET, array('service' => '')))) {
    try {
        $query = "
            SELECT json_build_object(
                'type', 'FeatureCollection',
                'features', json_agg(
                    json_build_object(
                        'type', 'Feature',
                        'geometry', ST_AsGeoJSON(geom)::json,
                        'properties', json_build_object(
                            'time', start_time,
                            'county', county,
                            'state', state,
                            'outage', outagen,
                            'lastchange', last_change,
                            'cause', cause
                        )
                    )
                    ORDER BY start_time ASC
                )
            ) as geojson
            FROM newpower 
            WHERE cwa = $1 AND active = true
        ";
        
        $result = pg_query_params($dbconn, $query, array('RLX'));
        if ($result === false) {
            throw new Exception('Query failed: ' . pg_last_error());
        }

        $resultArray = pg_fetch_assoc($result);
        
        if ($resultArray && $resultArray['geojson']) {
            echo $resultArray['geojson'];
        } else {
            echo json_encode(['type' => 'FeatureCollection', 'features' => []]);
        }

        pg_free_result($result);
    } catch (Exception $e) {
        http_response_code(500);
        die(json_encode(['error' => 'Query execution failed: ' . $e->getMessage()]));
    }
}

// Get current county outages from newcountyoutages table
if (isset($_GET['county'])) {
    try {
        $query = "
            WITH latest_fetch AS (
                SELECT MAX(fetch_time) as max_fetch_time FROM newcountyoutages
            )
            SELECT 
                n.fetch_time as time,
                n.county,
                n.state,
                n.outages as outage,
                n.served
            FROM newcountyoutages n, latest_fetch
            WHERE n.fetch_time = latest_fetch.max_fetch_time
                AND n.cwa = $1
            ORDER BY n.county, n.state
        ";

        $result = pg_query_params($dbconn, $query, ['RLX']);
        if ($result === false) {
            throw new Exception('Query failed: ' . pg_last_error());
        }

        $results = pg_fetch_all($result) ?: [];
        echo json_encode($results);

        pg_free_result($result);
    } catch (Exception $e) {
        http_response_code(500);
        echo json_encode(['error' => 'Query execution failed: ' . $e->getMessage()]);
    }
}

// Note: The 'countyarchive', 'archivepoint', and 'svr' endpoints from the original
// searchapi.php can be migrated here. The queries would need to be updated to use 
// the 'newpower' and 'newcountyoutages' tables and their corresponding columns 
// (e.g., start_time, geom, fetch_time).

pg_close($dbconn);
?>


