<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/

require_once __DIR__ . '/../../config/boot_global.php';
require_once __DIR__ . '/../../config/boot_local.php';

// Dependency - only used by this script
include_once( dirname(__FILE__) . '/../../../../../../dependencies/mysqldump-php-master/src/Ifsnop/Mysqldump/Mysqldump.php');

class TB_Server_Cron_DailyWorker extends TB_Server_Core_CronBase {

    /**
     * Delete outdated
     * @throws Exception
     */
    protected function run()
    {
        //
        // Send logs
        //
        $this->sendLogs();

        //
        // Generate and send stats
        //
        $this->sendStats();
    }


    /**
     * @param int $numDays
     */
    protected function sendMailReminder( $numDays )
    {
        $dbh = TB_Shared_Db_TeamData::get();
        $sql = "CALL proc_getRecentlyStartedTeams($numDays)";
        $stmt = $dbh->query( $sql );
        $rows = $stmt->fetchAll( PDO::FETCH_ASSOC );
        $stmt->closeCursor();
        $stmt = null;
        TB_Shared_Db_TeamData::close();

        foreach( $rows as $row )
        {
            // Collect required data
            $accountId = isset( $row[ 'account_id' ] ) ? $row[ 'account_id' ] : NULL;
            $firstName = isset( $row[ 'first_name' ] ) ? $row[ 'first_name' ] : NULL;
            $lastName =  isset( $row[ 'last_name' ] ) ? $row[ 'last_name' ] : NULL;
            $teamName =  isset( $row[ 'display_name' ] ) ? $row[ 'display_name' ] : NULL;
            $numBuddies = isset( $row[ 'Teilnehmer' ] ) ? $row[ 'Teilnehmer' ] : NULL;
            $numAppointments = isset( $row[ 'Termine' ] ) ? $row[ 'Termine' ] : NULL;
            $numReactions = isset( $row[ 'ZuAbsagen' ] ) ? $row[ 'ZuAbsagen' ] : NULL;

            /** @var TB_Shared_Ent_Core_Account $entAccount */
            $entAccount = TB_Shared_Ent_Core_Account::get( $accountId );
            if ( !(  $entAccount instanceof  TB_Shared_Ent_Core_Account ) )
            {
                continue;
            }

            $data = array(
                'firstName' => $firstName,
                'lastName' => $lastName,
                'teamName' => $teamName,
                'numBuddies' => $numBuddies,
                'numAppointments' => $numAppointments,
                'numReactions' => $numReactions
            );

            switch( $numDays )
            {
                case 2:
                    TB_Server_Utils_Mail::sendReminder2( $entAccount, $data );
                break;

                case 4:
                    TB_Server_Utils_Mail::sendReminder4( $entAccount, $data );
                break;

                case 8:
                    TB_Server_Utils_Mail::sendReminder8( $entAccount, $data );
                break;
            }

        }
    }



    /**
     * Generate some stats and send them via email
     */
    protected function sendStats() {

    }

    /**
     * Send log files via email
     */
    protected function sendLogs() {
        // Get content of logs
        $errorLogger = TB_Server_Utils_Log::get();
        $cronLogger  = TB_Server_Utils_Log::getCron();

        // Send mail
        TB_Server_Utils_Mail::sendLogs(
            $errorLogger->getContent() . "\n\n" . $cronLogger->getContent()
        );

        // Truncate
        $errorLogger->clearLogfile();
        $cronLogger->clearLogfile();
    }

}

$cron = new TB_Server_Cron_DailyWorker();
$cron->execute();