<?php

class ReportApi extends Api
{
    public $apiName = 'report';
    const REPORTS = array(
        "weeklySpending" => 1,
        "spendingByCategoryAllTime" => 1,
        "categoryItems" => 1,
        "spendingByCategoryLastSevenDays" => 1,
        "spendingByWeekdayLastSevenDays" => 1,
        "travelTimeBreakdownVsAverage" => 1,
        "emissionsByDays" => 1,
        "travelTimeByDays" => 1,
    );

    public function indexAction()
    {
        return $this->response(
            array(
                'error' => 'Data not found'
            ), 404);
    }

    /**
     * Method GET
     * Get single record (by id)
     * http://DOMAIN/address/1
     * @return string
     */
    public function viewAction()
    {
        //id must be the first parameter after /address/x
        $id = array_shift($this->requestUri);

        if($id && (int)$id>0){
            /*$db = new Db();
            $address = Address::getAddressById($db->getConnect(), (int)$id);
            if(is_array($address) && count($address)>0){
                return $this->response($address, 200);
            }*/
        }
        return $this->response(
            array(
                'error'=> 'Data not found'
            ), 404);
    }

    public function createAction()
    {
        $message = "Data not found";
        $type = (string)$this->requestParams["type"] ?? "";
        $data = $this->requestParams["data"] ?? array();
        if ($type!='' && is_array($data) && self::REPORTS[$type]==1) {
            $db = new Db();
            list ($results, $options) = call_user_func("Report::${type}", 
                $db->getConnect(), $db->getConnectGPS(), $data);
            if($results!=NULL && is_array($results) && count($results)>0){
                return $this->response(
                    array(
                        'type' => $type,
                        'data' => $data,
                        'report' => $results,
                        'options' => $options
                    ), 200);
            } else {
                $message = "Report data is not found";
            }
        } else {
            $message = "Invalid input";
        }
        return $this->response(
            array(
                "error" => $message
            ), 404);
    }

    public function updateAction()
    {
        return $this->response(
            array(
                "error" => "Update error"
            ), 400);
    }

    public function deleteAction()
    {
        return $this->response(
            array(
                "error" => "Delete error"
            ), 500);
    }
}
