<?php

declare(strict_types=1);

namespace Duplicator\Models\ActivityLog;

use Duplicator\Controllers\ToolsPageController;
use Duplicator\Core\Views\TplMng;
use Duplicator\Models\RecommendedFix;
use Duplicator\Models\SystemGlobalEntity;
use Duplicator\Package\AbstractPackage;
use Duplicator\Utils\Logging\DupLog;

/**
 * Shared error-context capture and rendering for activity-log events.
 */
trait TraitLogEventErrorContext
{
    /**
     * Read the last $maxLines of the package log, with $sentinel as fallback.
     *
     * @param AbstractPackage $package  Package
     * @param int             $maxLines Max trailing lines to capture
     * @param string          $sentinel Single-line fallback when the log is unavailable
     *
     * @return string[]
     */
    private function captureLogTail(AbstractPackage $package, int $maxLines, string $sentinel): array
    {
        $lines = DupLog::getLogContext($package->getNameHash(), $maxLines);
        return empty($lines) ? [$sentinel] : $lines;
    }

    /**
     * Snapshot SystemGlobalEntity::recommended_fixes as plain arrays.
     *
     * @return array<int, array{error_text:string, fix_text:string, type:int}>
     */
    private function captureQuickFixes(): array
    {
        $fixes = SystemGlobalEntity::getInstance()->recommended_fixes;
        return array_map(
            static fn(RecommendedFix $fix): array => [
                'error_text' => (string) $fix->error_text,
                'fix_text'   => (string) $fix->parameter1,
                'type'       => (int) $fix->recommended_fix_type,
            ],
            $fixes
        );
    }

    /**
     * Render the Error Context block via the shared error_log_context partial.
     *
     * @param string[] $logLines    Lines to display
     * @param string   $logFileName Filename used to build the full-log link
     * @param string   $footerText  Pre-built footer string
     *
     * @return void
     */
    private function renderLogContext(array $logLines, string $logFileName, string $footerText): void
    {
        TplMng::getInstance()->render(
            'admin_pages/activity_log/parts/error_log_context',
            [
                'logLines'   => $logLines,
                'logUrl'     => ToolsPageController::getLogViewerURL($logFileName, false),
                'footerText' => $footerText,
            ]
        );
    }

    /**
     * Render the Reason block. TYPE_FIX body is suppressed since its admin-notice button does not exist here.
     *
     * @param array<int, array{error_text:string, fix_text:string, type:int}> $quickFixes Captured quick fixes
     *
     * @return void
     */
    private function renderQuickFixesReason(array $quickFixes): void
    {
        if (empty($quickFixes)) {
            return;
        }
        ?>
        <hr>
        <div class="dup-log-type-wrapper">
            <strong><?php esc_html_e('Reason:', 'duplicator-pro'); ?></strong>
        </div>
        <?php foreach ($quickFixes as $fix) : ?>
            <div class="dup-log-type-wrapper">
                <strong><?php echo esc_html((string) ($fix['error_text'] ?? '')); ?></strong>
                <?php if ((int) ($fix['type'] ?? 0) === RecommendedFix::TYPE_TEXT) : ?>
                    <div><?php echo wp_kses_post((string) ($fix['fix_text'] ?? '')); ?></div>
                <?php endif; ?>
            </div>
        <?php endforeach;
    }
}
