<?php

namespace Licenzo\Http\Controller\Api\V1;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

use Licenzo\Framework\Http\Request;
use Licenzo\Helper\Settings;

class LicenseGeneratorSettingsController
{
    /**
     * Get license generator settings
     *
     * @return array
     */
    public function get()
    {
        return [
            'success' => true,
            'data' => Settings::getLicenseSettings(),
        ];
    }

    /**
     * Update license generator settings
     *
     * @param Request $request
     * @return array
     */
    public function update(Request $request)
    {
        $data = $request->getSafe([
            'character_map' => 'sanitize_text_field',
            'number_of_chunks' => 'intval',
            'chunk_length' => 'intval',
            'separator' => 'sanitize_text_field',
            'prefix' => 'sanitize_text_field',
            'suffix' => 'sanitize_text_field',
        ]);

        try {
            $updatedSettings = Settings::updateLicenseSettings($data);

            return [
                'success' => true,
                'message' => __('License generator settings updated successfully', 'licenzo'),
                'data' => $updatedSettings
            ];
        } catch (\Exception $e) {
            return [
                'success' => false,
                'message' => (defined('WP_DEBUG') && WP_DEBUG) ? 'Failed to update license generator settings: ' . $e->getMessage() : __('An error occurred', 'licenzo')
            ];
        }
    }
}
