<?php
/**
 * Wizard Step 3: Availability
 * 
 * @package WahraRentalsBookings
 */

defined('ABSPATH') || exit;

$step3 = $wizard_data['step_3'] ?? [];
$offer_type = $wizard_data['step_1']['offer_type'] ?? 'day';

// Defaults
$recurring_days = $step3['recurring_days'] ?? ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];
$min_duration = absint($step3['min_duration'] ?? 1);
$max_duration = absint($step3['max_duration'] ?? 30);
$min_booking_window = absint($step3['min_booking_window'] ?? 24);
$max_booking_window = absint($step3['max_booking_window'] ?? 365);
$buffer_before = absint($step3['buffer_before'] ?? 0);
$buffer_after = absint($step3['buffer_after'] ?? 0);

$days_of_week = [
    'mon' => ['short' => __('Mo', 'wahra-rentals-bookings'), 'full' => __('Monday', 'wahra-rentals-bookings')],
    'tue' => ['short' => __('Tu', 'wahra-rentals-bookings'), 'full' => __('Tuesday', 'wahra-rentals-bookings')],
    'wed' => ['short' => __('We', 'wahra-rentals-bookings'), 'full' => __('Wednesday', 'wahra-rentals-bookings')],
    'thu' => ['short' => __('Th', 'wahra-rentals-bookings'), 'full' => __('Thursday', 'wahra-rentals-bookings')],
    'fri' => ['short' => __('Fr', 'wahra-rentals-bookings'), 'full' => __('Friday', 'wahra-rentals-bookings')],
    'sat' => ['short' => __('Sa', 'wahra-rentals-bookings'), 'full' => __('Saturday', 'wahra-rentals-bookings')],
    'sun' => ['short' => __('Su', 'wahra-rentals-bookings'), 'full' => __('Sunday', 'wahra-rentals-bookings')],
];

$duration_label = $offer_type === 'slot' ? __('hours', 'wahra-rentals-bookings') : __('days', 'wahra-rentals-bookings');
?>

