<?php
require_once __DIR__ . '/common.php';

$dbconn = getDBConnection();

//no gets, current point outage info
if(empty(array_diff_key($_GET, array('service' => '')))) {
    $result = pg_query_params($dbconn,
    "SELECT json_build_object('type', 'FeatureCollection','features', json_agg(json_build_object('type','Feature', 'geometry',  ST_AsGeoJSON(realgeom)::json,'properties',json_build_object('time',startguess,'county',county,'state',state,'outage',outagen,'lastchange',lastchange,'cause',cause))order by startguess asc)) FROM power WHERE cwa = $1 and active = true",
    array('RLX')) or die('Query failed: ' . pg_last_error());
    $resultArray = pg_fetch_all($result);
    echo($resultArray[0]['json_build_object']);
}

//county current
if(isset($_GET['county'])) {
    $result = pg_query_params($dbconn,
    "SELECT distinct on (county,state) update as time, county, state, outages as outage,served FROM countyoutages where cwa = $1 order by county,state,update desc",
    array('RLX')) or die('Query failed: ' . pg_last_error());

    while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
        $array[] = $line;
    }
    echo json_encode($array);
}

//county archive
if(isset($_GET['countyarchive'])) {
    if(isset($_GET['start'])) {
        $starttime = getParam('start');
        if(isset($_GET['end'])) {
            $endtime = getParam('end');

            $result = pg_query_params($dbconn,
            "SELECT county,state, update as time, county, state, outages as outage,served FROM countyoutages where cwa = $1 and update > $2 and update < $3 order by update asc",
            array('RLX',$starttime,$endtime)) or die('Query failed: ' . pg_last_error());

            while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
                $array[] = $line;
            }
            echo json_encode($array);
        }
    }
}

//Archive point data
if(isset($_GET['archivepoint'])) {
    if(isset($_GET['start'])) {
        $starttime = getParam('start');
        if(isset($_GET['end'])) {
            $endtime = getParam('end');
            $result = pg_query_params($dbconn,
            "SELECT json_build_object('type', 'FeatureCollection','features', json_agg(json_build_object('type','Feature', 'geometry',  ST_AsGeoJSON(realgeom)::json,'properties',json_build_object('time',startguess,'county',county,'state',state,'outage',outagen,'lastchange',lastchange,'cause',cause))order by startguess asc)) FROM power WHERE cwa = $1 and startguess > $2 and lastchange < $3"
            ,array('RLX',$starttime,$endtime)) or die('Query failed: ' . pg_last_error());

            $resultArray = pg_fetch_all($result);
            echo($resultArray[0]['json_build_object']);
        }
    }
}

if(isset($_GET['svr']) && $_GET['svr'] === 'current') {
    $result = pg_query($dbconn,
    "SELECT json_build_object('type', 'FeatureCollection','features', json_agg(json_build_object('type','Feature', 'geometry',  ST_AsGeoJSON(nwspoly)::json,'properties',json_build_object('issue',issue,'end',endtime,'vtec',vtec,'type',warntype)))) FROM svr where issue < now() and endtime > now()") or die('Query failed: ' . pg_last_error());
    $resultArray = pg_fetch_all($result);
    echo($resultArray[0]['json_build_object']);
}

if(isset($_GET['svr']) && $_GET['svr'] === 'archive') {
    if(isset($_GET['start'])) {
        $starttime = getParam('start');
        if(isset($_GET['end'])) {
            $endtime = getParam('end');

            $result = pg_query_params($dbconn,
            "SELECT json_build_object('type', 'FeatureCollection','features', json_agg(json_build_object('type','Feature', 'geometry',  ST_AsGeoJSON(nwspoly)::json,'properties',json_build_object('issue',issue,'end',endtime,'vtec',vtec,'type',warntype)))) FROM svr where issue > $1 and endtime < $2"
            ,array($starttime,$endtime)) or die('Query failed: ' . pg_last_error());
            $resultArray = pg_fetch_all($result);
            echo($resultArray[0]['json_build_object']);
        }
    }

    if(!isset($_GET['start']) && !isset($_GET['end'])) {
        $result = pg_query($dbconn,
        "SELECT json_build_object('type', 'FeatureCollection','features', json_agg(json_build_object('type','Feature', 'geometry',  ST_AsGeoJSON(nwspoly)::json,'properties',json_build_object('issue',issue,'end',endtime,'vtec',vtec,'type',warntype)))) FROM svr where issue < now() - interval '24 hours' and endtime > now() - interval '24 hours'") or die('Query failed: ' . pg_last_error());
        $resultArray = pg_fetch_all($result);
        echo($resultArray[0]['json_build_object']);
    }
}

pg_free_result($result);

// Close database connection when needed
if (isset($dbconn)) {
    pg_close($dbconn);
}
?>