<?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';

/* =========================================
   REGISTRATION / TICKET EMAIL
========================================= */

function sendRegistrationEmail(
    $email,
    $name,
    $event,
    $token
){

    try{

        /* =========================================
           SETUP MAIL
        ========================================= */

        $mail = setupMail();

        /* =========================================
           RECEIVER
        ========================================= */

        $mail->addAddress(
            $email,
            $name
        );

        /* =========================================
           SUBJECT
        ========================================= */

        $mail->Subject =
        "Event Ticket - ".
        $event['title'];

        /* =========================================
           QR CODE
        ========================================= */

        $qrHTML = "";

        if(
            strtolower($event['mode']) === "physical"
        ){

            $qrHTML = "

            <div style='
                text-align:center;
                margin-bottom:30px;
            '>

                <img
                src='https://api.qrserver.com/v1/create-qr-code/?size=240x240&data={$token}'
                style='
                    width:240px;
                    height:240px;
                    border-radius:12px;
                    background:#fff;
                    padding:10px;
                '>

                <div style='
                    margin-top:12px;
                    font-size:13px;
                    color:#64748b;
                '>

                    Scan this QR during attendance verification

                </div>

            </div>
            ";
        }

        /* =========================================
           MEETING LINK
        ========================================= */

        $meetingHTML = "";

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

            $meetingHTML = "

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

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

                    Join Meeting

                </a>

            </div>
            ";
        }

        /* =========================================
           VENUE
        ========================================= */

        $venue = $event['venue'] ?? '-';

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

            $venue = "Online Event";
        }

        /* =========================================
           BODY
        ========================================= */

     $mail->Body = "

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

    <div style='
        max-width:740px;
        margin:auto;
        background:#ffffff;
        border-radius:28px;
        overflow:hidden;
        box-shadow:0 12px 40px rgba(15,23,42,0.08);
        border:1px solid #e2e8f0;
    '>

        <!-- HEADER -->

        <div style='
            background:linear-gradient(
                135deg,
                #2563eb,
                #3b82f6
            );
            padding:42px;
            color:white;
        '>

            <div style='
                font-size:32px;
                font-weight:700;
                letter-spacing:-0.6px;
            '>

                Event Registration Confirmed

            </div>

            <div style='
                margin-top:12px;
                font-size:15px;
                line-height:1.8;
                opacity:0.94;
                max-width:520px;
            '>

                Your registration has been successfully confirmed.
                Please review your ticket information below.

            </div>

        </div>

        <!-- BODY -->

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

            <!-- GREETING -->

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

                Hello
                <b>{$name}</b>,

                <br><br>

                Thank you for registering for this event.
                Kindly keep this email for attendance verification and future reference.

            </div>

            <!-- QR -->

            {$qrHTML}

            <!-- TICKET CARD -->

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

                <div style='
                    font-size:19px;
                    font-weight:700;
                    color:#0f172a;
                    margin-bottom:18px;
                '>

                    Ticket Information

                </div>

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

                    <tr>

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

                            Event Name

                        </td>

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

                            {$event['title']}

                        </td>

                    </tr>

                    <tr>

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

                            Event Date

                        </td>

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

                            {$event['event_date']}

                        </td>

                    </tr>

                    <tr>

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

                            Event Time

                        </td>

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

                            {$event['event_time']}

                        </td>

                    </tr>

                    <tr>

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

                            Venue

                        </td>

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

                            {$venue}

                        </td>

                    </tr>

                    <tr>

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

                            Ticket Token

                        </td>

                        <td style='
                            padding:15px 0;
                        '>

                            <span style='
                                display:inline-block;
                                background:#dbeafe;
                                color:#1d4ed8;
                                padding:8px 14px;
                                border-radius:12px;
                                font-size:13px;
                                font-weight:700;
                                letter-spacing:0.4px;
                            '>

                                {$token}

                            </span>

                        </td>

                    </tr>

                </table>

            </div>

            <!-- ONLINE BUTTON -->

            {$meetingHTML}

            <!-- NOTES -->

            <div style='
                margin-top:30px;
                background:#eff6ff;
                border:1px solid #bfdbfe;
                border-radius:18px;
                padding:22px;
            '>

                <div style='
                    font-size:15px;
                    font-weight:700;
                    color:#1e40af;
                    margin-bottom:12px;
                '>

                    Important Notes

                </div>

                <div style='
                    font-size:13px;
                    color:#334155;
                    line-height:1.9;
                '>

                    • Please present this ticket or QR code during attendance verification.

                    <br>

                    • For online events, ensure you join using the official meeting link provided above.

                    <br>

                    • Keep your ticket token confidential and do not share it publicly.

                    <br>

                    • Arrive at least 15 minutes earlier for physical events.

                </div>

            </div>

            <!-- FOOTER -->

            <div style='
                margin-top:35px;
                padding-top:24px;
                border-top:1px solid #e2e8f0;
                font-size:13px;
                color:#64748b;
                line-height:1.9;
            '>

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

                <br><br>

                We look forward to your participation and hope you enjoy the event experience.

            </div>

        </div>

    </div>

</div>
";

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

        $mail->send();

        return true;

    }catch(\Exception $e){

        error_log(
            "Registration Email Error : ".
            $e->getMessage()
        );

        return false;
    }
}
?>