<?php

namespace GriffinForms\Admin\Ajax;

class UninstallFeedback extends \GriffinForms\Admin\Ajax\Format
{
    protected $response = [];

    public function __construct()
    {
        $this->setAll();
        add_action('wp_ajax_griffinforms_uninstall_feedback', [$this, 'sendFeedback']);
    }

    private function getReasons(): array
    {
        return [
            'missing_feature' => __('Missing a feature I need', 'griffinforms-form-builder'),
            'found_bug'       => __('I encountered a bug or error', 'griffinforms-form-builder'),
            'too_complex'     => __('Too hard to use', 'griffinforms-form-builder'),
            'performance'     => __('Performance or speed issues', 'griffinforms-form-builder'),
            'switched_plugin' => __('I switched to another plugin', 'griffinforms-form-builder'),
            'temporary'       => __('Just temporary / debugging', 'griffinforms-form-builder'),
            'pricing'         => __('Pricing or licensing', 'griffinforms-form-builder'),
            'other'           => __('Other (please specify)', 'griffinforms-form-builder'),
        ];
    }

    public function sendFeedback(): void
    {
        if (!current_user_can('activate_plugins')) {
            $this->returnError(__('You do not have permission to perform this action.', 'griffinforms-form-builder'));
        }

        $this->checkNonce('griffinforms_uninstall_feedback');
        $this->checkPostData(['reason']);

        $reason = sanitize_key(wp_unslash($_POST['reason']));
        $details = isset($_POST['details']) ? sanitize_textarea_field(wp_unslash($_POST['details'])) : '';
        $allow_contact = !empty($_POST['allow_contact']);

        $reasons = $this->getReasons();
        $reason_label = $reasons[$reason] ?? $reason;

        $current_user = wp_get_current_user();
        $user_email = '';
        if ($allow_contact && $current_user && !empty($current_user->user_email)) {
            $user_email = $current_user->user_email;
        }

        $content  = '<div style="font:14px/1.5 -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica,Arial,sans-serif;color:#1d2327;">';
        $content .= '<p><strong>' . esc_html__('Uninstall feedback received', 'griffinforms-form-builder') . '</strong></p>';
        $content .= '<p><strong>' . esc_html__('Reason', 'griffinforms-form-builder') . ':</strong> ' . esc_html($reason_label) . '</p>';
        if (!empty($details)) {
            $content .= '<p><strong>' . esc_html__('Details', 'griffinforms-form-builder') . ':</strong><br>' . nl2br(esc_html($details)) . '</p>';
        }
        $content .= '<p><strong>' . esc_html__('Contact permission', 'griffinforms-form-builder') . ':</strong> ' . ($allow_contact ? esc_html__('Yes', 'griffinforms-form-builder') : esc_html__('No', 'griffinforms-form-builder')) . '</p>';
        if (!empty($user_email)) {
            $content .= '<p><strong>' . esc_html__('User email', 'griffinforms-form-builder') . ':</strong> ' . esc_html($user_email) . '</p>';
        }
        $content .= '<hr style="border:none;border-top:1px solid #dcdcde;margin:16px 0;" />';
        $content .= '<p><strong>' . esc_html__('Site', 'griffinforms-form-builder') . ':</strong> ' . esc_html(get_bloginfo('name')) . '</p>';
        $content .= '<p><strong>' . esc_html__('Site URL', 'griffinforms-form-builder') . ':</strong> ' . esc_html(home_url()) . '</p>';
        $content .= '<p><strong>' . esc_html__('Plugin version', 'griffinforms-form-builder') . ':</strong> ' . esc_html($this->config::VERSION) . '</p>';
        $content .= '<p><strong>' . esc_html__('WordPress version', 'griffinforms-form-builder') . ':</strong> ' . esc_html(get_bloginfo('version')) . '</p>';
        $content .= '<p><strong>' . esc_html__('PHP version', 'griffinforms-form-builder') . ':</strong> ' . esc_html(PHP_VERSION) . '</p>';
        $content .= '</div>';

        $subject = sprintf(
            __('GriffinForms deactivation feedback (%s)', 'griffinforms-form-builder'),
            get_bloginfo('name')
        );

        $mailer = new \GriffinForms\Includes\Mailer\MailHandler();
        $mailer->setLogContext('system', 0);
        $mailer->setEmailSubject($subject);
        $mailer->setEmailContent($content);
        $mailer->setEmailRecipient($this->config::SUPPORT_EMAIL);
        $result = $mailer->sendMail();

        if (is_wp_error($result)) {
            $this->returnError(__('Feedback could not be sent. Deactivation will continue.', 'griffinforms-form-builder'));
        }

        $this->returnSuccess();
    }
}
