<?php

namespace Licenzo\Http\Controller\Api\V1;

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

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

class EmailNotificationSettingController
{
    /**
     * Get notification settings
     *
     * @return array
     */
    public function get()
    {
        return [
            'success' => true,
            'data'    => EmailNotificationSetting::all()
        ];
    }

    /**
     * Update notification settings
     *
     * @param Request $request
     * @return array
     */
    public function update(Request $request)
    {
        $data = $request->getSafe([
            'customer_welcome.subject'    => 'sanitize_text_field',
            'customer_welcome.body'       => 'wp_kses_post',
            'customer_welcome.enabled'    => 'sanitize_text_field',

            'license_activation.subject'  => 'sanitize_text_field',
            'license_activation.body'     => 'wp_kses_post',
            'license_activation.enabled'  => 'sanitize_text_field',

            'license_expiration.subject'  => 'sanitize_text_field',
            'license_expiration.body'     => 'wp_kses_post',
            'license_expiration.enabled'  => 'sanitize_text_field',

            'maximum_activations.subject' => 'sanitize_text_field',
            'maximum_activations.body'    => 'wp_kses_post',
            'maximum_activations.enabled' => 'sanitize_text_field',
        ]);


        try {
            EmailNotificationSetting::update([
                'customer_welcome'    => [
                    'subject' => $data['customer_welcome.subject'],
                    'body'    => $data['customer_welcome.body'],
                    'enabled' => $data['customer_welcome.enabled'],
                ],
                'license_activation'  => [
                    'subject' => $data['license_activation.subject'],
                    'body'    => $data['license_activation.body'],
                    'enabled' => $data['license_activation.enabled'],
                ],
                'license_expiration'  => [
                    'subject' => $data['license_expiration.subject'],
                    'body'    => $data['license_expiration.body'],
                    'enabled' => $data['license_expiration.enabled'],
                ],
                'maximum_activations' => [
                    'subject' => $data['maximum_activations.subject'],
                    'body'    => $data['maximum_activations.body'],
                    'enabled' => $data['maximum_activations.enabled'],
                ],
            ]);
            return [
                'success' => true,
                'message' => __('Notification settings updated successfully', 'licenzo'),
                'data'    => EmailNotificationSetting::all()
            ];
        } catch (\Exception $e) {
            return [
                'success' => false,
                'message' => (defined('WP_DEBUG') && WP_DEBUG) ? 'Failed to update notification settings: ' . $e->getMessage() : __('An error occurred', 'licenzo')
            ];
        }
    }
}
