<?php

namespace App\Jobs;

use App\GuestCheckin;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Log;

class SendCheckinNotificationEmails implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $checkin;
    protected $input;

    public function __construct($checkin, $input)
    {
        $this->checkin = $checkin;
        $this->input = $input;
    }

    public function handle()
    {
        try {
            // Enable SwiftMailer debugging
            \Swift_Preferences::getInstance()->setDebug(true);

            // Guest email
            if (!empty($this->input['email']) && filter_var($this->input['email'], FILTER_VALIDATE_EMAIL)) {
                $customerHtml = '
                    <div style="font-family: Arial, sans-serif; color: #333; padding: 20px; background: #f7f7f7;">
                        <div style="max-width: 600px; margin: auto; background: #fff; border-radius: 10px; padding: 20px; box-shadow: 0 0 10px rgba(0,0,0,0.05);">
                            <h2 style="color: #0055aa;">Guest Registration Confirmation</h2>
                            <p>Dear ' . htmlspecialchars($this->input['surname'] . ' ' . $this->input['name']) . ',</p>
                            <p>Thank you for submitting your guest details to Coffee Garden Suite. We have received your information and will process your request shortly.</p>
                            <div style="background: #e8f5e8; padding: 15px; border-radius: 5px; margin: 20px 0; border-left: 4px solid #4CAF50;">
                                <p style="margin: 0; font-weight: bold; color: #2e7d32;">✅ Guest Details Received</p>
                            </div>
                            <div style="background: #f9f9f9; padding: 15px; border-radius: 5px; margin: 20px 0;">
                                <h3 style="color: #0055aa; margin-top: 0;">Guest Details:</h3>
                                <table style="width: 100%; border-collapse: collapse;">
                                    <tr><td style="padding: 5px 0;"><strong>Surname:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars($this->input['surname']) . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Name:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars($this->input['name']) . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Phone:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars($this->input['phone']) . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Email:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars($this->input['email']) . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Gender:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars($this->input['gender']) . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Nationality:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['nationality']) ? $this->input['nationality'] : 'N/A') . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>ID Type:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['id_type']) ? $this->input['id_type'] : 'N/A') . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>ID Number:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['id_number']) ? $this->input['id_number'] : 'N/A') . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Passport Number:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['passport_no']) ? $this->input['passport_no'] : 'N/A') . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Purpose of Stay:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['stay_purpose']) ? $this->input['stay_purpose'] : 'N/A') . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Payment Method:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['payment_method']) ? $this->input['payment_method'] : 'N/A') . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Company:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['company']) ? $this->input['company'] : 'N/A') . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Remarks:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['remarks']) ? $this->input['remarks'] : 'N/A') . '</td></tr>
                                </table>
                            </div>
                            <div style="background: #e1f5fe; padding: 15px; border-radius: 5px; margin: 20px 0;">
                                <h3 style="color: #0277bd; margin-top: 0;">📋 Next Steps:</h3>
                                <ul style="margin: 10px 0; padding-left: 20px;">
                                    <li>Our team will confirm your stay details and room availability.</li>
                                    <li>Contact us if you need to make changes to your request.</li>
                                </ul>
                            </div>
                            <p style="margin-top: 20px;">If you have any questions, please contact us at:</p>
                            <ul>
                                <li>Email: <a href="mailto:info@coffeegardensuite.co.ke">info@coffeegardensuite.co.ke</a></li>
                                <li>Phone: +254 722 229 905</li>
                            </ul>
                            <p style="margin-top: 30px;">We look forward to welcoming you!</p>
                            <p style="margin-top: 10px;">Warm regards,<br/><strong>Coffee Garden Suite Team</strong></p>
                        </div>
                    </div>';
                Mail::send([], [], function ($message) use ($customerHtml) {
                    $message->to($this->input['email'])
                            ->from(config('mail.from.address'), config('mail.from.name'))
                            ->subject('Guest Registration Submitted')
                            ->setBody($customerHtml, 'text/html');
                });
                Log::info("Guest email sent successfully to {$this->input['email']} for check-in ID {$this->checkin->id}");
            } else {
                Log::warning("Guest email not sent: Invalid or missing email for check-in ID {$this->checkin->id}");
            }

            // Admin email
            $adminEmail = config('mail.admin_email', 'test@coffeegardensuite.co.ke');
            if ($adminEmail && filter_var($adminEmail, FILTER_VALIDATE_EMAIL)) {
                $adminHtml = '
                    <div style="font-family: Arial, sans-serif; color: #333; padding: 20px; background: #f7f7f7;">
                        <div style="max-width: 600px; margin: auto; background: #fff; border-radius: 10px; padding: 20px; box-shadow: 0 0 10px rgba(0,0,0,0.05);">
                            <h2 style="color: #d32f2f;">🏨 New Guest Registration</h2>
                            <p><strong>A new guest has submitted their details to Coffee Garden Suite.</strong></p>
                            <div style="background: #e8f5e8; padding: 15px; border-radius: 5px; margin: 20px 0; border-left: 4px solid #4CAF50;">
                                <h3 style="color: #2e7d32; margin-top: 0;">✅ Guest Details Submitted</h3>
                                <p style="margin: 5px 0;"><strong>Check-in ID:</strong> #' . $this->checkin->id . '</p>
                                <p style="margin: 5px 0;"><strong>Status:</strong> <span style="color: #2e7d32; font-weight: bold;">PENDING REVIEW</span></p>
                                <p style="margin: 5px 0;"><strong>Submitted:</strong> ' . date('F j, Y g:i A') . '</p>
                            </div>
                            <div style="background: #f9f9f9; padding: 15px; border-radius: 5px; margin: 20px 0;">
                                <h3 style="color: #0055aa; margin-top: 0;">👤 Guest Information:</h3>
                                <table style="width: 100%; border-collapse: collapse;">
                                    <tr><td style="padding: 5px 0;"><strong>Surname:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars($this->input['surname']) . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Name:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars($this->input['name']) . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Phone:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars($this->input['phone']) . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Email:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars($this->input['email']) . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Gender:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars($this->input['gender']) . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Nationality:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['nationality']) ? $this->input['nationality'] : 'N/A') . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>ID Type:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['id_type']) ? $this->input['id_type'] : 'N/A') . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>ID Number:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['id_number']) ? $this->input['id_number'] : 'N/A') . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Passport Number:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['passport_no']) ? $this->input['passport_no'] : 'N/A') . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Purpose of Stay:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['stay_purpose']) ? $this->input['stay_purpose'] : 'N/A') . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Payment Method:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['payment_method']) ? $this->input['payment_method'] : 'N/A') . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Company:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['company']) ? $this->input['company'] : 'N/A') . '</td></tr>
                                    <tr><td style="padding: 5px 0;"><strong>Remarks:</strong></td><td style="padding: 5px 0;">' . htmlspecialchars(isset($this->input['remarks']) ? $this->input['remarks'] : 'N/A') . '</td></tr>
                                </table>
                            </div>
                            <div style="background: #ffebee; padding: 15px; border-radius: 5px; margin: 20px 0; border-left: 4px solid #f44336;">
                                <h3 style="color: #c62828; margin-top: 0;">📋 Staff Actions Required:</h3>
                                <ul style="margin: 0; padding-left: 20px;">
                                    <li>Contact guest to confirm stay details and room availability.</li>
                                    <li>Update the property management system with guest information.</li>
                                    <li>Verify payment details if applicable.</li>
                                </ul>
                            </div>
                            <div style="background: #f3e5f5; padding: 15px; border-radius: 5px; margin: 20px 0;">
                                <h3 style="color: #7b1fa2; margin-top: 0;">📊 Quick Stats:</h3>
                                <p style="margin: 5px 0;">• Guest has been automatically notified via email.</p>
                                <p style="margin: 5px 0;">• Submission received: ' . date('F j, Y g:i A') . '</p>
                            </div>
                            <p style="text-align: center; margin-top: 30px;">
                                <a href="' . url('/admin/checkins/' . $this->checkin->id) . '" style="background: #4CAF50; color: white; padding: 12px 25px; text-decoration: none; border-radius: 5px; display: inline-block;">
                                    View Check-in Details
                                </a>
                            </p>
                            <p style="margin-top: 20px; font-size: 12px; color: #666; text-align: center;">
                                This is an automated notification from the Coffee Garden Suite Management System.
                            </p>
                        </div>
                    </div>';
                Mail::send([], [], function ($message) use ($adminEmail, $adminHtml) {
                    $message->to($adminEmail)
                            ->from(config('mail.from.address'), config('mail.from.name'))
                            ->subject('🏨 New Guest Registration - ' . htmlspecialchars($this->input['surname'] . ' ' . $this->input['name']))
                            ->setBody($adminHtml, 'text/html');
                });
                Log::info("Admin email sent successfully to {$adminEmail} for check-in ID {$this->checkin->id}");
            } else {
                Log::warning("Admin email not sent: Invalid or missing admin email for check-in ID {$this->checkin->id}");
            }
        } catch (\Exception $e) {
            Log::error("Check-in notification email sending failed for check-in ID {$this->checkin->id}: {$e->getMessage()}");
        }
    }
}