<?php
/**
 * Snooze settings — admin options page partial.
 *
 * Two columns on the Autoship Cloud > Settings > Options page that
 * control the Snooze feature: the Native UI action settings (label,
 * pause-action behavior, preview) and the frequency source picker
 * (QMC vs plugin defaults, custom date toggle, max-duration cap).
 *
 * Included from `templates/admin/settings/options.php`. Shares variable
 * scope with the parent file, so `$autoship_settings` is available
 * without any explicit forwarding.
 *
 * @package    Autoship
 * @subpackage Snooze\Admin
 * @since      2.13.0
 *
 * @var array $autoship_settings Plugin settings array from the parent template.
 */

defined( 'ABSPATH' ) || exit;
?>
<!-- ====================================================================
     SNOOZE MODULE 1: Native UI: Snooze Action
     ==================================================================== -->
<div class="asc-settings-column">
    <h3>
        <i class="pi pi-clock"></i>
        <?php echo esc_html( __( 'Native UI: Snooze Action', 'autoship' ) ); ?>
        <span class="asc-badge asc-badge-new"><?php echo esc_html( __( 'NEW', 'autoship' ) ); ?></span>
    </h3>
    <p class="asc-card-description"><?php echo esc_html( __( "Control whether the Snooze action appears in the subscriber's Scheduled Order view, how it is labeled, and whether it replaces or sits alongside the Pause action.", 'autoship' ) ); ?></p>

    <?php if ( autoship_qpilot_snooze_flag_definitively_off() ) : ?>
        <div class="asc-alert asc-alert-warning" style="margin-bottom: 1rem;">
            <i class="pi pi-exclamation-triangle"></i>
            <div class="asc-alert-content">
                <p style="margin: 0 0 0.5rem; font-weight: 600;">
                    <?php echo esc_html( __( 'Snooze is not enabled on your QPilot site', 'autoship' ) ); ?>
                </p>
                <small><?php echo esc_html( __( 'The QPilot Snooze feature flag is not active on your connected site. Subscribers will see the Snooze action, but snooze attempts will be rejected by the API. Contact QPilot support to enable the feature flag for your site.', 'autoship' ) ); ?></small>
            </div>
        </div>
    <?php endif; ?>

    <div class="asc-form-group">
        <label class="asc-form-label"><?php echo esc_html( __( 'Enable Snooze', 'autoship' ) ); ?></label>
        <label for="autoship_snooze_enabled" style="display: flex; align-items: flex-start; gap: 0.5rem; margin-top: 0.5rem; font-weight: 400;">
            <input type="checkbox"
                   id="autoship_snooze_enabled"
                   name="autoship_snooze_enabled"
                   value="yes"
                <?php echo checked( 'yes', $autoship_settings['autoship_snooze_enabled'] ); ?>
                   autocomplete="false" style="margin-top: 0.2rem;"/>
            <span><?php echo esc_html( __( 'Display the Snooze action on the Native UI Scheduled Order view', 'autoship' ) ); ?></span>
        </label>
        <p class="asc-field-hint"><?php echo esc_html( __( 'When enabled, subscribers can pause their order with an automatic resume date.', 'autoship' ) ); ?></p>
    </div>

    <div class="autoship_snooze_extra_options<?php echo 'yes' === $autoship_settings['autoship_snooze_enabled'] ? '' : ' is-muted'; ?>">

        <div class="asc-form-group">
            <label class="asc-form-label" for="autoship_snooze_action_label"><?php echo esc_html( __( 'Action Label', 'autoship' ) ); ?></label>
            <input type="text"
                   id="autoship_snooze_action_label"
                   name="autoship_snooze_action_label"
                   value="<?php echo esc_attr( $autoship_settings['autoship_snooze_action_label'] ); ?>"
                   placeholder="Snooze"
                   style="max-width: 280px;"/>
            <p class="asc-field-hint"><?php echo wp_kses_post( __( 'The text shown to subscribers for this action. Default: <em>Snooze</em>.', 'autoship' ) ); ?></p>
        </div>

        <div class="asc-form-group">
            <label class="asc-form-label"><?php echo esc_html( __( 'Pause Action Behavior', 'autoship' ) ); ?></label>
            <div class="asc-radio-group">
                <label class="asc-radio-option">
                    <input type="radio"
                           name="autoship_snooze_pause_behavior"
                           id="autoship_snooze_pause_behavior_alongside"
                           value="alongside"
                            <?php echo checked( 'alongside', $autoship_settings['autoship_snooze_pause_behavior'] ); ?> />
                    <span>
                        <?php echo wp_kses_post( __( 'Show Snooze <strong>alongside</strong> Pause', 'autoship' ) ); ?>
                        <small style="display:block; color: var(--qmc-text-secondary, #64748b); font-weight: 400;"><?php echo esc_html( __( 'Both actions appear in the action row. Pause = indefinite. Snooze = auto-resume.', 'autoship' ) ); ?></small>
                    </span>
                </label>
                <label class="asc-radio-option">
                    <input type="radio"
                           name="autoship_snooze_pause_behavior"
                           id="autoship_snooze_pause_behavior_instead"
                           value="instead"
                            <?php echo checked( 'instead', $autoship_settings['autoship_snooze_pause_behavior'] ); ?> />
                    <span>
                        <?php echo wp_kses_post( __( 'Show Snooze <strong>instead of</strong> Pause', 'autoship' ) ); ?>
                        <small style="display:block; color: var(--qmc-text-secondary, #64748b); font-weight: 400;"><?php echo esc_html( __( 'Snooze replaces Pause entirely. Subscribers always get an auto-resume date when pausing.', 'autoship' ) ); ?></small>
                    </span>
                </label>
            </div>
        </div>

        <div class="asc-snooze-action-preview">
            <p class="asc-snooze-preview-label"><?php echo esc_html( __( 'ACTION ROW PREVIEW', 'autoship' ) ); ?></p>
            <div class="asc-snooze-preview-row">
                <span class="asc-snooze-preview-action"><?php echo esc_html( __( 'Edit Schedule', 'autoship' ) ); ?></span>
                <span class="asc-snooze-preview-sep">|</span>
                <span class="asc-snooze-preview-action asc-snooze-preview-pause"
                      style="display:<?php echo 'instead' === $autoship_settings['autoship_snooze_pause_behavior'] ? 'none' : 'inline'; ?>;"><?php echo esc_html( __( 'Pause', 'autoship' ) ); ?></span>
                <span class="asc-snooze-preview-sep asc-snooze-preview-pause-sep"
                      style="display:<?php echo 'instead' === $autoship_settings['autoship_snooze_pause_behavior'] ? 'none' : 'inline'; ?>;">|</span>
                <span class="asc-snooze-preview-action asc-snooze-preview-snooze"><?php echo esc_html( $autoship_settings['autoship_snooze_action_label'] ); ?></span>
                <span class="asc-snooze-preview-sep">|</span>
                <span class="asc-snooze-preview-action"><?php echo esc_html( __( 'Skip', 'autoship' ) ); ?></span>
                <span class="asc-snooze-preview-sep">|</span>
                <span class="asc-snooze-preview-action"><?php echo esc_html( __( 'Delete Scheduled Order', 'autoship' ) ); ?></span>
            </div>
        </div>

    </div>
