<?php

namespace App\Utils;

/**
 * Gestor mejorado de resultados de importación
 */
class ImportResultManager
{
    private $stats = [
        'total_rows' => 0,
        'processed' => 0, // 'created' y 'updated' se eliminan
        'errors' => 0,
        'warnings' => 0,
        'skipped' => 0
    ];

    private $error_details = [];
    private $warning_details = [];

    public function addError($message, $row_number = null)
    {
        $this->stats['errors']++;
        $this->error_details[] = [
            'message' => $message,
            'row' => $row_number,
            'type' => 'error'
        ];
    }

    public function addWarning($message, $row_number = null)
    {
        $this->stats['warnings']++;
        $this->warning_details[] = [
            'message' => $message,
            'row' => $row_number,
            'type' => 'warning'
        ];
    }

    public function incrementProcessed()
    {
        $this->stats['processed']++;
    }

    public function incrementSkipped()
    {
        $this->stats['skipped']++;
    }

    public function setTotalRows($count)
    {
        $this->stats['total_rows'] = $count;
    }

    public function getStats()
    {
        return $this->stats;
    }

    public function generateReport()
    {
        $success_rate = $this->stats['total_rows'] > 0
            ? round(($this->stats['processed'] / $this->stats['total_rows']) * 100, 2)
            : 0;

        $html = "<div class='import-results'>";
        $html .= "<h4>Resumen de Importación</h4>";
        $html .= "<div class='row'>";
        $html .= "<div class='col-md-6'>";
        $html .= "<ul class='list-unstyled'>";
        $html .= "<li><strong>Total de filas:</strong> {$this->stats['total_rows']}</li>";
        $html .= "<li><strong>Procesadas exitosamente:</strong> <span class='text-success'>{$this->stats['processed']}</span></li>"; // Se elimina el desglose
        $html .= "<li><strong>Errores:</strong> <span class='text-danger'>{$this->stats['errors']}</span></li>";
        $html .= "<li><strong>Advertencias:</strong> <span class='text-warning'>{$this->stats['warnings']}</span></li>";
        $html .= "<li><strong>Saltadas:</strong> <span class='text-muted'>{$this->stats['skipped']}</span></li>";
        $html .= "</ul>";
        $html .= "</div>";
        $html .= "<div class='col-md-6'>";
        $html .= "<div class='text-center'>";
        $html .= "<h5>Tasa de Éxito</h5>";
        $html .= "<span class='badge badge-" . ($success_rate >= 80 ? 'success' : ($success_rate >= 60 ? 'warning' : 'danger')) . " fs-6'>";
        $html .= "{$success_rate}%</span>";
        $html .= "</div>";
        $html .= "</div>";
        $html .= "</div>";

        // Detalles de errores
        if (!empty($this->error_details)) {
            $html .= "<h5 class='mt-3 text-danger'>Errores Encontrados:</h5>";
            $html .= "<div class='alert alert-danger'>";
            $html .= "<ul class='mb-0'>";
            foreach ($this->error_details as $error) {
                $row_info = $error['row'] ? " (Fila {$error['row']})" : "";
                $html .= "<li>{$error['message']}{$row_info}</li>";
            }
            $html .= "</ul>";
            $html .= "</div>";
        }

        // Detalles de advertencias
        if (!empty($this->warning_details)) {
            $html .= "<h5 class='mt-3 text-warning'>Advertencias:</h5>";
            $html .= "<div class='alert alert-warning'>";
            $html .= "<ul class='mb-0'>";
            foreach ($this->warning_details as $warning) {
                $row_info = $warning['row'] ? " (Fila {$warning['row']})" : "";
                $html .= "<li>{$warning['message']}{$row_info}</li>";
            }
            $html .= "</ul>";
            $html .= "</div>";
        }

        $html .= "</div>";
        return $html;
    }
}
