<?php
/**
 * General Settings Controller V1
 *
 * @package Licenzo\Http\Controller\Api\V1
 */

namespace Licenzo\Http\Controller\Api\V1;

use Licenzo\Helper\Settings;
use Licenzo\App;
use Licenzo\Framework\Http\Request;
use Licenzo\Http\Traits\HandlesErrors;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}
class GeneralSettingsController
{
    use HandlesErrors;
    /**
     * Get general settings
     *
     * @return array
     */
    public function get()
    {
        try {
            return $this->successResponse(
                Settings::getGeneralSettings(),
                'General settings retrieved successfully'
            );

        } catch (\Exception $e) {
            return $this->handleError($e, 'Failed to retrieve general settings');
        }
    }

    /**
     * Update general settings
     *
     * @param Request $request
     * @return array
     */
    public function update(Request $request)
    {
        $data = $request->getSafe([
            'commerce_provider'          => 'sanitize_text_field',
            'enable_woo_my_account'      => 'sanitize_text_field',
            'default_status'             => 'sanitize_text_field',
            'automatic_revoke_on_expiry' => 'sanitize_text_field',
            'days_after_expire'          => 'intval',
        ]);

        try {
            $settings = Settings::updateGeneralSettings($data);

            return $this->successResponse(
                $settings,
                'General settings updated successfully'
            );

        } catch (\Exception $e) {
            return $this->handleError($e, 'Failed to update general settings');
        }
    }
}
