<?php
/**
 * Booking Event Subscriber
 *
 * Handles booking-related events
 *
 * @package NemtlyBooking
 * @since   PBOOKING_SINCE
 */

namespace NemtlyBooking\Subscribers;

use NemtlyBooking\Interfaces\Event_Subscriber_Interface;

/**
 * Booking Event Subscriber class
 *
 * @since PBOOKING_SINCE
 */
class Booking_Event_Subscriber implements Event_Subscriber_Interface {

    /**
     * Get subscribed events
     *
     * @since PBOOKING_SINCE
     *
     * @return array Array of event names and their corresponding methods.
     */
    public function get_subscribed_events() {
        return [
            'booking.created'        => 'on_booking_created',
            'booking.updated'        => 'on_booking_updated',
            'booking.cancelled'      => 'on_booking_cancelled',
            'booking.confirmed'      => 'on_booking_confirmed',
            'booking.status_changed' => 'on_booking_status_changed',
        ];
    }

    /**
     * Handle booking created event
     *
     * @since PBOOKING_SINCE
     *
     * @param array $data Event data.
     *
     * @return array Modified event data.
     */
    public function on_booking_created( $data ) {
        // Send confirmation email
        if ( isset( $data['booking_id'] ) ) {
            do_action( 'nemtly_booking_send_confirmation', $data['booking_id'] );
        }

        // Update event availability
        if ( isset( $data['event_id'] ) ) {
            do_action( 'nemtly_booking_update_availability', $data['event_id'] );
        }

        return $data;
    }

    /**
     * Handle booking updated event
     *
     * @since PBOOKING_SINCE
     *
     * @param array $data Event data.
     *
     * @return array Modified event data.
     */
    public function on_booking_updated( $data ) {
        // Send update notification
        if ( isset( $data['booking_id'] ) ) {
            do_action( 'nemtly_booking_send_update_notification', $data['booking_id'] );
        }

        return $data;
    }

    /**
     * Handle booking cancelled event
     *
     * @since PBOOKING_SINCE
     *
     * @param array $data Event data.
     *
     * @return array Modified event data.
     */
    public function on_booking_cancelled( $data ) {
        // Send cancellation email
        if ( isset( $data['booking_id'] ) ) {
            do_action( 'nemtly_booking_send_cancellation', $data['booking_id'] );
        }

        // Update event availability
        if ( isset( $data['event_id'] ) ) {
            do_action( 'nemtly_booking_update_availability', $data['event_id'] );
        }

        return $data;
    }

    /**
     * Handle booking confirmed event
     *
     * @since PBOOKING_SINCE
     *
     * @param array $data Event data.
     *
     * @return array Modified event data.
     */
    public function on_booking_confirmed( $data ) {
        // Send confirmation email
        if ( isset( $data['booking_id'] ) ) {
            do_action( 'nemtly_booking_send_confirmation', $data['booking_id'] );
        }

        return $data;
    }

    /**
     * Handle booking status changed event
     *
     * @since PBOOKING_SINCE
     *
     * @param array $data Event data.
     *
     * @return array Modified event data.
     */
    public function on_booking_status_changed( $data ) {
        // Send status notification
        if ( isset( $data['booking_id'] ) ) {
            do_action( 'nemtly_booking_send_status_notification', $data['booking_id'] );
        }

        return $data;
    }
}
