<?php

namespace GriffinForms\Admin\Secure;

class FileStorage extends \GriffinForms\Admin\Secure\Settings
{
    /**
     * files_default_storage_mode → 'private' | 'media_library'
     */
    protected function secureFilesDefaultStorageMode($value)
    {
        $allowed = array('private', 'media_library');
        $val = is_string($value) ? strtolower(trim($value)) : '';
        if (!in_array($val, $allowed, true)) {
            $this->errors['files_default_storage_mode'] = $this->lang->getText('filesInvalidStorageModeError');
            return 'private';
        }
        return $val;
    }

    /**
     * files_delete_on_submission_delete → bool (0/1)
     */
    protected function secureFilesDeleteOnSubmissionDelete($value)
    {
        return $this->secureToggle($value);
    }

    /**
     * files_temp_retention_hours → int >= 1
     */
    protected function secureFilesTempRetentionHours($value)
    {
        $num = is_numeric($value) ? (int) $value : 0;
        if ($num < 1) {
            $this->errors['files_temp_retention_hours'] = $this->lang->getText('filesInvalidTempRetentionHoursError');
            return 48; // sensible default
        }
        return $num;
    }

    /**
     * files_cleanup_recurrence → 'hourly' | 'twicedaily' | 'daily'
     */
    protected function secureFilesCleanupRecurrence($value)
    {
        $allowed = array('hourly', 'twicedaily', 'daily');
        $val = is_string($value) ? strtolower(trim($value)) : 'hourly';
        if (!in_array($val, $allowed, true)) {
            $this->errors['files_cleanup_recurrence'] = $this->lang->getText('filesInvalidCleanupRecurrenceError');
            return 'hourly';
        }
        return $val;
    }

    /**
     * files_zip_max_size_mb → float > 0
     */
    protected function secureFilesZipMaxSizeMb($value)
    {
        $num = is_numeric($value) ? (float) $value : 0.0;
        if (!($num > 0)) {
            $this->errors['files_zip_max_size_mb'] = $this->lang->getText('filesInvalidZipMaxSizeError');
            return 256.0; // sensible default MB
        }
        return $num;
    }

    /**
     * files_storage_alert_threshold_mb → float >= 0 (0 disables)
     */
    protected function secureFilesStorageAlertThresholdMb($value)
    {
        if ($value === '' || $value === null) {
            return 0.0; // blank means disabled
        }
        $num = is_numeric($value) ? (float) $value : -1.0;
        if ($num < 0) {
            $this->errors['files_storage_alert_threshold_mb'] = $this->lang->getText('filesInvalidStorageAlertThresholdError');
            return 0.0;
        }
        return $num;
    }
}