<?php

define('APP_ROOT_DIR', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
require_once (defined('COMPOSER_ROOT_DIR') ? COMPOSER_ROOT_DIR : APP_ROOT_DIR) . '/vendor/autoload.php';
require_once APP_ROOT_DIR . '/core/php/autoload.php';
require_once APP_ROOT_DIR . '/core/php/autoloadCLI.php';
/**
 *
 * @package com.meetyournextmp
 * @license Closed Source
 * @copyright (c) 2013-2015, JMB Technology Limited, http://jmbtechnology.co.uk/
 * @author James Baster <*****@*****.**>
 */
$stat = $DB->prepare("SELECT area_information.*, " . "area_mapit_information.mapit_id, area_mapit_information.code_unit_id, area_mapit_information.code_gss, area_mapit_information.name " . " FROM area_information JOIN area_mapit_information ON area_mapit_information.area_id = area_information.id");
$stat->execute(array());
$fp = fopen(__DIR__ . "/../../../webSingleSite/datadump/seats.csv", 'w');
if ($fp) {
    fputcsv($fp, array('name', 'mapit_id', 'code_unit_id', 'code_gss', "URL"));
    while ($data = $stat->fetch()) {
        $area = new \models\AreaModel();
        $area->setFromDataBaseRow($data);
        fputcsv($fp, array($data['name'], $data['mapit_id'], $data['code_unit_id'], $data['code_gss'], "http://" . $CONFIG->webSiteDomain . "/area/" . $area->getSlugForUrl()));
    }
    fclose($fp);
} else {
    die("OH NO");
}
    die("DIE\n");
}
// setup
$siteRepo = new \repositories\SiteRepository();
$site = $siteRepo->loadById($CONFIG->singleSiteID);
configureAppForSite($site);
$innerSQL = "SELECT human_in_area.area_id, COUNT(human_tweet.id) As tweets FROM human_in_area " . " LEFT JOIN human_tweet ON human_tweet.human_id = human_in_area.human_id AND human_in_area.removed_at IS NULL  " . "GROUP BY human_in_area.area_id";
// Get area
$stat = $DB->prepare("SELECT area_information.* FROM area_information " . " JOIN area_mapit_information ON area_mapit_information.area_id = area_information.id " . " JOIN (" . $innerSQL . ") AS area_tweets ON area_tweets.area_id = area_information.id " . " WHERE area_tweets.tweets = 0 AND  area_information.is_deleted = '0' AND area_information.cached_future_events >  " . EVENTS_NEEDED_IN_AREA . " ORDER BY area_information.cached_future_events DESC LIMIT 1 ");
$stat->execute();
if ($stat->rowCount() == 0) {
    die("No AREA Data");
}
$area = new \models\AreaModel();
$area->setFromDataBaseRow($stat->fetch());
$areaURL = "http://" . $CONFIG->webSiteDomain . "/area/" . $area->getSlugForUrl();
print "Found Area " . $area->getTitle() . "\n";
$erb = new \com\meetyournextmp\repositories\builders\EventRepositoryBuilder();
$erb->setAfterNow();
$erb->setArea($area);
$erb->setIncludeCancelled(false);
$erb->setIncludeDeleted(false);
$events = $erb->fetchCount();
if ($events < EVENTS_NEEDED_IN_AREA) {
    die("We checked and found less than " . EVENTS_NEEDED_IN_AREA . " events; is cache broken!");
}
$hrb = new \com\meetyournextmp\repositories\builders\HumanRepositoryBuilder();
$hrb->setIncludeDeleted(false);
$hrb->setArea($area);
$humans = $hrb->fetchAll();
$humanTweets = array();