<?php
/**
 * Booking Model
 *
 * @package Freelance_Studio
 */

if (!defined('ABSPATH')) {
    exit;
}

class FS_Booking {

    /**
     * Get table name
     */
    private static function get_table_name() {
        global $wpdb;
        return $wpdb->prefix . 'fs_bookings';
    }

    /**
     * Get a single booking by ID
     */
    public static function get($id) {
        global $wpdb;
        $table = self::get_table_name();
        
        return $wpdb->get_row($wpdb->prepare(
            "SELECT * FROM $table WHERE id = %d",
            $id
        ));
    }

    /**
     * Get booking by confirmation token
     */
    public static function get_by_token($token) {
        global $wpdb;
        $table = self::get_table_name();
        
        return $wpdb->get_row($wpdb->prepare(
            "SELECT * FROM $table WHERE confirmation_token = %s",
            $token
        ));
    }

    /**
     * Get booking by Google Calendar event ID
     */
    public static function get_by_gcal_event($event_id) {
        global $wpdb;
        $table = self::get_table_name();
        
        return $wpdb->get_row($wpdb->prepare(
            "SELECT * FROM $table WHERE gcal_event_id = %s",
            $event_id
        ));
    }

    /**
     * Get all bookings with optional filters
     */
    public static function get_all($args = array()) {
        global $wpdb;
        $table = self::get_table_name();
        $meeting_types_table = $wpdb->prefix . 'fs_meeting_types';
        $clients_table = $wpdb->prefix . 'fs_clients';
        
        $defaults = array(
            'status' => null,
            'meeting_type_id' => null,
            'client_id' => null,
            'start_date' => null,
            'end_date' => null,
            'upcoming_only' => false,
            'limit' => null,
            'offset' => 0,
            'orderby' => 'start_datetime',
            'order' => 'ASC',
        );
        
        $args = wp_parse_args($args, $defaults);
        
        $where = array('1=1');
        
        if ($args['status']) {
            if (is_array($args['status'])) {
                $statuses = implode("','", array_map('esc_sql', $args['status']));
                $where[] = "b.status IN ('$statuses')";
            } else {
                $where[] = $wpdb->prepare("b.status = %s", $args['status']);
            }
        }
        
        if ($args['meeting_type_id']) {
            $where[] = $wpdb->prepare("b.meeting_type_id = %d", $args['meeting_type_id']);
        }
        
        if ($args['client_id']) {
            $where[] = $wpdb->prepare("b.client_id = %d", $args['client_id']);
        }
        
        if ($args['start_date']) {
            $where[] = $wpdb->prepare("DATE(b.start_datetime) >= %s", $args['start_date']);
        }
        
        if ($args['end_date']) {
            $where[] = $wpdb->prepare("DATE(b.start_datetime) <= %s", $args['end_date']);
        }
        
        if ($args['upcoming_only']) {
            $where[] = "b.start_datetime >= NOW()";
        }
        
        $where_sql = implode(' AND ', $where);
        $order = esc_sql($args['order']);
        $orderby = esc_sql($args['orderby']);
        
        $limit_sql = '';
        if ($args['limit']) {
            $limit_sql = $wpdb->prepare("LIMIT %d OFFSET %d", $args['limit'], $args['offset']);
        }
        
        return $wpdb->get_results(
            "SELECT b.*, 
                    mt.name as meeting_type_name, 
                    mt.duration as meeting_duration,
                    mt.color as meeting_color,
                    mt.location_type,
                    mt.location_details,
                    c.name as client_name,
                    c.company_name as client_company,
                    c.email as client_email
             FROM $table b
             LEFT JOIN $meeting_types_table mt ON b.meeting_type_id = mt.id
             LEFT JOIN $clients_table c ON b.client_id = c.id
             WHERE $where_sql
             ORDER BY b.$orderby $order
             $limit_sql"
        );
    }

