<?php
/**
 * MeetingTypeList Shortcode
 *
 * Renders a public listing of all active meeting types.
 * Usage: [rtrb_meeting_list]
 *
 * @package RadiusTheme\RadiusBooking\Shortcodes
 * @since   1.2.0
 */

namespace RadiusTheme\RadiusBooking\Shortcodes;

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

use RadiusTheme\RadiusBooking\Helpers\SettingsHelper;
use RadiusTheme\RadiusBooking\Models\MeetingType;

/**
 * Class MeetingTypeList
 */
class MeetingTypeList {

	/**
	 * Render the meeting type list shortcode output.
	 *
	 * @param array $atts Shortcode attributes.
	 *
	 * @return string HTML output.
	 */
	public static function output( $atts = array() ): string {
		if ( ! rtrb_is_module_active( 'meeting' ) ) {
			return '';
		}

		$atts = shortcode_atts(
			array(
				'employee_id' => '',
				'columns'     => '3',
			),
			$atts,
			'rtrb_meeting_list'
		);

		$query = MeetingType::where( 'is_active', 1 );
		if ( ! empty( $atts['employee_id'] ) ) {
			$query = $query->where( 'employee_id', (int) $atts['employee_id'] );
		}
		$meeting_types = $query->get();

		if ( empty( $meeting_types ) ) {
			return '<p class="rtrb-no-meetings">' . esc_html__( 'No meeting types available.', 'radius-booking' ) . '</p>';
		}

		$pages          = SettingsHelper::get_setting( 'pages' );
		$booking_page   = ! empty( $pages['meetingBookingPage'] ) ? get_permalink( $pages['meetingBookingPage'] ) : '';
		$columns        = max( 1, min( 4, (int) $atts['columns'] ) );

		ob_start();
		?>
		<div class="rtrb-meeting-list" style="display:grid;grid-template-columns:repeat(<?php echo esc_attr( $columns ); ?>,1fr);gap:20px;">
			<?php foreach ( $meeting_types as $mt ) :
				$name        = $mt->name ?? $mt->attributes['name'] ?? '';
				$description = $mt->description ?? $mt->attributes['description'] ?? '';
				$duration    = (int) ( $mt->duration ?? $mt->attributes['duration'] ?? 0 );
				$price       = (float) ( $mt->price ?? $mt->attributes['price'] ?? 0 );
				$slug        = $mt->slug ?? $mt->attributes['slug'] ?? '';
				$color       = $mt->color ?? $mt->attributes['color'] ?? '#0040ff';
				$employee_id = $mt->employee_id ?? $mt->attributes['employee_id'] ?? 0;
				$employee    = $employee_id ? get_userdata( $employee_id ) : null;
				$link        = $booking_page ? add_query_arg( 'meeting', $slug, $booking_page ) : '#';
				?>
				<div class="rtrb-meeting-card" style="border:1px solid #e2e8f0;border-radius:12px;padding:24px;background:#fff;transition:box-shadow .2s;">
					<div style="width:40px;height:4px;border-radius:2px;background:<?php echo esc_attr( $color ); ?>;margin-bottom:16px;"></div>
					<h3 style="margin:0 0 8px;font-size:18px;font-weight:600;color:#1a202c;"><?php echo esc_html( $name ); ?></h3>
					<?php if ( $description ) : ?>
						<p style="margin:0 0 12px;font-size:14px;color:#718096;line-height:1.5;"><?php echo esc_html( $description ); ?></p>
					<?php endif; ?>
					<div style="display:flex;flex-wrap:wrap;gap:12px;margin-bottom:16px;font-size:13px;color:#4a5568;">
						<span>⏱ <?php echo esc_html( $duration ); ?> min</span>
						<?php if ( $price > 0 ) : ?>
							<span>💰 <?php echo esc_html( get_rtrb_currency_symbol() . number_format( $price, 2 ) ); ?></span>
						<?php else : ?>
							<span style="color:#38a169;">Free</span>
						<?php endif; ?>
						<?php if ( $employee ) : ?>
							<span>👤 <?php echo esc_html( $employee->display_name ); ?></span>
						<?php endif; ?>
					</div>
					<a href="<?php echo esc_url( $link ); ?>"
					   style="display:inline-block;padding:10px 24px;background:<?php echo esc_attr( $color ); ?>;color:#fff;border-radius:8px;text-decoration:none;font-size:14px;font-weight:500;transition:opacity .2s;"
					   onmouseover="this.style.opacity='0.9'" onmouseout="this.style.opacity='1'">
						<?php esc_html_e( 'Book Now', 'radius-booking' ); ?>
					</a>
				</div>
			<?php endforeach; ?>
		</div>
		<?php
		return ob_get_clean();
	}
}
