<?php

use RadiusTheme\RadiusBooking\Repositories\UserProfileRepository;

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

$current_user = wp_get_current_user();
$roles        = (array) $current_user->roles;
$current_role = '';

if ( in_array( 'radius_booking_customer', $roles, true ) ) {
    $current_role = 'customer';
} elseif ( in_array( 'radius_booking_employee', $roles, true ) ) {
    $current_role = 'employee';
}

$user_data         = [
        'current_role' => $current_role,
        'id'           => (int) $current_user->ID,
        'email'        => sanitize_email( $current_user->user_email ),
        'display_name' => sanitize_text_field( $current_user->display_name ),
];
// Resolve the primary booking employee for this WP user. With the Classified
// Listing integration, one WP user can own many UserProfile rows — one per
// listing-as-staff. findProfileByWPId() has no ORDER BY and would otherwise
// return whichever row MySQL handed back first, so the dashboard would
// silently lock onto a non-listing profile (when one existed) and the Manage
// Staff query would then fall through to the legacy created_by branch and
// show nothing. Prefer a listing-tied profile when present so the dashboard,
// Manage Staff, services, and work schedule all agree on the same identity.
$rtrb_listing_profile = \RadiusTheme\RadiusBooking\Models\UserProfile::where( 'wp_user_id', $current_user->ID )
    ->whereNotNull( 'listing_id' )
    ->first();
$profile = $rtrb_listing_profile ?: ( new UserProfileRepository )->findProfileByWPId( $current_user->ID );
if( $profile){
    $rtrb_user_profile = \RadiusTheme\RadiusBooking\Models\UserProfile::find( $profile->id );
    if(!empty($rtrb_user_profile->getUserId())){
        $user_data['booking_user_id'] = $rtrb_user_profile->getUserId();
    }
    $get_image         = $rtrb_user_profile->getImageUrl();
    if ( ! empty( $get_image ) ) {
        $user_data['profile_image'] = $get_image;
    }
    $fullName = $rtrb_user_profile->fullName();
    if(!empty($fullName)){
        $user_data['display_name'] = $fullName;
    }
}
?>
<script>
    window.rtrb_current_user_data = <?php echo wp_json_encode( $user_data ); ?>;
</script>

<?php
$rtrb_context    = ! empty( $atts['context'] ) ? $atts['context'] : '';
$rtrb_extra_class = ( 'classified_listing' === $rtrb_context ) ? ' rtrb-classified-listing' : '';
?>
<div id="rtrb-my-booking-dashboard" class="rtrb-root<?php echo esc_attr( $rtrb_extra_class ); ?>" data-context="<?php echo esc_attr( $rtrb_context ); ?>">
	<div class="rtrb-initial-loader" style="display:flex;align-items:center;justify-content:center;min-height:300px;">
		<svg width="36" height="36" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="animation:rtrb-spin 1s linear infinite;color:#6B7280;">
			<path d="M21 12a9 9 0 1 1-6.219-8.56"/>
		</svg>
	</div>
    <?php do_action( 'rtrb_my_booking_dashboard' ); ?>
</div>
<style>@keyframes rtrb-spin{to{transform:rotate(360deg)}}</style>
