using System.IO;
using System.Text;
using SharepointToolbox.Core.Models;
using SharepointToolbox.Localization;

namespace SharepointToolbox.Services.Export;

/// <summary>
/// Exports SearchResult list to a self-contained sortable/filterable HTML report.
/// Port of PS Export-SearchToHTML (PS lines 2112-2233).
/// Columns are sortable by clicking the header. A filter input narrows rows by text match.
/// </summary>
public class SearchHtmlExportService
{
    /// <summary>
    /// Builds a self-contained HTML table with inline sort/filter scripts.
    /// Each <see cref="SearchResult"/> becomes one row; the document has no
    /// external dependencies.
    /// </summary>
    public string BuildHtml(IReadOnlyList<SearchResult> results, ReportBranding? branding = null)
    {
        var T = TranslationSource.Instance;
        var sb = new StringBuilder();

        sb.AppendLine("<!DOCTYPE html>");
        sb.AppendLine("<html lang=\"en\">");
        sb.AppendLine("<head>");
        sb.AppendLine("<meta charset=\"UTF-8\">");
        sb.AppendLine("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
        sb.AppendLine($"<title>{T["report.title.search"]}</title>");
        sb.AppendLine("""
            <style>
              body { font-family: 'Segoe UI', Arial, sans-serif; font-size: 13px; margin: 20px; background: #f5f5f5; }
              h1 { color: #0078d4; }
              .toolbar { margin-bottom: 10px; display: flex; gap: 12px; align-items: center; }
              .toolbar label { font-weight: 600; }
              #filterInput { padding: 6px 10px; border: 1px solid #ccc; border-radius: 4px; width: 280px; font-size: 13px; }
              #resultCount { font-size: 12px; color: #666; }
              table { border-collapse: collapse; width: 100%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.15); }
              th { background: #0078d4; color: #fff; padding: 8px 12px; text-align: left; cursor: pointer;
                   font-weight: 600; user-select: none; white-space: nowrap; }
              th:hover { background: #106ebe; }
              th.sorted-asc::after  { content: ' ▲'; font-size: 10px; }
              th.sorted-desc::after { content: ' ▼'; font-size: 10px; }
              td { padding: 6px 12px; border-bottom: 1px solid #e0e0e0; word-break: break-all; }
              tr:hover td { background: #f0f7ff; }
              tr.hidden { display: none; }
              .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
              .generated { font-size: 11px; color: #888; margin-top: 12px; }
            </style>
            </head>
            <body>
            """);
        sb.Append(BrandingHtmlHelper.BuildBrandingHeader(branding));
        sb.AppendLine($"""
            <h1>{T["report.title.search_short"]}</h1>
            <div class="toolbar">
              <label for="filterInput">{T["report.filter.label"]}</label>
              <input id="filterInput" type="text" placeholder="{T["report.filter.placeholder_rows"]}" oninput="filterTable()" />
              <span id="resultCount"></span>
            </div>
            """);

        sb.AppendLine($"""
            <table id="resultsTable">
              <thead>
                <tr>
                  <th onclick="sortTable(0)">{T["report.col.file_name"]}</th>
                  <th onclick="sortTable(1)">{T["report.col.extension"]}</th>
                  <th onclick="sortTable(2)">{T["report.col.path"]}</th>
                  <th onclick="sortTable(3)">{T["report.col.created"]}</th>
                  <th onclick="sortTable(4)">{T["report.col.created_by"]}</th>
                  <th onclick="sortTable(5)">{T["report.col.modified"]}</th>
                  <th onclick="sortTable(6)">{T["report.col.modified_by"]}</th>
                  <th class="num" onclick="sortTable(7)">{T["report.col.size"]}</th>
                </tr>
              </thead>
              <tbody>
            """);

        foreach (var r in results)
        {
            string fileName = System.IO.Path.GetFileName(r.Path);
            if (string.IsNullOrEmpty(fileName)) fileName = r.Title;

            sb.AppendLine($"""
                  <tr>
                    <td>{H(fileName)}</td>
                    <td>{H(r.FileExtension)}</td>
                    <td><a href="{H(r.Path)}" target="_blank">{H(r.Path)}</a></td>
                    <td>{(r.Created.HasValue ? r.Created.Value.ToString("yyyy-MM-dd") : string.Empty)}</td>
                    <td>{H(r.Author)}</td>
                    <td>{(r.LastModified.HasValue ? r.LastModified.Value.ToString("yyyy-MM-dd") : string.Empty)}</td>
                    <td>{H(r.ModifiedBy)}</td>
                    <td class="num" data-sort="{r.SizeBytes}">{FormatSize(r.SizeBytes)}</td>
                  </tr>
                """);
        }

        sb.AppendLine("  </tbody>\n</table>");

        int count = results.Count;
        sb.AppendLine($"<p class=\"generated\">{T["report.text.generated_colon"]} {DateTime.Now:yyyy-MM-dd HH:mm} — {count:N0} {T["report.text.results_parens"]}</p>");

        sb.AppendLine($$"""
            <script>
            var sortDir = {};
            function sortTable(col) {
              var tbl = document.getElementById('resultsTable');
              var tbody = tbl.tBodies[0];
              var rows = Array.from(tbody.rows);
              var asc = sortDir[col] !== 'asc';
              sortDir[col] = asc ? 'asc' : 'desc';
              rows.sort(function(a, b) {
                var av = a.cells[col].dataset.sort || a.cells[col].innerText;
                var bv = b.cells[col].dataset.sort || b.cells[col].innerText;
                var an = parseFloat(av), bn = parseFloat(bv);
                if (!isNaN(an) && !isNaN(bn)) return asc ? an - bn : bn - an;
                return asc ? av.localeCompare(bv) : bv.localeCompare(av);
              });
              rows.forEach(function(r) { tbody.appendChild(r); });
              var ths = tbl.tHead.rows[0].cells;
              for (var i = 0; i < ths.length; i++) {
                ths[i].className = (i === col) ? (asc ? 'sorted-asc' : 'sorted-desc') : '';
              }
            }
            function filterTable() {
              var q = document.getElementById('filterInput').value.toLowerCase();
              var rows = document.getElementById('resultsTable').tBodies[0].rows;
              var visible = 0;
              for (var i = 0; i < rows.length; i++) {
                var match = rows[i].innerText.toLowerCase().indexOf(q) >= 0;
                rows[i].className = match ? '' : 'hidden';
                if (match) visible++;
              }
              document.getElementById('resultCount').innerText = q ? (visible + ' {{T["report.text.of"]}} {{count:N0}} {{T["report.text.shown"]}}') : '';
            }
            window.onload = function() {
              document.getElementById('resultCount').innerText = '{{count:N0}} {{T["report.text.results_parens"]}}';
            };
            </script>
            </body></html>
            """);

        return sb.ToString();
    }

    /// <summary>Writes the HTML report to <paramref name="filePath"/>.</summary>
    public async Task WriteAsync(IReadOnlyList<SearchResult> results, string filePath, CancellationToken ct, ReportBranding? branding = null)
    {
        var html = BuildHtml(results, branding);
        await System.IO.File.WriteAllTextAsync(filePath, html, Encoding.UTF8, ct);
    }

    private static string H(string value) =>
        System.Net.WebUtility.HtmlEncode(value ?? string.Empty);

    private static string FormatSize(long bytes)
    {
        if (bytes >= 1_073_741_824L) return $"{bytes / 1_073_741_824.0:F2} GB";
        if (bytes >= 1_048_576L)     return $"{bytes / 1_048_576.0:F2} MB";
        if (bytes >= 1024L)          return $"{bytes / 1024.0:F2} KB";
        return $"{bytes} B";
    }
}