<div class="wahrb-step wahrb-step-3" data-step="3">
    <div class="wahrb-step-header">
        <h2><?php esc_html_e('📅 Availability', 'wahra-rentals-bookings'); ?></h2>
        <p class="wahrb-step-description"><?php esc_html_e('When can your item be rented?', 'wahra-rentals-bookings'); ?></p>
    </div>

    <!-- Days of Week Card -->
    <div class="wahrb-availability-card">
        <h4><span class="icon">📆</span> <?php esc_html_e('Availability days', 'wahra-rentals-bookings'); ?></h4>
        
        <div class="wahrb-quick-select">
            <button type="button" class="wahrb-quick-btn" data-select="all"><?php esc_html_e('All', 'wahra-rentals-bookings'); ?></button>
            <button type="button" class="wahrb-quick-btn" data-select="weekdays"><?php esc_html_e('Weekday', 'wahra-rentals-bookings'); ?></button>
            <button type="button" class="wahrb-quick-btn" data-select="weekend"><?php esc_html_e('Weekend', 'wahra-rentals-bookings'); ?></button>
        </div>
        
        <div class="wahrb-days-grid">
            <?php foreach ($days_of_week as $key => $day): ?>
                <label class="wahrb-day-btn <?php echo esc_attr( in_array($key, $recurring_days) ? 'active' : '' ); ?>" title="<?php echo esc_attr($day['full']); ?>">
                    <input type="checkbox" name="recurring_days[]" value="<?php echo esc_attr($key); ?>" 
                           <?php checked(in_array($key, $recurring_days)); ?>>
                    <?php echo esc_html($day['short']); ?>
                </label>
            <?php endforeach; ?>
        </div>
    </div>

    <!-- Duration Card -->
    <div class="wahrb-availability-card">
        <h4><span class="icon">⏱️</span> <?php esc_html_e('Rental duration', 'wahra-rentals-bookings'); ?></h4>
        <p style="font-size: 13px; color: #64748b; margin: -8px 0 16px 0;">
            <?php esc_html_e('Set the minimum and maximum rental duration', 'wahra-rentals-bookings'); ?>
        </p>
        
        <div class="wahrb-range-row">
            <!-- Minimum Duration -->
            <div class="wahrb-duration-stepper">
                <span class="stepper-label"><?php esc_html_e('MINIMUM', 'wahra-rentals-bookings'); ?></span>
                <div class="stepper-controls">
                    <button type="button" class="stepper-btn" data-action="decrease" data-target="min_duration">−</button>
                    <div class="stepper-value">
                        <input type="number" name="min_duration" id="min_duration" 
                               value="<?php echo esc_attr($min_duration); ?>" min="1" max="365">
                        <span class="stepper-unit"><?php echo esc_html($duration_label); ?></span>
                    </div>
                    <button type="button" class="stepper-btn" data-action="increase" data-target="min_duration">+</button>
                </div>
            </div>
            
            <span class="wahrb-range-separator">→</span>
            
            <!-- Maximum Duration -->
            <div class="wahrb-duration-stepper">
                <span class="stepper-label"><?php esc_html_e('MAXIMUM', 'wahra-rentals-bookings'); ?></span>
                <div class="stepper-controls">
                    <button type="button" class="stepper-btn" data-action="decrease" data-target="max_duration">−</button>
                    <div class="stepper-value">
                        <input type="number" name="max_duration" id="max_duration" 
                               value="<?php echo esc_attr($max_duration); ?>" min="1" max="365">
                        <span class="stepper-unit"><?php echo esc_html($duration_label); ?></span>
                    </div>
                    <button type="button" class="stepper-btn" data-action="increase" data-target="max_duration">+</button>
                </div>
            </div>
        </div>
    </div>

    <?php if ($offer_type === 'slot'): 
        // Slot-specific settings
        $slot_start_hour = $step3['slot_start_hour'] ?? '08:00';
        $slot_end_hour = $step3['slot_end_hour'] ?? '18:00';
        $slot_duration = absint($step3['slot_duration'] ?? 60);
        $hours_mode = $step3['hours_mode'] ?? 'same'; // 'same' or 'per_day'
        $day_hours = $step3['day_hours'] ?? [];
        
        // Default hours per day
        $default_day_hours = [
            'mon' => ['start' => '08:00', 'end' => '18:00', 'enabled' => true],
            'tue' => ['start' => '08:00', 'end' => '18:00', 'enabled' => true],
            'wed' => ['start' => '08:00', 'end' => '18:00', 'enabled' => true],
            'thu' => ['start' => '08:00', 'end' => '18:00', 'enabled' => true],
            'fri' => ['start' => '08:00', 'end' => '18:00', 'enabled' => true],
            'sat' => ['start' => '09:00', 'end' => '17:00', 'enabled' => true],
            'sun' => ['start' => '10:00', 'end' => '16:00', 'enabled' => false],
        ];
        
        $day_names = [
            'mon' => __('Monday', 'wahra-rentals-bookings'),
            'tue' => __('Tuesday', 'wahra-rentals-bookings'),
            'wed' => __('Wednesday', 'wahra-rentals-bookings'),
            'thu' => __('Thursday', 'wahra-rentals-bookings'),
            'fri' => __('Friday', 'wahra-rentals-bookings'),
            'sat' => __('Saturday', 'wahra-rentals-bookings'),
            'sun' => __('Sunday', 'wahra-rentals-bookings'),
        ];
    ?>
    <!-- Business Hours Card (SLOT MODE ONLY) -->
    <div class="wahrb-availability-card wahrb-slot-hours-card">
        <h4><span class="icon">🕐</span> <?php esc_html_e('Business Hours', 'wahra-rentals-bookings'); ?></h4>
        
        <!-- Hours Mode Toggle -->
        <div class="wahrb-hours-mode-toggle">
            <label class="wahrb-mode-option <?php echo esc_attr( $hours_mode === 'same' ? 'active' : '' ); ?>">
                <input type="radio" name="hours_mode" value="same" <?php checked($hours_mode, 'same'); ?>>
                <?php esc_html_e('Same hours every day', 'wahra-rentals-bookings'); ?>
            </label>
            <label class="wahrb-mode-option <?php echo esc_attr( $hours_mode === 'per_day' ? 'active' : '' ); ?>">
                <input type="radio" name="hours_mode" value="per_day" <?php checked($hours_mode, 'per_day'); ?>>
                <?php esc_html_e('Different hours per day', 'wahra-rentals-bookings'); ?>
            </label>
        </div>
        
        <!-- Same Hours Section -->
        <div class="wahrb-same-hours-section" style="<?php echo esc_attr( $hours_mode === 'per_day' ? 'display:none;' : '' ); ?>">
            <div class="wahrb-range-row" style="margin-top: 16px;">
                <div class="wahrb-range-input">
                    <input type="time" name="slot_start_hour" id="slot_start_hour" 
                           value="<?php echo esc_attr($slot_start_hour); ?>">
                    <span class="label"><?php esc_html_e('Opening', 'wahra-rentals-bookings'); ?></span>
                </div>
                <span class="wahrb-range-separator">→</span>
                <div class="wahrb-range-input">
                    <input type="time" name="slot_end_hour" id="slot_end_hour" 
                           value="<?php echo esc_attr($slot_end_hour); ?>">
                    <span class="label"><?php esc_html_e('Closing', 'wahra-rentals-bookings'); ?></span>
                </div>
            </div>
        </div>
        
        <!-- Per-Day Hours Section -->
        <div class="wahrb-per-day-hours-section" style="<?php echo esc_attr( $hours_mode === 'same' ? 'display:none;' : '' ); ?>">
            <?php foreach ($day_names as $day_key => $day_name): 
                $day_data = $day_hours[$day_key] ?? $default_day_hours[$day_key];
                $day_enabled = $day_data['enabled'] ?? true;
                $day_start = $day_data['start'] ?? '08:00';
                $day_end = $day_data['end'] ?? '18:00';
            ?>
            <div class="wahrb-day-hours-row <?php echo esc_attr( $day_enabled ? '' : 'disabled' ); ?>">
                <label class="wahrb-day-toggle">
                    <input type="checkbox" name="day_hours[<?php echo esc_attr($day_key); ?>][enabled]" 
                           value="1" <?php checked($day_enabled); ?>>
                    <span class="wahrb-day-name"><?php echo esc_html($day_name); ?></span>
                </label>
                <div class="wahrb-day-times">
                    <input type="time" name="day_hours[<?php echo esc_attr($day_key); ?>][start]" 
                           value="<?php echo esc_attr($day_start); ?>" <?php echo esc_attr( $day_enabled ? '' : 'disabled' ); ?>>
                    <span>→</span>
                    <input type="time" name="day_hours[<?php echo esc_attr($day_key); ?>][end]" 
                           value="<?php echo esc_attr($day_end); ?>" <?php echo esc_attr( $day_enabled ? '' : 'disabled' ); ?>>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        
        <h4 style="margin-top: 24px;"><span class="icon">⏱️</span> <?php esc_html_e('Slot Duration', 'wahra-rentals-bookings'); ?></h4>
        <div class="wahrb-slot-durations">
            <?php 
            $durations = [
                30 => __('30 min', 'wahra-rentals-bookings'),
                60 => __('1 hour', 'wahra-rentals-bookings'),
                90 => __('1h30', 'wahra-rentals-bookings'),
                120 => __('2 hours', 'wahra-rentals-bookings'),
                180 => __('3 hours', 'wahra-rentals-bookings'),
                240 => __('4 hours', 'wahra-rentals-bookings'),
            ];
            foreach ($durations as $minutes => $label): ?>
                <label class="wahrb-slot-duration-btn <?php echo esc_attr( $slot_duration === $minutes ? 'active' : '' ); ?>">
                    <input type="radio" name="slot_duration" value="<?php echo esc_attr($minutes); ?>" 
                           <?php checked($slot_duration, $minutes); ?>>
                    <?php echo esc_html($label); ?>
                </label>
            <?php endforeach; ?>
        </div>
    </div>
    
    <?php endif; ?>

    <?php if ($offer_type === 'service'): 
        // Service-specific settings
        $service_duration = absint($step3['service_duration'] ?? 60);
        $capacity_per_slot = absint($step3['capacity_per_slot'] ?? 1);
    ?>
    <!-- Service Duration Card (SERVICE MODE ONLY) -->
    <div class="wahrb-availability-card wahrb-service-card">
        <h4><span class="icon">🔧</span> <?php esc_html_e('Session Settings', 'wahra-rentals-bookings'); ?></h4>
        
        <div class="wahrb-buffer-grid">
            <div class="wahrb-buffer-item">
                <label for="service_duration"><?php esc_html_e('Session duration', 'wahra-rentals-bookings'); ?></label>
                <input type="number" name="service_duration" id="service_duration" 
                       value="<?php echo esc_attr($service_duration); ?>" min="15" max="480" step="15">
                <div class="unit"><?php esc_html_e('minutes', 'wahra-rentals-bookings'); ?></div>
            </div>
            <div class="wahrb-buffer-item">
                <label for="capacity_per_slot"><?php esc_html_e('Capacity per slot', 'wahra-rentals-bookings'); ?></label>
                <input type="number" name="capacity_per_slot" id="capacity_per_slot" 
                       value="<?php echo esc_attr($capacity_per_slot); ?>" min="1" max="100">
                <div class="unit"><?php esc_html_e('persons', 'wahra-rentals-bookings'); ?></div>
            </div>
        </div>
    </div>
    <?php endif; ?>


    <!-- Booking Window Card -->
    <div class="wahrb-availability-card">
        <h4><span class="icon">🕐</span> <?php esc_html_e('Booking window', 'wahra-rentals-bookings'); ?></h4>
        <p style="font-size: 13px; color: #64748b; margin: -8px 0 16px 0;">
            <?php esc_html_e('How far in advance can bookings be made?', 'wahra-rentals-bookings'); ?>
        </p>
        
        <div class="wahrb-range-row">
            <div class="wahrb-range-input">
                <input type="number" name="min_booking_window" id="min_booking_window" 
                       value="<?php echo esc_attr($min_booking_window); ?>" min="0" max="720">
                <span class="label"><?php esc_html_e('min. hours', 'wahra-rentals-bookings'); ?></span>
            </div>
            <span class="wahrb-range-separator">→</span>
            <div class="wahrb-range-input">
                <input type="number" name="max_booking_window" id="max_booking_window" 
                       value="<?php echo esc_attr($max_booking_window); ?>" min="1" max="730">
                <span class="label"><?php esc_html_e('max. days', 'wahra-rentals-bookings'); ?></span>
            </div>
        </div>
    </div>

    <!-- Buffer Card -->
    <div class="wahrb-availability-card">
        <h4><span class="icon">🔄</span> <?php esc_html_e('Preparation time', 'wahra-rentals-bookings'); ?></h4>
        <p style="font-size: 13px; color: #64748b; margin: -8px 0 16px 0;">
            <?php esc_html_e('Time needed between rentals (cleaning, check...)', 'wahra-rentals-bookings'); ?>
        </p>
        
        <div class="wahrb-buffer-grid">
            <div class="wahrb-buffer-item">
                <label for="buffer_before"><?php esc_html_e('Before rental', 'wahra-rentals-bookings'); ?></label>
                <input type="number" name="buffer_before" id="buffer_before" 
                       value="<?php echo esc_attr($buffer_before); ?>" min="0" max="48">
                <div class="unit"><?php esc_html_e('hours', 'wahra-rentals-bookings'); ?></div>
            </div>
            <div class="wahrb-buffer-item">
                <label for="buffer_after"><?php esc_html_e('After rental', 'wahra-rentals-bookings'); ?></label>
                <input type="number" name="buffer_after" id="buffer_after" 
                       value="<?php echo esc_attr($buffer_after); ?>" min="0" max="48">
                <div class="unit"><?php esc_html_e('hours', 'wahra-rentals-bookings'); ?></div>
            </div>
        </div>
    </div>
</div>

<?php
// Step 3 JS is handled by wahrb-wizard-steps.js (enqueued in booking-wizard.php)
?>
