<?php
/**
 * Meta box template for Recast Paywall
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}
?>

<div class="recast-paywall-meta-box">
    <p>
        <label>
            <input type="checkbox" name="_recast_paywall_enabled" value="1" <?php checked($isPaywalled, true); ?> />
            <?php esc_html_e('Enable paywall for this post', 'recast-paywall'); ?>
        </label>
    </p>

    <p>
        <label for="_recast_gating_mode"><strong><?php esc_html_e('Gating mode', 'recast-paywall'); ?></strong></label><br>
        <select name="_recast_gating_mode" id="_recast_gating_mode">
            <?php
                $mode = get_post_meta($post->ID, '_recast_gating_mode', true) ?: 'post';
                $options = [
                    'post' => __('Post (entire article)', 'recast-paywall'),
                    'block' => __('Block (only selected blocks)', 'recast-paywall'),
                ];
                foreach ($options as $value => $label):
            ?>
                <option value="<?php echo esc_attr($value); ?>" <?php selected($mode, $value); ?>><?php echo esc_html($label); ?></option>
            <?php endforeach; ?>
        </select>
    </p>

    <?php if (!empty($itemId)): ?>
        <div class="recast-paywall-status">
            <p><strong><?php esc_html_e('Status:', 'recast-paywall'); ?></strong> 
               <span class="status-synced"><?php esc_html_e('Synced', 'recast-paywall'); ?></span>
            </p>
            
            <p><strong><?php esc_html_e('Recast ID:', 'recast-paywall'); ?></strong><br>
               <code><small><?php echo esc_html($itemId); ?></small></code>
            </p>
            
            <?php if (!empty($lastSynced)): ?>
                <p><strong><?php esc_html_e('Last Synced:', 'recast-paywall'); ?></strong><br>
                   <?php echo esc_html(date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($lastSynced))); ?>
                </p>
            <?php endif; ?>
        </div>
    <?php else: ?>
        <div class="recast-paywall-status">
            <p><strong><?php esc_html_e('Status:', 'recast-paywall'); ?></strong> 
               <span class="status-not-synced"><?php esc_html_e('Not synced', 'recast-paywall'); ?></span>
            </p>
        </div>
    <?php endif; ?>

    <hr>

    <p>
        <label for="_recast_price"><strong><?php esc_html_e('Price (in minor units)', 'recast-paywall'); ?></strong></label><br>
        <input type="number" step="1" min="0" name="_recast_price" id="_recast_price" value="<?php echo esc_attr($price); ?>" class="small-text" />
    </p>

    <p>
        <label for="_recast_status"><strong><?php esc_html_e('Status', 'recast-paywall'); ?></strong></label><br>
        <select name="_recast_status" id="_recast_status">
            <?php $statusOptions = ['PRESALE', 'PURCHASEABLE', 'UNPURCHASEABLE'];
            foreach ($statusOptions as $opt): ?>
                <option value="<?php echo esc_attr($opt); ?>" <?php selected($status, $opt); ?>><?php echo esc_html($opt); ?></option>
            <?php endforeach; ?>
        </select>
    </p>

    <p>
        <label for="_recast_geo_availability"><strong><?php esc_html_e('Geo Availability (comma-separated ISO country codes)', 'recast-paywall'); ?></strong></label><br>
        <input type="text" name="_recast_geo_availability" id="_recast_geo_availability" value="<?php echo esc_attr($geoAvailability); ?>" class="regular-text" />
        <br><small><?php esc_html_e('Use ISO 3166-1 alpha-2 codes (e.g., US, GB, CA, AU). Leave empty for global availability.', 'recast-paywall'); ?></small>
    </p>

    <p>
        <label for="_recast_affiliate_share_pct"><strong><?php esc_html_e('Affiliate Share %', 'recast-paywall'); ?></strong></label><br>
        <input type="number" step="0.01" min="0" max="100" name="_recast_affiliate_share_pct" id="_recast_affiliate_share_pct" value="<?php echo esc_attr($affiliateSharePct); ?>" class="small-text" />
    </p>

    <p>
        <button type="button" class="button button-secondary recast-sync-button" data-post-id="<?php echo esc_attr($post->ID); ?>" data-nonce="<?php echo esc_attr(wp_create_nonce('recast_sync_post')); ?>">
            <?php esc_html_e('Sync Now', 'recast-paywall'); ?>
        </button>
        <span class="spinner" style="float: none; margin-left: 5px;"></span>
    </p>

    <div class="recast-sync-message" style="display: none;"></div>
</div>