    /**
     * Get bookings for a date range (for conflict checking)
     */
    public static function get_in_range($start_date, $end_date, $exclude_cancelled = true) {
        global $wpdb;
        $table = self::get_table_name();
        
        $status_sql = $exclude_cancelled ? "AND status NOT IN ('cancelled')" : "";
        
        return $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM $table 
             WHERE DATE(start_datetime) BETWEEN %s AND %s 
             $status_sql
             ORDER BY start_datetime ASC",
            $start_date,
            $end_date
        ));
    }

    /**
     * Get upcoming bookings for a client
     */
    public static function get_client_bookings($client_id, $upcoming_only = true) {
        return self::get_all(array(
            'client_id' => $client_id,
            'upcoming_only' => $upcoming_only,
            'status' => array('confirmed'),
        ));
    }

    /**
     * Get today's bookings
     */
    public static function get_today() {
        $today = gmdate('Y-m-d');
        return self::get_all(array(
            'start_date' => $today,
            'end_date' => $today,
            'status' => 'confirmed',
        ));
    }

    /**
     * Get upcoming bookings
     */
    public static function get_upcoming($limit = 10) {
        return self::get_all(array(
            'upcoming_only' => true,
            'status' => 'confirmed',
            'limit' => $limit,
        ));
    }

    /**
     * Create a new booking
     */
    public static function create($data) {
        global $wpdb;
        $table = self::get_table_name();
        
        // Generate confirmation token
        if (empty($data['confirmation_token'])) {
            $data['confirmation_token'] = wp_generate_password(32, false);
        }
        
        $defaults = array(
            'status' => 'confirmed',
            'timezone' => get_option('fs_booking_timezone', wp_timezone_string()),
            'reminder_sent' => 0,
        );
        
        $data = wp_parse_args($data, $defaults);
        
        $result = $wpdb->insert($table, $data);
        
        if ($result === false) {
            error_log('FS Booking: Failed to create booking - ' . $wpdb->last_error);
            return false;
        }
        
        $booking_id = $wpdb->insert_id;
        
        // Create Google Calendar event (and get meet link)
        $booking = self::get($booking_id);
        self::sync_to_gcal($booking);
        
        // Note: Confirmation email is sent by the frontend router, not here
        // This prevents duplicate emails when booking is created
        
        return $booking_id;
    }

    /**
     * Update a booking
     */
    public static function update($id, $data) {
        global $wpdb;
        $table = self::get_table_name();
        
        $result = $wpdb->update($table, $data, array('id' => $id));
        
        // Update Google Calendar event (but NOT for cancelled bookings)
        $booking = self::get($id);
        if ($booking && $booking->gcal_event_id && $booking->status !== 'cancelled') {
            self::sync_to_gcal($booking);
        }
        
        return $result;
    }

    /**
     * Cancel a booking
     */
    public static function cancel($id, $reason = '', $cancelled_by = 'client') {
        global $wpdb;
        $table = self::get_table_name();
        
        $booking = self::get($id);
        if (!$booking) {
            return false;
        }
        
        // Delete Google Calendar event FIRST (before clearing the ID)
        if ($booking->gcal_event_id && FS_Google_Calendar_Service::is_connected()) {
            FS_Google_Calendar_Service::delete_event($booking->gcal_event_id);
        }
        
        // Update booking status AND clear the gcal_event_id to prevent re-sync
        $result = $wpdb->update($table, array(
            'status' => 'cancelled',
            'cancellation_reason' => $reason,
            'cancelled_at' => current_time('mysql'),
            'cancelled_by' => $cancelled_by,
            'gcal_event_id' => null, // Clear to prevent any future re-sync
        ), array('id' => $id));
        
        // Send cancellation email
        self::send_cancellation_email($booking, $reason, $cancelled_by);
        
        return $result;
    }

    /**
     * Mark booking as completed
     */
    public static function complete($id) {
        return self::update($id, array('status' => 'completed'));
    }

    /**
     * Mark booking as no-show
     */
    public static function no_show($id) {
        return self::update($id, array('status' => 'no_show'));
    }

    /**
     * Count bookings with optional filters
     *
     * @since 8.5.4
     * @param array $args Filter arguments (status, meeting_type_id, start_date, end_date)
     * @return int
     */
    public static function count($args = array()) {
        global $wpdb;
        $table = self::get_table_name();
        
        $where = array('1=1');
        
        if (!empty($args['status'])) {
            if (is_array($args['status'])) {
                $statuses = implode("','", array_map('esc_sql', $args['status']));
                $where[] = "status IN ('$statuses')";
            } else {
                $where[] = $wpdb->prepare("status = %s", $args['status']);
            }
        }
        
        if (!empty($args['meeting_type_id'])) {
            $where[] = $wpdb->prepare("meeting_type_id = %d", $args['meeting_type_id']);
        }
        
        if (!empty($args['start_date'])) {
            $where[] = $wpdb->prepare("DATE(start_datetime) >= %s", $args['start_date']);
        }
        
        if (!empty($args['end_date'])) {
            $where[] = $wpdb->prepare("DATE(start_datetime) <= %s", $args['end_date']);
        }
        
        if (!empty($args['upcoming_only'])) {
            $where[] = "start_datetime >= NOW()";
        }
        
        $where_clause = implode(' AND ', $where);
        
        return (int) $wpdb->get_var("SELECT COUNT(*) FROM {$table} WHERE {$where_clause}"); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
    }

    /**
     * Delete a booking
     */
    public static function delete($id) {
        global $wpdb;
        $table = self::get_table_name();
        
        $booking = self::get($id);
        
        // Delete Google Calendar event
        if ($booking && $booking->gcal_event_id && FS_Google_Calendar_Service::is_connected()) {
            FS_Google_Calendar_Service::delete_event($booking->gcal_event_id);
        }
        
        return $wpdb->delete($table, array('id' => $id));
    }

    /**
     * Sync booking to Google Calendar
     */
    public static function sync_to_gcal($booking) {
        if (!FS_Google_Calendar_Service::is_connected()) {
            return false;
        }
        
        // Never sync cancelled bookings to Google Calendar
        if ($booking->status === 'cancelled') {
            return false;
        }
        
        $meeting_type = FS_Meeting_Type::get($booking->meeting_type_id);
        if (!$meeting_type) {
            return false;
        }
        
        // Get attendee info
        $attendee_name = $booking->client_id 
            ? (FS_Client::get($booking->client_id)->name ?? $booking->guest_name)
            : $booking->guest_name;
        $attendee_email = $booking->client_id 
            ? (FS_Client::get($booking->client_id)->email ?? $booking->guest_email)
            : $booking->guest_email;
        
        $timezone = $booking->timezone ?: wp_timezone_string();
        
        $event_data = array(
            'summary' => $meeting_type->name . ' - ' . $attendee_name,
            'description' => self::build_event_description($booking, $meeting_type),
            'start' => array(
                'dateTime' => gmdate('Y-m-d\TH:i:s', strtotime($booking->start_datetime)),
                'timeZone' => $timezone,
            ),
            'end' => array(
                'dateTime' => gmdate('Y-m-d\TH:i:s', strtotime($booking->end_datetime)),
                'timeZone' => $timezone,
            ),
            'extendedProperties' => array(
                'private' => array(
                    'fs_type' => 'booking',
                    'fs_booking_id' => $booking->id,
                ),
            ),
        );
        
        // NOTE: We intentionally do NOT add the guest as a Google Calendar attendee.
        // This prevents any possibility of Google creating duplicate meeting links or
        // sending its own notifications. All guest communication goes through our plugin.
        // The guest's info is included in the event description for your reference.
        
        // Add Google Meet link ONLY if:
        // 1. Meeting type is video AND auto-meet is enabled
        // 2. AND booking doesn't already have a meet_link (prevents creating duplicate links on updates)
        $with_conference = false;
        $needs_new_conference = $meeting_type->location_type === 'video' 
            && get_option('fs_booking_auto_google_meet', '0') === '1'
            && empty($booking->meet_link);
        
        if ($needs_new_conference) {
            // Use booking ID as requestId for idempotency - same ID = same conference
            $event_data['conferenceData'] = array(
                'createRequest' => array(
                    'requestId' => 'fs-booking-' . $booking->id,
                    'conferenceSolutionKey' => array(
                        'type' => 'hangoutsMeet',
                    ),
                ),
            );
            $with_conference = true;
        }
        
        $result = FS_Google_Calendar_Service::upsert_event($event_data, $booking->gcal_event_id, $with_conference);
        
        if (!$result) {
            return false;
        }
        
        // Extract event ID and meet link from result
        $update_data = array();
        
        if ($with_conference && is_array($result)) {
            // Full event data returned
            $event_id = $result['id'] ?? null;
            
            // Extract Google Meet link from conference data
            if (isset($result['conferenceData']['entryPoints'])) {
                foreach ($result['conferenceData']['entryPoints'] as $entry_point) {
                    if ($entry_point['entryPointType'] === 'video') {
                        $update_data['meet_link'] = $entry_point['uri'];
                        break;
                    }
                }
            }
            // Fallback to hangoutLink if available
            if (empty($update_data['meet_link']) && isset($result['hangoutLink'])) {
                $update_data['meet_link'] = $result['hangoutLink'];
            }
        } else {
            // Just the event ID returned
            $event_id = is_array($result) ? ($result['id'] ?? null) : $result;
        }
        
        if ($event_id && $event_id !== $booking->gcal_event_id) {
            $update_data['gcal_event_id'] = $event_id;
        }
        
        // Update booking with event ID and/or meet link
        if (!empty($update_data)) {
            global $wpdb;
            $table = self::get_table_name();
            $wpdb->update($table, $update_data, array('id' => $booking->id));
        }
        
        return $event_id;
    }

    /**
     * Build event description
     */
    private static function build_event_description($booking, $meeting_type) {
        $lines = array();
        
        $lines[] = "Meeting Type: " . $meeting_type->name;
        $lines[] = "Duration: " . FS_Meeting_Type::get_duration_display($meeting_type->duration);
        
        if ($meeting_type->location_type !== 'custom') {
            $lines[] = "Location: " . FS_Meeting_Type::get_location_display($meeting_type->location_type);
        } elseif ($meeting_type->location_details) {
            $lines[] = "Location: " . $meeting_type->location_details;
        }
        
        $attendee_name = $booking->guest_name;
        $attendee_email = $booking->guest_email;
        $attendee_phone = $booking->guest_phone;
        
        if ($booking->client_id) {
            $client = FS_Client::get($booking->client_id);
            if ($client) {
                $attendee_name = $client->name;
                $attendee_email = $client->email;
                $attendee_phone = $client->phone;
            }
        }
        
        $lines[] = "";
        $lines[] = "Attendee: " . $attendee_name;
        if ($attendee_email) $lines[] = "Email: " . $attendee_email;
        if ($attendee_phone) $lines[] = "Phone: " . $attendee_phone;
        
        if ($booking->notes) {
            $lines[] = "";
            $lines[] = "Notes: " . $booking->notes;
        }
        
        $lines[] = "";
        $lines[] = "Booked via Freelance Studio";
        
        return implode("\n", $lines);
    }

    /**
     * Send confirmation email
     */
    public static function send_confirmation_email($booking) {
        $meeting_type = FS_Meeting_Type::get($booking->meeting_type_id);
        $business_name = get_option('fs_business_name', 'Business');
        
        $to = $booking->guest_email;
        if ($booking->client_id) {
            $client = FS_Client::get($booking->client_id);
            if ($client && $client->email) {
                $to = $client->email;
            }
        }
        
        if (!$to) {
            return false;
        }
        
        $subject = sprintf(
            /* translators: 1. service name, 2. client name */
            __('Booking Confirmed: %1$s with %2$s', 'freelance-studio'),
            $meeting_type->name,
            $business_name
        );
        
        $date = gmdate('l, F j, Y', strtotime($booking->start_datetime));
        $time = gmdate('g:i A', strtotime($booking->start_datetime)) . ' - ' . gmdate('g:i A', strtotime($booking->end_datetime));
        $location = FS_Meeting_Type::get_location_display($meeting_type->location_type, $meeting_type->location_details);
        
        $cancel_url = home_url('/fs-booking/cancel/' . $booking->confirmation_token);
        
        // Build HTML content
        $content = '<p style="margin: 0 0 24px 0;">' . __('Your booking has been confirmed!', 'freelance-studio') . '</p>';
        
        // Meeting details box
        $content .= '<table width="100%" cellpadding="0" cellspacing="0" style="background-color: #f9fafb; border-radius: 8px; margin-bottom: 24px;">';
        $content .= '<tr><td style="padding: 24px;">';
        $content .= '<h3 style="margin: 0 0 16px 0; color: #111827; font-size: 18px;">' . esc_html($meeting_type->name) . '</h3>';
        $content .= '<p style="margin: 0 0 8px 0; color: #4b5563;"><strong>' . __('Date:', 'freelance-studio') . '</strong> ' . esc_html($date) . '</p>';
        $content .= '<p style="margin: 0 0 8px 0; color: #4b5563;"><strong>' . __('Time:', 'freelance-studio') . '</strong> ' . esc_html($time) . '</p>';
        $content .= '<p style="margin: 0; color: #4b5563;"><strong>' . __('Location:', 'freelance-studio') . '</strong> ' . esc_html($location) . '</p>';
        $content .= '</td></tr></table>';
        
        $content .= '<p style="margin: 0; color: #6b7280; font-size: 14px;">';
        $content .= __('Need to cancel? ', 'freelance-studio');
        $content .= '<a href="' . esc_url($cancel_url) . '" style="color: #4173B0;">' . __('Click here to cancel your booking', 'freelance-studio') . '</a>';
        $content .= '</p>';
        
        return FS_Email_Service::send_styled_email(
            $to,
            $subject,
            __('Booking Confirmed', 'freelance-studio'),
            $content
        );
    }

    /**
     * Send cancellation email
     */
    public static function send_cancellation_email($booking, $reason = '', $cancelled_by = 'client') {
        $meeting_type = FS_Meeting_Type::get($booking->meeting_type_id);
        $business_name = get_option('fs_business_name', 'Business');
        $business_email = get_option('fs_business_email', get_option('admin_email'));
        
        $guest_email = $booking->guest_email;
        if ($booking->client_id) {
            $client = FS_Client::get($booking->client_id);
            if ($client && $client->email) {
                $guest_email = $client->email;
            }
        }
        
        $date = gmdate('l, F j, Y', strtotime($booking->start_datetime));
        $time = gmdate('g:i A', strtotime($booking->start_datetime));
        
        // Always send confirmation to guest
        if ($guest_email) {
            $subject = sprintf(
                /* translators: 1. service name */
                __('Booking Cancelled: %s', 'freelance-studio'),
                $meeting_type->name
            );
            
            // Build HTML content for guest
            $content = '<p style="margin: 0 0 24px 0;">' . __('Your booking has been cancelled.', 'freelance-studio') . '</p>';
            
            // Meeting details box
            $content .= '<table width="100%" cellpadding="0" cellspacing="0" style="background-color: #fef2f2; border-radius: 8px; margin-bottom: 24px;">';
            $content .= '<tr><td style="padding: 24px;">';
            $content .= '<h3 style="margin: 0 0 16px 0; color: #991b1b; font-size: 18px;">' . esc_html($meeting_type->name) . '</h3>';
            $content .= '<p style="margin: 0 0 8px 0; color: #7f1d1d;"><strong>' . __('Date:', 'freelance-studio') . '</strong> ' . esc_html($date) . '</p>';
            $content .= '<p style="margin: 0; color: #7f1d1d;"><strong>' . __('Time:', 'freelance-studio') . '</strong> ' . esc_html($time) . '</p>';
            if ($reason) {
                $content .= '<p style="margin: 16px 0 0 0; color: #7f1d1d;"><strong>' . __('Reason:', 'freelance-studio') . '</strong> ' . esc_html($reason) . '</p>';
            }
            $content .= '</td></tr></table>';
            
            $content .= '<p style="margin: 0; color: #6b7280;">' . __('If you\'d like to reschedule, please visit our booking page.', 'freelance-studio') . '</p>';
            
            FS_Email_Service::send_styled_email(
                $guest_email,
                $subject,
                __('Booking Cancelled', 'freelance-studio'),
                $content
            );
        }
        
        // Notify business owner when client cancels
        if ($cancelled_by === 'client' && $business_email) {
            $owner_subject = sprintf(
                /* translators: 1. service name, 2. client name, 3. date */
                __('Booking Cancelled: %1$s with %2$s on %3$s', 'freelance-studio'),
                $meeting_type->name,
                $booking->guest_name,
                date_i18n(get_option('date_format'), strtotime($booking->start_datetime))
            );
            
            /* translators: 1. has cancelled value */
            $owner_content = '<p style="margin: 0 0 24px 0;">' . sprintf(__('%s has cancelled their booking.', 'freelance-studio'), esc_html($booking->guest_name)) . '</p>';
            
            $owner_content .= '<table width="100%" cellpadding="0" cellspacing="0" style="background-color: #fef2f2; border-radius: 8px; margin-bottom: 24px;">';
            $owner_content .= '<tr><td style="padding: 24px;">';
            $owner_content .= '<h3 style="margin: 0 0 16px 0; color: #991b1b; font-size: 18px;">' . esc_html($meeting_type->name) . '</h3>';
            $owner_content .= '<p style="margin: 0 0 8px 0; color: #4b5563;"><strong>' . __('Guest:', 'freelance-studio') . '</strong> ' . esc_html($booking->guest_name) . '</p>';
            $owner_content .= '<p style="margin: 0 0 8px 0; color: #4b5563;"><strong>' . __('Email:', 'freelance-studio') . '</strong> ' . esc_html($guest_email) . '</p>';
            $owner_content .= '<p style="margin: 0 0 8px 0; color: #7f1d1d;"><strong>' . __('Date:', 'freelance-studio') . '</strong> ' . esc_html($date) . '</p>';
            $owner_content .= '<p style="margin: 0; color: #7f1d1d;"><strong>' . __('Time:', 'freelance-studio') . '</strong> ' . esc_html($time) . '</p>';
            if ($reason) {
                $owner_content .= '<p style="margin: 16px 0 0 0; color: #7f1d1d;"><strong>' . __('Reason:', 'freelance-studio') . '</strong> ' . esc_html($reason) . '</p>';
            }
            $owner_content .= '</td></tr></table>';
            
            FS_Email_Service::send_styled_email(
                $business_email,
                $owner_subject,
                __('Booking Cancelled', 'freelance-studio'),
                $owner_content
            );
        }
        
        return true;
    }

    /**
     * Send reminder emails for upcoming bookings
     */
    public static function send_reminders() {
        $reminder_hours = get_option('fs_booking_reminder_hours', 24);
        
        $reminder_time = gmdate('Y-m-d H:i:s', strtotime('+' . $reminder_hours . ' hours'));
        $now = current_time('mysql');
        
        global $wpdb;
        $table = self::get_table_name();
        
        // Only send reminders to bookings where:
        // 1. Status is confirmed
        // 2. Reminder hasn't been sent yet
        // 3. Meeting is within the reminder window
        // 4. Guest wants to receive a reminder (wants_reminder = 1 or NULL for legacy bookings)
        $bookings = $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM $table 
             WHERE status = 'confirmed' 
             AND reminder_sent = 0 
             AND (wants_reminder = 1 OR wants_reminder IS NULL)
             AND start_datetime BETWEEN %s AND %s",
            $now,
            $reminder_time
        ));
        
        foreach ($bookings as $booking) {
            self::send_reminder_email($booking);
            $wpdb->update($table, array('reminder_sent' => 1), array('id' => $booking->id));
        }
        
        return count($bookings);
    }

    /**
     * Send reminder email
     */
    private static function send_reminder_email($booking) {
        $meeting_type = FS_Meeting_Type::get($booking->meeting_type_id);
        $business_name = get_option('fs_business_name', 'Business');
        
        $to = $booking->guest_email;
        if ($booking->client_id) {
            $client = FS_Client::get($booking->client_id);
            if ($client && $client->email) {
                $to = $client->email;
            }
        }
        
        if (!$to) {
            return false;
        }
        
        $subject = sprintf(
            /* translators: 1. service name, 2. client name */
            __('Reminder: %1$s tomorrow with %2$s', 'freelance-studio'),
            $meeting_type->name,
            $business_name
        );
        
        $date = gmdate('l, F j, Y', strtotime($booking->start_datetime));
        $time = gmdate('g:i A', strtotime($booking->start_datetime));
        $location = FS_Meeting_Type::get_location_display($meeting_type->location_type, $meeting_type->location_details);
        
        // Build HTML content
        $content = '<p style="margin: 0 0 24px 0;">' . __('This is a reminder about your upcoming meeting.', 'freelance-studio') . '</p>';
        
        // Meeting details box
        $content .= '<table width="100%" cellpadding="0" cellspacing="0" style="background-color: #fef3c7; border-radius: 8px; margin-bottom: 24px;">';
        $content .= '<tr><td style="padding: 24px;">';
        $content .= '<h3 style="margin: 0 0 16px 0; color: #92400e; font-size: 18px;">' . esc_html($meeting_type->name) . '</h3>';
        $content .= '<p style="margin: 0 0 8px 0; color: #78350f;"><strong>' . __('Date:', 'freelance-studio') . '</strong> ' . esc_html($date) . '</p>';
        $content .= '<p style="margin: 0 0 8px 0; color: #78350f;"><strong>' . __('Time:', 'freelance-studio') . '</strong> ' . esc_html($time) . '</p>';
        $content .= '<p style="margin: 0; color: #78350f;"><strong>' . __('Location:', 'freelance-studio') . '</strong> ' . esc_html($location) . '</p>';
        $content .= '</td></tr></table>';
        
        $content .= '<p style="margin: 0; color: #4b5563;">' . __('We look forward to meeting with you!', 'freelance-studio') . '</p>';
        
        return FS_Email_Service::send_styled_email(
            $to,
            $subject,
            __('Meeting Reminder', 'freelance-studio'),
            $content
        );
    }

    /**
     * Get status badge HTML
     */
    public static function get_status_badge($status) {
        $badges = array(
            'confirmed' => '<span class="fs-badge fs-badge-success">' . __('Confirmed', 'freelance-studio') . '</span>',
            'cancelled' => '<span class="fs-badge fs-badge-danger">' . __('Cancelled', 'freelance-studio') . '</span>',
            'completed' => '<span class="fs-badge fs-badge-info">' . __('Completed', 'freelance-studio') . '</span>',
            'no_show' => '<span class="fs-badge fs-badge-warning">' . __('No Show', 'freelance-studio') . '</span>',
        );
        
        return isset($badges[$status]) ? $badges[$status] : $status;
    }
}
