<?php

/**
 * Software Distribution Settings Schema
 *
 * @package WooNooW\Modules
 */

namespace WooNooW\Modules;

if (!defined('ABSPATH')) exit;

use WooNooW\Core\ModuleRegistry;

class SoftwareSettings
{
    private static $option_key = 'woonoow_module_software_settings';

    /**
     * Initialize settings
     */
    public static function init()
    {
        // Register module with ModuleRegistry
        add_filter('woonoow/modules/registry', [__CLASS__, 'register_module']);
    }

    /**
     * Register the software module
     */
    public static function register_module($modules)
    {
        $modules['software'] = [
            'id' => 'software',
            'name' => __('Software Distribution', 'woonoow'),
            'description' => __('Sell and distribute software with version tracking, changelogs, and automatic update checking. Works with any software type.', 'woonoow'),
            'icon' => 'Package',
            'category' => 'sales',
            'requires' => ['licensing'], // Depends on licensing module
            'settings' => self::get_settings_schema(),
        ];

        return $modules;
    }

    /**
     * Get settings schema
     */
    public static function get_settings_schema()
    {
        return [
            [
                'id' => 'rate_limit',
                'type' => 'number',
                'label' => __('API Rate Limit', 'woonoow'),
                'description' => __('Maximum update check requests per minute per license', 'woonoow'),
                'default' => 10,
                'min' => 1,
                'max' => 100,
            ],
            [
                'id' => 'token_expiry',
                'type' => 'number',
                'label' => __('Download Token Expiry', 'woonoow'),
                'description' => __('Minutes until download token expires (default: 5)', 'woonoow'),
                'default' => 5,
                'min' => 1,
                'max' => 60,
            ],
            [
                'id' => 'cache_ttl',
                'type' => 'number',
                'label' => __('Client Cache TTL', 'woonoow'),
                'description' => __('Hours to cache update check results on client (default: 12)', 'woonoow'),
                'default' => 12,
                'min' => 1,
                'max' => 168,
            ],
        ];
    }

    /**
     * Get current settings
     */
    public static function get_settings()
    {
        $defaults = [
            'rate_limit' => 10,
            'token_expiry' => 5,
            'cache_ttl' => 12,
        ];

        $settings = get_option(self::$option_key, []);

        return wp_parse_args($settings, $defaults);
    }

    /**
     * Save settings
     */
    public static function save_settings($settings)
    {
        $sanitized = [
            'rate_limit' => absint($settings['rate_limit'] ?? 10),
            'token_expiry' => absint($settings['token_expiry'] ?? 5),
            'cache_ttl' => absint($settings['cache_ttl'] ?? 12),
        ];

        update_option(self::$option_key, $sanitized);

        return $sanitized;
    }
}
