<?php

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */


// Detect if loaded by the user or by PHP
if (count(get_included_files()) == 1) {
    define("LOADED", true);
} else {
    define("LOADED", false);
}

require_once __DIR__ . "/../required.php";

dieifnotloggedin();

if (LOADED) {
    if (isset($VARS['type']) && isset($VARS['format'])) {
        generateReport($VARS['type'], $VARS['format']);
        die();
    } else {
        $Strings->get("invalid parameters");
        die();
    }
}

/**
 * Get a 2d array of the families in the database.
 * @global type $database
 * @param array $filter Medoo WHERE clause.
 * @return string
 */
function getPeopleReport($filter = ""): Report {
    global $database, $Strings;

    if (empty($filter)) {
        $report = new Report($Strings->get("People", false));
        $filter = ["ORDER" => ["familyname" => "ASC"]];
    } else {
        $report = new Report($Strings->get("$filter", false));
    }

    $join = [];
    $where = [];
    $header = [];
    switch ($filter) {
        case "Campers":
            $join = ["[>]campers" => ["camperid" => "camperid"]];
            $where = ["people.camperid[!]" => null];
            $header = [
                $Strings->get("First", false),
                $Strings->get("Last", false),
                $Strings->get("Parent", false),
                $Strings->get("Unit", false),
                $Strings->get("Rank", false),
                $Strings->get("Phone", false),
                $Strings->get("Email", false),
                $Strings->get("Address", false),
                $Strings->get("ZIP", false),
                $Strings->get("Shirt", false),
                $Strings->get("Sex", false),
                $Strings->get("Den", false),
                $Strings->get("Health", false),
                $Strings->get("Notes", false)
            ];
            break;
        case "Adults":
            $join = ["[>]adults" => ["adultid" => "adultid"]];
            $where = ["people.adultid[!]" => null];
            $header = [
                $Strings->get("First", false),
                $Strings->get("Last", false),
                $Strings->get("Phone", false),
                $Strings->get("Email", false),
                $Strings->get("Address", false),
                $Strings->get("ZIP", false),
                $Strings->get("Days", false),
                $Strings->get("Position", false),
                $Strings->get("Shirt", false),
                $Strings->get("Sex", false),
                $Strings->get("Notes", false)
            ];
            break;
        case "Youth":
            $join = ["[>]youth" => ["youthid" => "youthid"]];
            $where = ["people.youthid[!]" => null];
            $header = [
                $Strings->get("First", false),
                $Strings->get("Last", false),
                $Strings->get("Parent", false),
                $Strings->get("Phone", false),
                $Strings->get("Parent Phone", false),
                $Strings->get("Email", false),
                $Strings->get("Address", false),
                $Strings->get("ZIP", false),
                $Strings->get("Days", false),
                $Strings->get("Position", false),
                $Strings->get("Shirt", false),
                $Strings->get("Sex", false),
                $Strings->get("Notes", false)
            ];
            break;
        default:
            $header = [
                $Strings->get("First", false),
                $Strings->get("Last", false),
                $Strings->get("Type", false),
                $Strings->get("Phone", false),
                $Strings->get("Email", false),
                $Strings->get("Address", false),
                $Strings->get("ZIP", false),
                $Strings->get("Shirt", false),
                $Strings->get("Sex", false),
                $Strings->get("Notes", false)
            ];
    }

    if (empty($join)) {
        $people = $database->select("people", '*', $where);
    } else {
        $people = $database->select("people", $join, '*', $where);
    }

    $report->setHeader($header);

    foreach ($people as $p) {
        $row = [];

        $type = "Unknown";
        if (!empty($p['camperid'])) {
            $type = $Strings->get("Camper", false);
        } else if (!empty($p['adultid'])) {
            $type = $Strings->get("Adult", false);
        } else if (!empty($p['youthid'])) {
            $type = $Strings->get("Youth", false);
        }

        switch ($filter) {
            case "Campers":
                $row = [
                    $p['firstname'],
                    $p['lastname'],
                    $p['parentname'],
                    $p['unit'],
                    $p['rank'],
                    $p['phone1'],
                    $p['email'],
                    $p['address'],
                    $p['zip'],
                    $p['shirt'],
                    $p['sex'],
                    $p['den'],
                    $p['health'],
                    $p['notes']
                ];
                break;
            case "Adults":
                $row = [
                    $p['firstname'],
                    $p['lastname'],
                    $p['phone1'],
                    $p['email'],
                    $p['address'],
                    $p['zip'],
                    $p['days'],
                    $p['position'],
                    $p['shirt'],
                    $p['sex'],
                    $p['notes']
                ];
                break;
            case "Youth":
                $row = [
                    $p['firstname'],
                    $p['lastname'],
                    $p['parentname'],
                    $p['phone1'],
                    $p['phone2'],
                    $p['email'],
                    $p['address'],
                    $p['zip'],
                    $p['days'],
                    $p['position'],
                    $p['shirt'],
                    $p['sex'],
                    $p['notes']
                ];
                break;
            default:
                $row = [
                    $p['firstname'],
                    $p['lastname'],
                    $type,
                    $p['phone1'] . (empty($p['phone2']) ? "" : "  " . $p['phone2']),
                    $p['email'],
                    $p['address'],
                    $p['zip'],
                    $p['shirt'],
                    $p['sex'],
                    $p['notes']
                ];
        }

        $report->addDataRow($row);
    }
    return $report;
}

function getReport($type): Report {
    switch ($type) {
        case "campers":
            return getPeopleReport("Campers");
            break;
        case "adults":
            return getPeopleReport("Adults");
            break;
        case "youth":
            return getPeopleReport("Youth");
            break;
        case "people":
            return getPeopleReport("");
            break;
        default:
            return new Report("error", ["ERROR"], ["Invalid report type."]);
    }
}

function generateReport($type, $format) {
    $report = getReport($type);
    $report->output($format);
}
