<?php
/**
 * Custom Plugin Upgrader Skin
 *
 * Captures all feedback and error messages from Plugin_Upgrader,
 * especially license-related errors from premium plugins.
 *
 * @package FleekDash\Controllers\Plugins
 */

namespace FleekDash\Controllers\Plugins;

require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

class PluginUpgraderSkin extends \Automatic_Upgrader_Skin
{
    /**
     * @var array Collected feedback messages (custom, not parent's $messages)
     */
    protected $captured_messages = [];

    /**
     * @var array Collected error messages
     */
    protected $captured_errors = [];

    /**
     * @var bool Whether any errors occurred
     */
    protected $captured_had_errors = false;

    /**
     * Capture feedback messages
     */
    public function feedback($string, ...$args)
    {
        // Call parent to maintain compatibility
        parent::feedback($string, ...$args);

        if (!empty($string)) {
            $message = vsprintf($string, $args);
            $this->captured_messages[] = [
                'message' => $message,
                'type' => 'info',
                'timestamp' => time(),
            ];

            // Check if message indicates error
            $lower = strtolower($message);
            if (
                strpos($lower, 'error') !== false ||
                strpos($lower, 'failed') !== false ||
                strpos($lower, 'could not') !== false ||
                strpos($lower, 'unable to') !== false ||
                strpos($lower, 'license') !== false ||
                strpos($lower, 'activation') !== false ||
                strpos($lower, 'premium') !== false
            ) {
                $this->captured_had_errors = true;
                $this->captured_errors[] = [
                    'message' => $message,
                    'type' => 'error',
                    'timestamp' => time(),
                ];
            }
        }
    }

    /**
     * Capture error messages
     */
    public function error($errors)
    {
        // Call parent to maintain compatibility
        parent::error($errors);

        $this->captured_had_errors = true;

        if (is_string($errors)) {
            $this->captured_errors[] = [
                'message' => $errors,
                'type' => 'error',
                'timestamp' => time(),
            ];
        } elseif (is_wp_error($errors)) {
            foreach ($errors->get_error_messages() as $error) {
                $this->captured_errors[] = [
                    'message' => $error,
                    'type' => 'error',
                    'error_code' => $errors->get_error_code(),
                    'error_data' => $errors->get_error_data(),
                    'timestamp' => time(),
                ];
            }
        } elseif (is_array($errors)) {
            foreach ($errors as $error) {
                $this->captured_errors[] = [
                    'message' => is_string($error) ? $error : print_r($error, true),
                    'type' => 'error',
                    'timestamp' => time(),
                ];
            }
        }
    }

    /**
     * Get all collected messages
     */
    public function get_messages(): array
    {
        return $this->captured_messages;
    }

    /**
     * Get all collected errors
     */
    public function get_errors(): array
    {
        return $this->captured_errors;
    }

    /**
     * Get the first error message (most relevant)
     */
    public function get_first_error(): ?string
    {
        if (empty($this->captured_errors)) {
            return null;
        }
        return $this->captured_errors[0]['message'] ?? null;
    }

    /**
     * Get all error messages combined
     */
    public function get_all_error_messages(): string
    {
        if (empty($this->captured_errors)) {
            return '';
        }
        return implode(' ', array_column($this->captured_errors, 'message'));
    }

    /**
     * Check if any errors occurred
     */
    public function has_errors(): bool
    {
        return $this->captured_had_errors;
    }

    /**
     * Check if errors are license-related
     */
    public function is_license_error(): bool
    {
        $all_errors = $this->get_all_error_messages();
        $lower = strtolower($all_errors);

        $license_keywords = [
            'license',
            'licence',
            'activation',
            'premium',
            'subscription',
            'key',
            'invalid license',
            'license required',
            'license not found',
            'license expired',
            'unauthorized',
            'not activated',
            'activation required',
        ];

        foreach ($license_keywords as $keyword) {
            if (strpos($lower, $keyword) !== false) {
                return true;
            }
        }

        return false;
    }
}
