<?php

namespace GriffinForms\Admin\Js\Local;

final class FileStorage extends \GriffinForms\Admin\Js\Local\Settings
{
    /**
     * Build the JS that collects options and pushes them to the AJAX payload.
     */
    protected function getOptionsDataJs()
    {
        $js = '
        function getOptionsData() {
            getDefaultStorageModeOption();
            getDeleteOnSubmissionDeleteOption();
            getTempRetentionHoursOption();
            getCleanupRecurrenceOption();
            getZipMaxSizeMbOption();
            getStorageAlertThresholdMbOption();
        }
        ';

        $js .= $this->getDefaultStorageModeOptionJs();
        $js .= $this->getDeleteOnSubmissionDeleteOptionJs();
        $js .= $this->getTempRetentionHoursOptionJs();
        $js .= $this->getCleanupRecurrenceOptionJs();
        $js .= $this->getZipMaxSizeMbOptionJs();
        $js .= $this->getStorageAlertThresholdMbOptionJs();

        return $js;
    }

    /**
     * Collect files_default_storage_mode
     */
    protected function getDefaultStorageModeOptionJs()
    {
        return '
        function getDefaultStorageModeOption() {
            var el = document.getElementById("griffinforms-settings-filestorage-filesdefaultstoragemode");
            if (!el) { return; }
            var val = el.value === "media_library" ? "media_library" : "private";
            optionsData["files_default_storage_mode"] = val;
        }
        ';
    }

    /**
     * Collect files_delete_on_submission_delete
     */
    protected function getDeleteOnSubmissionDeleteOptionJs()
    {
        return '
        function getDeleteOnSubmissionDeleteOption() {
            var el = document.getElementById("griffinforms-settings-filestorage-filesdeleteonsubmissiondelete");
            if (!el) { return; }
            var val = !!el.checked ? 1 : 0;
            optionsData["files_delete_on_submission_delete"] = val;
        }
        ';
    }

    /**
     * Collect files_temp_retention_hours with numeric validation (int >= 1)
     */
    protected function getTempRetentionHoursOptionJs()
    {
        return '
        function getTempRetentionHoursOption() {
            var el = document.getElementById("griffinforms-settings-filestorage-filestempretentionhours");
            if (!el) { return; }
            var raw = (el.value || "").trim();
            if (raw === "") { optionsErrors["files_temp_retention_hours"] = "' . esc_js($this->lang->getText('files_temp_retention_hours_required', 'error')) . '"; return; }
            if (!/^\d+$/.test(raw)) { optionsErrors["files_temp_retention_hours"] = "' . esc_js($this->lang->getText('files_temp_retention_hours_integer', 'error')) . '"; return; }
            var num = parseInt(raw, 10);
            if (num < 1) { optionsErrors["files_temp_retention_hours"] = "' . esc_js($this->lang->getText('files_temp_retention_hours_min', 'error')) . '"; return; }
            optionsData["files_temp_retention_hours"] = num;
        }
        ';
    }

    /**
     * Collect files_cleanup_recurrence
     */
    protected function getCleanupRecurrenceOptionJs()
    {
        return '
        function getCleanupRecurrenceOption() {
            var el = document.getElementById("griffinforms-settings-filestorage-filescleanuprecurrence");
            if (!el) { return; }
            var allowed = { hourly:1, twicedaily:1, daily:1 };
            var val = (el.value || "hourly");
            if (!allowed[val]) { val = "hourly"; }
            optionsData["files_cleanup_recurrence"] = val;
        }
        ';
    }

    /**
     * Collect files_zip_max_size_mb with numeric validation (float > 0)
     */
    protected function getZipMaxSizeMbOptionJs()
    {
        return '
        function getZipMaxSizeMbOption() {
            var el = document.getElementById("griffinforms-settings-filestorage-fileszipmaxsizemb");
            if (!el) { return; }
            var raw = (el.value || "").trim();
            if (raw === "") { optionsErrors["files_zip_max_size_mb"] = "' . esc_js($this->lang->getText('files_zip_max_size_required', 'error')) . '"; return; }
            if (!/^\d+(?:\.\d+)?$/.test(raw)) { optionsErrors["files_zip_max_size_mb"] = "' . esc_js($this->lang->getText('files_zip_max_size_number', 'error')) . '"; return; }
            var num = parseFloat(raw);
            if (!(num > 0)) { optionsErrors["files_zip_max_size_mb"] = "' . esc_js($this->lang->getText('files_zip_max_size_min', 'error')) . '"; return; }
            optionsData["files_zip_max_size_mb"] = num;
        }
        ';
    }

    /**
     * Collect files_storage_alert_threshold_mb with numeric validation (float >= 0)
     */
    protected function getStorageAlertThresholdMbOptionJs()
    {
        return '
        function getStorageAlertThresholdMbOption() {
            var el = document.getElementById("griffinforms-settings-filestorage-filesstoragealertthresholdmb");
            if (!el) { return; }
            var raw = (el.value || "").trim();
            if (raw === "") { optionsData["files_storage_alert_threshold_mb"] = 0; return; }
            if (!/^\d+(?:\.\d+)?$/.test(raw)) { optionsErrors["files_storage_alert_threshold_mb"] = "' . esc_js($this->lang->getText('files_storage_alert_threshold_number_or_blank', 'error')) . '"; return; }
            var num = parseFloat(raw);
            if (num < 0) { optionsErrors["files_storage_alert_threshold_mb"] = "' . esc_js($this->lang->getText('files_storage_alert_threshold_min', 'error')) . '"; return; }
            optionsData["files_storage_alert_threshold_mb"] = num;
        }
        ';
    }
}