</div>

<!-- ====================================================================
     SNOOZE MODULE 2: Snooze Frequency Source
     ==================================================================== -->
<div class="asc-settings-column">
    <h3>
        <i class="pi pi-calendar"></i>
        <?php echo esc_html( __( 'Snooze Frequency Source', 'autoship' ) ); ?>
        <span class="asc-badge asc-badge-new"><?php echo esc_html( __( 'NEW', 'autoship' ) ); ?></span>
    </h3>
    <p class="asc-card-description"><?php echo esc_html( __( 'Choose where the available snooze durations come from. Durations are shown to subscribers in the duration picker when they initiate a Snooze.', 'autoship' ) ); ?></p>

    <div class="asc-form-group">
        <label class="asc-form-label"><?php echo esc_html( __( 'Duration Source', 'autoship' ) ); ?></label>
        <div class="asc-radio-group">
            <label class="asc-radio-option">
                <input type="radio"
                       name="autoship_snooze_frequency_source"
                       id="autoship_snooze_frequency_source_qmc"
                       value="qmc"
                        <?php echo checked( 'qmc', $autoship_settings['autoship_snooze_frequency_source'] ); ?> />
                <span>
                    <?php echo wp_kses_post( __( 'Use <strong>QMC Site Snooze Frequencies</strong>', 'autoship' ) ); ?>
                    <small style="display:block; color: var(--qmc-text-secondary, #64748b); font-weight: 400;"><?php echo esc_html( __( 'Pulls the snooze durations configured in your connected QPilot site settings. Recommended — stays in sync automatically.', 'autoship' ) ); ?></small>
                </span>
            </label>
            <label class="asc-radio-option">
                <input type="radio"
                       name="autoship_snooze_frequency_source"
                       id="autoship_snooze_frequency_source_plugin"
                       value="plugin"
                        <?php echo checked( 'plugin', $autoship_settings['autoship_snooze_frequency_source'] ); ?> />
                <span>
                    <?php echo wp_kses_post( __( 'Use <strong>Plugin Default Frequencies</strong>', 'autoship' ) ); ?>
                    <small style="display:block; color: var(--qmc-text-secondary, #64748b); font-weight: 400;"><?php echo esc_html( __( 'Use the frequency list defined below. Falls back to this list if no QMC snooze frequencies are configured.', 'autoship' ) ); ?></small>
                </span>
            </label>
        </div>
    </div>

    <?php
    $qmc_site_id = get_option( 'autoship_site_id', '' );
    $is_qmc      = 'qmc' === $autoship_settings['autoship_snooze_frequency_source'];
    ?>

    <div class="asc-snooze-qmc-info" style="display:<?php echo $is_qmc ? 'block' : 'none'; ?>; margin-bottom: 0.75rem;">
        <?php if ( ! empty( $qmc_site_id ) ) : ?>
            <div class="asc-alert asc-alert-info">
                <i class="pi pi-link"></i>
                <div class="asc-alert-content">
                    <p style="margin: 0 0 0.5rem; font-weight: 600;">
                        <?php
                        // translators: %s is the QPilot Site ID.
                        echo esc_html( sprintf( __( 'Connected to QPilot site (ID %s)', 'autoship' ), $qmc_site_id ) );
                        ?>
                    </p>
                    <small><?php echo esc_html( __( "Snooze frequencies will be read from your QPilot site's Snooze settings at runtime. To manage these frequencies, visit your QPilot Site Settings → Snooze.", 'autoship' ) ); ?></small>
                    <p style="margin: 0.5rem 0 0; font-style: italic; font-size: 0.85em;">
                        <?php echo esc_html( __( 'If no snooze frequencies are configured in QMC, the plugin default list will be used as a fallback.', 'autoship' ) ); ?>
                    </p>
                </div>
            </div>
        <?php endif; ?>
    </div>

    <?php
    $plugin_frequencies = $autoship_settings['autoship_snooze_plugin_frequencies'];

    if ( ! is_array( $plugin_frequencies ) || empty( $plugin_frequencies ) ) {
        $plugin_frequencies = autoship_snooze_plugin_frequencies_default_init( null );
    }
    ?>

    <div class="asc-snooze-plugin-frequencies" style="display:<?php echo $is_qmc ? 'none' : 'block'; ?>; margin-bottom: 0.75rem;">
        <div class="asc-snooze-frequencies-panel">
            <p class="asc-snooze-frequencies-title"><?php echo esc_html( __( 'PLUGIN DEFAULT SNOOZE DURATIONS', 'autoship' ) ); ?></p>

            <div class="asc-snooze-frequencies-list">
                <?php foreach ( $plugin_frequencies as $index => $freq ) :
                    $type  = isset( $freq['type'] ) ? $freq['type'] : 'Weeks';
                    $every = isset( $freq['every'] ) ? (int) $freq['every'] : 1;
                    $label = isset( $freq['label'] ) ? $freq['label'] : '';
                    ?>
                    <div class="asc-snooze-frequency-row" data-index="<?php echo esc_attr( $index ); ?>">
                        <span class="asc-snooze-freq-field">
                            <label><?php echo esc_html( __( 'Type', 'autoship' ) ); ?></label>
                            <select name="autoship_snooze_plugin_frequencies[<?php echo esc_attr( $index ); ?>][type]" class="asc-snooze-freq-type">
                                <option value="Days" <?php selected( 'Days', $type ); ?>><?php echo esc_html( __( 'Days', 'autoship' ) ); ?></option>
                                <option value="Weeks" <?php selected( 'Weeks', $type ); ?>><?php echo esc_html( __( 'Weeks', 'autoship' ) ); ?></option>
                                <option value="Months" <?php selected( 'Months', $type ); ?>><?php echo esc_html( __( 'Months', 'autoship' ) ); ?></option>
                            </select>
                        </span>
                        <span class="asc-snooze-freq-field">
                            <label><?php echo esc_html( __( 'Every', 'autoship' ) ); ?></label>
                            <input type="number"
                                   name="autoship_snooze_plugin_frequencies[<?php echo esc_attr( $index ); ?>][every]"
                                   class="asc-snooze-freq-every"
                                   value="<?php echo esc_attr( $every ); ?>"
                                   min="1"
                                   step="1"/>
                        </span>
                        <span class="asc-snooze-freq-field asc-snooze-freq-label-field">
                            <label><?php echo esc_html( __( 'Label', 'autoship' ) ); ?></label>
                            <input type="text"
                                   name="autoship_snooze_plugin_frequencies[<?php echo esc_attr( $index ); ?>][label]"
                                   class="asc-snooze-freq-label"
                                   value="<?php echo esc_attr( $label ); ?>"
                                   placeholder="<?php echo esc_attr( __( 'e.g. 1 week', 'autoship' ) ); ?>"/>
                        </span>
                        <button type="button" class="asc-snooze-freq-remove" aria-label="<?php echo esc_attr( __( 'Remove duration', 'autoship' ) ); ?>">×</button>
                    </div>
                <?php endforeach; ?>
            </div>

            <button type="button" class="asc-snooze-freq-add">
                + <?php echo esc_html( __( 'Add Another Duration', 'autoship' ) ); ?>
            </button>

            <div class="asc-snooze-frequencies-note">
                <strong><?php echo esc_html( __( 'Note:', 'autoship' ) ); ?></strong>
                <?php echo esc_html( __( 'These durations are also used as a fallback when the QMC source is selected but no snooze frequencies are configured in your connected QPilot site.', 'autoship' ) ); ?>
            </div>
        </div>
    </div>

    <div class="asc-form-group">
        <label class="asc-form-label"><?php echo esc_html( __( 'Allow Custom Resume Date', 'autoship' ) ); ?></label>
        <label for="autoship_snooze_allow_custom_date" style="display: flex; align-items: flex-start; gap: 0.5rem; margin-top: 0.5rem; font-weight: 400;">
            <input type="checkbox"
                   id="autoship_snooze_allow_custom_date"
                   name="autoship_snooze_allow_custom_date"
                   value="yes"
                <?php echo checked( 'yes', $autoship_settings['autoship_snooze_allow_custom_date'] ); ?>
                   autocomplete="false" style="margin-top: 0.2rem;"/>
            <span><?php echo wp_kses_post( __( 'Include a "Custom date…" option in the duration picker', 'autoship' ) ); ?></span>
        </label>
        <p class="asc-field-hint"><?php echo esc_html( __( 'Subscribers can pick any future date as their resume date.', 'autoship' ) ); ?></p>
    </div>

    <div class="asc-form-group">
        <label class="asc-form-label" for="autoship_snooze_max_duration_days"><?php echo esc_html( __( 'Maximum Snooze Duration (days)', 'autoship' ) ); ?></label>
        <input type="number"
               id="autoship_snooze_max_duration_days"
               name="autoship_snooze_max_duration_days"
               value="<?php echo esc_attr( $autoship_settings['autoship_snooze_max_duration_days'] ); ?>"
               min="0"
               step="1"
               placeholder="180"
               style="max-width: 160px;"/>
        <p class="asc-field-hint"><?php echo wp_kses_post( __( 'Prevents subscribers from snoozing beyond this many days. Leave blank for no limit. Default: <em>180 days</em>.', 'autoship' ) ); ?></p>
    </div>
</div>
