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


class TB_Server_Job_AppointmentReminder extends TB_Server_Job_Base
{
    public function __construct( TB_Shared_Ent_TeamData_Job $jobEntity )
    {
        $this->entity = $jobEntity;
    }

    public function execute()
    {
        $appointment = NULL;
        $team = NULL;
        $appointmentId = $this->entity->job_data_js[ 'appointmentId' ];

        if ( $appointmentId )
        {
            $appointment = TB_Shared_Ent_TeamData_Appointment::get( $appointmentId );
        }

        if ( is_null( $appointment ) || $appointment->isCancelled() )
        {
            // Do nothing
            // Note
            // We leave this notice here and do not delete job reminders to be more flexible when it comes to reactivate an appointment
            return;
        }

        $team = TB_Shared_Ent_TeamData_Team::get( $appointment->team_id );

        if ( is_null( $team ) )
        {
            TB_Server_Utils_Log::get()->log( 'TB_Server_Job_AppointmentReminder could not resolve team.' );
            return;
        }

        $attendees = TB_Shared_Ent_TeamData_Attendee::getAttendeesWithStatusForAppointmentId(
            $appointmentId,
            TB_Shared_Ent_TeamData_Attendee::STATUS_ACCEPTED );

        $profileIds = array();

        /** @var TB_Shared_Ent_TeamData_Attendee $attendee */
        foreach( $attendees as $attendee )
        {
            array_push( $profileIds, $attendee->profile_id );
        }

        TB_Server_Core_Notification::notifyAppointmentStart( $profileIds, $appointment, $team );
    }

    public static function getRefId( $appointmentId )
    {
        return 'appointment-reminder-' . $appointmentId;
    }
}