<?php

namespace GriffinForms\Admin\Html\Pages\Settings;

class FileStorage extends \GriffinForms\Admin\Html\Pages\Settings\Format
{
    /**
     * Render all options in this settings group in order.
     */
    protected function settingsGroupOptions()
    {
        // Storage behavior
        $this->getOptionHtml('files_default_storage_mode');
        $this->getOptionHtml('files_delete_on_submission_delete');

        // Retention & cleanup
        $this->getOptionHtml('files_temp_retention_hours');
        $this->getOptionHtml('files_cleanup_recurrence');

        // Limits & alerts
        $this->getOptionHtml('files_zip_max_size_mb');
        $this->getOptionHtml('files_storage_alert_threshold_mb');
    }

    /**
     * files_default_storage_mode
     * Select where to store uploaded files by default (private vs media library).
     */
    protected function getFilesDefaultStorageModeHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('files_default_storage_mode');
        $current = isset($this->option['current_value']) ? (string) $this->option['current_value'] : 'private';
        $input_id = $input_id !== '' ? $input_id : 'griffinforms-settings-files-default-storage-mode';
        ?>
        <fieldset class="form-group">
            <select id="<?php echo esc_attr($input_id); ?>" name="files_default_storage_mode" class="form-control">
                <option value="private" <?php selected($current, 'private'); ?>>
                    <?php echo esc_html($this->lang->getText('filesDefaultStorageModePrivate')); ?>
                </option>
                <option value="media_library" <?php selected($current, 'media_library'); ?>>
                    <?php echo esc_html($this->lang->getText('filesDefaultStorageModeMediaLibrary')); ?>
                </option>
            </select>
            <?php echo $this->getOptionDescription('files_default_storage_mode'); ?>
            <?php $this->displayOptionHistory(); ?>
        </fieldset>
        <?php
    }

    /**
     * files_delete_on_submission_delete
     * Checkbox toggle: delete attachments when a submission is deleted.
     */
    protected function getFilesDeleteOnSubmissionDeleteHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('files_delete_on_submission_delete');
        $current = !empty($this->option['current_value']) ? (int) $this->option['current_value'] : 0;
        $input_id = $input_id !== '' ? $input_id : 'griffinforms-settings-files-delete-on-submission-delete';
        ?>
        <fieldset class="form-group">
            <input type="checkbox"
                   id="<?php echo esc_attr($input_id); ?>"
                   name="files_delete_on_submission_delete"
                   value="1" <?php checked($current, 1); ?> />
            <?php echo $this->getOptionDescription('files_delete_on_submission_delete'); ?>
            <?php $this->displayOptionHistory(); ?>
        </fieldset>
        <?php
    }

    /**
     * files_temp_retention_hours
     * Number input: how long temp files are kept before cleanup.
     */
    protected function getFilesTempRetentionHoursHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('files_temp_retention_hours');
        $current = isset($this->option['current_value']) && $this->option['current_value'] !== '' ? (int) $this->option['current_value'] : 48;
        $input_id = $input_id !== '' ? $input_id : 'griffinforms-settings-files-temp-retention-hours';
        ?>
        <fieldset class="form-group">
            <input type="number" min="1" step="1"
                   id="<?php echo esc_attr($input_id); ?>"
                   name="files_temp_retention_hours"
                   class="form-control"
                   value="<?php echo esc_attr($current); ?>" />
            <?php echo $this->getOptionDescription('files_temp_retention_hours'); ?>
            <?php $this->displayOptionHistory(); ?>
        </fieldset>
        <?php
    }

    /**
     * files_cleanup_recurrence
     * Cron frequency for temp cleanup.
     */
    protected function getFilesCleanupRecurrenceHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('files_cleanup_recurrence');
        $current = isset($this->option['current_value']) ? (string) $this->option['current_value'] : 'hourly';
        $input_id = $input_id !== '' ? $input_id : 'griffinforms-settings-files-cleanup-recurrence';
        ?>
        <fieldset class="form-group">
            <select id="<?php echo esc_attr($input_id); ?>" name="files_cleanup_recurrence" class="form-control">
                <option value="hourly" <?php selected($current, 'hourly'); ?>>
                    <?php echo esc_html($this->lang->getText('recurrenceHourly')); ?>
                </option>
                <option value="twicedaily" <?php selected($current, 'twicedaily'); ?>>
                    <?php echo esc_html($this->lang->getText('recurrenceTwiceDaily')); ?>
                </option>
                <option value="daily" <?php selected($current, 'daily'); ?>>
                    <?php echo esc_html($this->lang->getText('recurrenceDaily')); ?>
                </option>
            </select>
            <?php echo $this->getOptionDescription('files_cleanup_recurrence'); ?>
            <?php $this->displayOptionHistory(); ?>
        </fieldset>
        <?php
    }

    /**
     * files_zip_max_size_mb
     * Number input: max size of a generated zip to download all attachments.
     */
    protected function getFilesZipMaxSizeMbHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('files_zip_max_size_mb');
        $current = isset($this->option['current_value']) && $this->option['current_value'] !== '' ? (float) $this->option['current_value'] : 256.0;
        $input_id = $input_id !== '' ? $input_id : 'griffinforms-settings-files-zip-max-size-mb';
        ?>
        <fieldset class="form-group">
            <input type="number" min="1" step="0.1"
                   id="<?php echo esc_attr($input_id); ?>"
                   name="files_zip_max_size_mb"
                   class="form-control"
                   value="<?php echo esc_attr($current); ?>" />
            <?php echo $this->getOptionDescription('files_zip_max_size_mb'); ?>
            <?php $this->displayOptionHistory(); ?>
        </fieldset>
        <?php
    }

    /**
     * files_storage_alert_threshold_mb
     * Number input: show warning/alert when total storage exceeds threshold.
     */
    protected function getFilesStorageAlertThresholdMbHtml($input_id = '')
    {
        $this->option = $this->settings->getOptionHistory('files_storage_alert_threshold_mb');
        $current = isset($this->option['current_value']) && $this->option['current_value'] !== '' ? (float) $this->option['current_value'] : 0;
        $input_id = $input_id !== '' ? $input_id : 'griffinforms-settings-files-storage-alert-threshold-mb';
        ?>
        <fieldset class="form-group">
            <input type="number" min="0" step="0.1"
                   id="<?php echo esc_attr($input_id); ?>"
                   name="files_storage_alert_threshold_mb"
                   class="form-control"
                   value="<?php echo esc_attr($current); ?>" />
            <?php echo $this->getOptionDescription('files_storage_alert_threshold_mb'); ?>
            <?php $this->displayOptionHistory(); ?>
        </fieldset>
        <?php
    }
}