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

/**
 * Class TB_Shared_Ent_TeamData_Appointment
 *
 * @property integer $id
 * @property integer $team_id
 * @property string|null $subject
 * @property string|null $home
 * @property string|null $guest
 * @property string|null $id_nr
 * @property integer|null $home_score
 * @property integer|null $guest_score
 * @property integer $starttime
 * @property integer $feedback_deadline
 * @property string|null $feedback_deadline_notice
 * @property integer $serial_id
 * @property integer|null $duration
 * @property string|null $location
 * @property string|null $comment
 */
class TB_Shared_Ent_TeamData_Appointment extends Francis_Db_Row {

    /**
     * Return appointment of given teamId
     *
     * @param integer $teamId
     * @return array TB_Shared_Ent_TeamData_Appointment[]
     */
    public static function getByTeamId( $teamId ) {
        $sql = 'SELECT * FROM ' . self::getTable() . ' WHERE team_id = :team_id ORDER BY `starttime` ASC';
        return self::findMany( $sql, array( ':team_id' => $teamId ) );
    }

    public static function getCurrentByTeamId( $teamId ) {
        $sql = 'SELECT * FROM ' . self::getTable() . ' WHERE team_id = :team_id ';
        $sql .= 'AND FROM_UNIXTIME( `starttime` ) > ( date_sub( now(),INTERVAL 30 DAY ) ) ';
        $sql .= 'ORDER BY `starttime` ASC';
        return self::findMany( $sql, array( ':team_id' => $teamId ) );
    }

    public static function getAcceptedByProfile( $teamId, $profileId ) {
        $sql  = 'SELECT * FROM ' . self::getTable() . ' t1 INNER JOIN attendee a1 ON (t1.id = a1.appointment_id) ';
        $sql .= 'INNER JOIN profile p1 ON (a1.profile_id = p1.id) WHERE t1.team_id = :team_id AND ';
        $sql .= 'FROM_UNIXTIME( t1.starttime ) > ( date_sub( now(),INTERVAL 30 DAY ) ) AND ';
        $sql .= 'a1.status = "accepted" AND a1.profile_id = :profile_id';

        return self::findMany( $sql, array( ':team_id' => $teamId, ':profile_id' => $profileId ) );
    }

    /**
     * Delete all appointments by given team id
     * @param int $teamId
     * @return bool
     */
    public static function deleteByTeamId( $teamId ) {
        $sql = 'DELETE FROM ' . self::getTable() . ' WHERE team_id  = :team_id';
        return self::execute( $sql, array( ':team_id' => $teamId) );
    }

    /**
     * Return table name
     *
     * @return null|string
     */
    public static function getTable() {
        return 'appointment';
    }

    /**
     * Return db handler
     *
     * @return Francis_Db_MySql|null
     */
    public static function getDbh() {
        return TB_Shared_Db_TeamData::get();
    }

    /**
     * @return null|string
     */
    public function getName() {
        $subject = $this->subject;
        if ( isset( $subject ) )  {
            return $subject;
        } else {
            return $this->home . " : " . $this->guest;
        }
    }

    public function isOver() {
        return ( $this->starttime < time() );
    }

    public function isDealineOver()
    {
        return ( ( $this->starttime - ( $this->feedback_deadline * 60 ) ) < time() );
    }

    /**
     * Called during deletion
     */
    public function deleteReferences() {

        // Delete all attendees
        $sql  = "DELETE FROM " . TB_Shared_Ent_TeamData_Attendee::getTable() . " ";
        $sql .=  "WHERE appointment_id = :appointment_id";
        $params = array( ':appointment_id' => $this->id );

        try {
            // Delete
            TB_Shared_Ent_TeamData_Attendee::execute( $sql, $params );
        } catch( Exception $e ) {
            TB_Server_Utils_Log::get()->log( $e );
        }
   }
}