<?php

ini_set('display_errors',1);
error_reporting(E_ALL);

include($_SERVER['DOCUMENT_ROOT']."/SEAT/config/db.php");

require_once $_SERVER['DOCUMENT_ROOT'].'/SEAT/vendor/PHPMailer/src/Exception.php';

require_once $_SERVER['DOCUMENT_ROOT'].'/SEAT/vendor/PHPMailer/src/PHPMailer.php';

require_once $_SERVER['DOCUMENT_ROOT'].'/SEAT/vendor/PHPMailer/src/SMTP.php';

require_once $_SERVER['DOCUMENT_ROOT'].'/SEAT/config/mail.php';


/* =========================================
   GET TODAY EVENTS (TEST)
========================================= */

$sql = "
SELECT 
    er.email,
    er.full_name,

    e.title,
    e.event_date,
    e.start_time,
    e.venue,
    e.mode,
    e.meeting_link

FROM event_registrations er

JOIN events e
ON er.event_id = e.id

WHERE DATE(e.event_date)=DATE(NOW())

AND er.status='registered'
";


$result = $conn->query($sql);

/* =========================================
   SEND EMAIL
========================================= */

while($row = $result->fetch_assoc()){

    try{

        $mail = setupMail();

        $mail->addAddress(
            $row['email'],
            $row['full_name']
        );

        $mail->Subject =
        "Reminder : ".$row['title']." is Tomorrow";

        $venue = $row['venue'];

        if(
            strtolower($row['mode'])==="online"
        ){

            $venue = "Online Event";
        }

        $meetingHTML = "";

        if(
            strtolower($row['mode'])==="online"
            &&
            !empty($row['meeting_link'])
        ){

            $meetingHTML = "

            <div style='margin-top:25px;text-align:center;'>

                <a 
                href='{$row['meeting_link']}'
                target='_blank'
                style='
                    background:#2563eb;
                    color:white;
                    padding:14px 24px;
                    border-radius:12px;
                    text-decoration:none;
                    font-weight:700;
                    display:inline-block;
                '>

                    Join Meeting

                </a>

            </div>
            ";
        }

        $mail->isHTML(true);

        $mail->Body = "

        <div style='
            background:#f8fafc;
            padding:40px 20px;
            font-family:Segoe UI,sans-serif;
        '>

            <div style='
                max-width:680px;
                margin:auto;
                background:white;
                border-radius:24px;
                overflow:hidden;
                border:1px solid #e2e8f0;
                box-shadow:0 10px 30px rgba(15,23,42,0.08);
            '>

                <div style='
                    background:#2563eb;
                    padding:35px;
                    color:white;
                '>

                    <div style='
                        font-size:30px;
                        font-weight:700;
                    '>

                        Event Reminder

                    </div>

                    <div style='
                        margin-top:10px;
                        font-size:15px;
                        opacity:0.92;
                    '>

                        Your event is happening tomorrow.

                    </div>

                </div>

                <div style='padding:35px;'>

                    <div style='
                        font-size:15px;
                        color:#334155;
                        line-height:1.9;
                        margin-bottom:28px;
                    '>

                        Hello
                        <b>{$row['full_name']}</b>,

                        <br><br>

                        This is a reminder regarding your upcoming registered event.

                    </div>

                    <div style='
                        background:#f8fafc;
                        border:1px solid #e2e8f0;
                        border-radius:18px;
                        padding:24px;
                    '>

                        <table
                        width='100%'
                        style='
                            border-collapse:collapse;
                            font-size:14px;
                            color:#334155;
                        '>

                            <tr>

                                <td style='
                                    padding:14px 0;
                                    width:180px;
                                    color:#64748b;
                                    border-bottom:1px solid #e2e8f0;
                                '>

                                    Event Title

                                </td>

                                <td style='
                                    padding:14px 0;
                                    font-weight:600;
                                    color:#0f172a;
                                    border-bottom:1px solid #e2e8f0;
                                '>

                                    {$row['title']}

                                </td>

                            </tr>

                            <tr>

                                <td style='
                                    padding:14px 0;
                                    color:#64748b;
                                    border-bottom:1px solid #e2e8f0;
                                '>

                                    Venue

                                </td>

                                <td style='
                                    padding:14px 0;
                                    font-weight:600;
                                    color:#0f172a;
                                    border-bottom:1px solid #e2e8f0;
                                '>

                                    {$venue}

                                </td>

                            </tr>

                            <tr>

                                <td style='
                                    padding:14px 0;
                                    color:#64748b;
                                    border-bottom:1px solid #e2e8f0;
                                '>

                                    Event Date

                                </td>

                                <td style='
                                    padding:14px 0;
                                    font-weight:600;
                                    color:#0f172a;
                                    border-bottom:1px solid #e2e8f0;
                                '>

                                    {$row['event_date']}

                                </td>

                            </tr>

                            <tr>

                                <td style='
                                    padding:14px 0;
                                    color:#64748b;
                                '>

                                    Event Time

                                </td>

                                <td style='
                                    padding:14px 0;
                                    font-weight:600;
                                    color:#0f172a;
                                '>

                                    {$row['start_time']}

                                </td>

                            </tr>

                        </table>

                    </div>

                    {$meetingHTML}

                    <div style='
                        margin-top:28px;
                        background:#eff6ff;
                        border:1px solid #bfdbfe;
                        border-radius:16px;
                        padding:20px;
                        font-size:13px;
                        color:#334155;
                        line-height:1.9;
                    '>

                        • Please refer to the SEAT Event System for the latest and most accurate updates.

                        <br>

                        • Kindly prepare earlier before the event session starts.

                    </div>

                    <div style='
                        margin-top:30px;
                        padding-top:20px;
                        border-top:1px solid #e2e8f0;
                        font-size:13px;
                        color:#64748b;
                    '>

                        Thank you for using
                        <b>SEAT Event System</b>.

                    </div>

                </div>

            </div>

        </div>
        ";

        $mail->send();

        echo "Reminder sent : ".
        $row['email']."<br>";

    }catch(Exception $e){

        echo "Failed : ".
        $e->getMessage()."<br>";
    }
}

echo "<br>Reminder completed";
?>