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

namespace SharepointToolbox.Services.Export;

/// <summary>
/// Exports user access audit results to CSV format.
/// Produces one CSV file per audited user with a summary section at the top.
/// </summary>
public class UserAccessCsvExportService
{
    private static string BuildDataHeader()
    {
        var T = TranslationSource.Instance;
        return $"\"{T["report.col.site"]}\",\"{T["report.col.object_type"]}\",\"{T["report.col.object"]}\",\"{T["report.col.url"]}\",\"{T["report.col.permission_level"]}\",\"{T["report.col.access_type"]}\",\"{T["report.col.granted_through"]}\",\"TargetLabel\",\"TargetUrl\",\"SharingLinkType\"";
    }

    /// <summary>
    /// Builds a CSV string for a single user's access entries.
    /// Includes a summary section at the top followed by data rows.
    /// </summary>
    public string BuildCsv(string userDisplayName, string userLogin, IReadOnlyList<UserAccessEntry> entries)
    {
        var T = TranslationSource.Instance;
        var sb = new StringBuilder();

        // Summary section
        var sitesCount = entries.Select(e => e.SiteUrl).Distinct().Count();
        var highPrivCount = entries.Count(e => e.IsHighPrivilege);

        sb.AppendLine($"\"{T["report.title.user_access"]}\"");
        sb.AppendLine($"\"{T["report.col.user"]}\",\"{Csv(userDisplayName)} ({Csv(userLogin)})\"");
        sb.AppendLine($"\"{T["report.stat.total_accesses"]}\",\"{entries.Count}\"");
        sb.AppendLine($"\"{T["report.col.sites"]}\",\"{sitesCount}\"");
        sb.AppendLine($"\"{T["report.stat.high_privilege"]}\",\"{highPrivCount}\"");
        sb.AppendLine($"\"{T["report.text.generated"]}\",\"{DateTime.Now:yyyy-MM-dd HH:mm:ss}\"");
        sb.AppendLine(); // Blank line separating summary from data

        // Data rows
        sb.AppendLine(BuildDataHeader());
        foreach (var entry in entries)
        {
            sb.AppendLine(string.Join(",", new[]
            {
                Csv(entry.SiteTitle),
                Csv(entry.ObjectType),
                Csv(entry.ObjectTitle),
                Csv(entry.ObjectUrl),
                Csv(entry.PermissionLevel),
                Csv(entry.AccessType.ToString()),
                Csv(entry.GrantedThrough),
                Csv(entry.TargetLabel ?? string.Empty),
                Csv(entry.TargetUrl ?? string.Empty),
                Csv(entry.SharingLinkType ?? string.Empty)
            }));
        }

        return sb.ToString();
    }

    /// <summary>
    /// Writes one CSV file per user to the specified directory.
    /// File names: audit_{email}_{date}.csv
    /// </summary>
    public async Task WriteAsync(
        IReadOnlyList<UserAccessEntry> allEntries,
        string directoryPath,
        CancellationToken ct)
    {
        Directory.CreateDirectory(directoryPath);
        var dateStr = DateTime.Now.ToString("yyyy-MM-dd");

        // Group by user
        var byUser = allEntries.GroupBy(e => e.UserLogin);

        foreach (var group in byUser)
        {
            ct.ThrowIfCancellationRequested();

            var userLogin = group.Key;
            var displayName = group.First().UserDisplayName;
            var entries = group.ToList();

            // Sanitize email for filename (replace @ and other invalid chars)
            var safeLogin = SanitizeFileName(userLogin);
            var fileName = $"audit_{safeLogin}_{dateStr}.csv";
            var filePath = Path.Combine(directoryPath, fileName);

            var csv = BuildCsv(displayName, userLogin, entries);
            await ExportFileWriter.WriteCsvAsync(filePath, csv, ct);
        }
    }

    /// <summary>
    /// Writes all entries split per site. File naming: "{base}_{siteLabel}.csv".
    /// </summary>
    public async Task WriteBySiteAsync(
        IReadOnlyList<UserAccessEntry> allEntries,
        string basePath,
        CancellationToken ct,
        bool mergePermissions = false)
    {
        foreach (var group in allEntries.GroupBy(e => (e.SiteUrl, e.SiteTitle)))
        {
            ct.ThrowIfCancellationRequested();
            var label = ReportSplitHelper.DeriveSiteLabel(group.Key.SiteUrl, group.Key.SiteTitle);
            var path = ReportSplitHelper.BuildPartitionPath(basePath, label);
            await WriteSingleFileAsync(group.ToList(), path, ct, mergePermissions);
        }
    }

    /// <summary>
    /// Split-aware export dispatcher.
    /// Single → one file at <paramref name="basePath"/>.
    /// BySite → one file per site. ByUser → one file per user.
    /// </summary>
    public async Task WriteAsync(
        IReadOnlyList<UserAccessEntry> allEntries,
        string basePath,
        ReportSplitMode splitMode,
        CancellationToken ct,
        bool mergePermissions = false)
    {
        switch (splitMode)
        {
            case ReportSplitMode.Single:
                await WriteSingleFileAsync(allEntries, basePath, ct, mergePermissions);
                break;
            case ReportSplitMode.BySite:
                await WriteBySiteAsync(allEntries, basePath, ct, mergePermissions);
                break;
            case ReportSplitMode.ByUser:
                await WriteByUserAsync(allEntries, basePath, ct, mergePermissions);
                break;
        }
    }

    /// <summary>
    /// Writes one CSV per user using <paramref name="basePath"/> as a filename template.
    /// </summary>
    public async Task WriteByUserAsync(
        IReadOnlyList<UserAccessEntry> allEntries,
        string basePath,
        CancellationToken ct,
        bool mergePermissions = false)
    {
        foreach (var group in allEntries.GroupBy(e => e.UserLogin))
        {
            ct.ThrowIfCancellationRequested();
            var label = ReportSplitHelper.SanitizeFileName(group.Key);
            var path = ReportSplitHelper.BuildPartitionPath(basePath, label);
            await WriteSingleFileAsync(group.ToList(), path, ct, mergePermissions);
        }
    }

    /// <summary>
    /// Writes all entries to a single CSV file (alternative for single-file export).
    /// Used when the ViewModel export command picks a single file path.
    /// When <paramref name="mergePermissions"/> is true, entries are consolidated using
    /// <see cref="PermissionConsolidator"/> and written in a compact multi-location format.
    /// </summary>
    public async Task WriteSingleFileAsync(
        IReadOnlyList<UserAccessEntry> entries,
        string filePath,
        CancellationToken ct,
        bool mergePermissions = false)
    {
        var T = TranslationSource.Instance;
        if (mergePermissions)
        {
            var consolidated = PermissionConsolidator.Consolidate(entries);
            var sb = new StringBuilder();

            // Summary section
            sb.AppendLine($"\"{T["report.title.user_access_consolidated"]}\"");
            sb.AppendLine($"\"{T["report.stat.users_audited"]}\",\"{consolidated.Select(e => e.UserLogin).Distinct().Count()}\"");
            sb.AppendLine($"\"{T["report.stat.total_entries"]}\",\"{consolidated.Count}\"");
            sb.AppendLine($"\"{T["report.text.generated"]}\",\"{DateTime.Now:yyyy-MM-dd HH:mm:ss}\"");
            sb.AppendLine();

            // Header
            sb.AppendLine($"\"{T["report.col.user"]}\",\"User Login\",\"{T["report.col.permission_level"]}\",\"{T["report.col.access_type"]}\",\"{T["report.col.granted_through"]}\",\"TargetLabel\",\"TargetUrl\",\"SharingLinkType\",\"Locations\",\"Location Count\"");

            // Data rows
            foreach (var entry in consolidated)
            {
                var locations = string.Join("; ", entry.Locations.Select(l => l.SiteTitle));
                sb.AppendLine(string.Join(",", new[]
                {
                    Csv(entry.UserDisplayName),
                    Csv(entry.UserLogin),
                    Csv(entry.PermissionLevel),
                    Csv(entry.AccessType.ToString()),
                    Csv(entry.GrantedThrough),
                    Csv(entry.TargetLabel ?? string.Empty),
                    Csv(entry.TargetUrl ?? string.Empty),
                    Csv(entry.SharingLinkType ?? string.Empty),
                    Csv(locations),
                    Csv(entry.LocationCount.ToString())
                }));
            }

            await File.WriteAllTextAsync(filePath, sb.ToString(), new UTF8Encoding(false), ct);
            return;
        }

        {
        var sb = new StringBuilder();
        var fullHeader = $"\"{T["report.col.user"]}\",\"User Login\"," + BuildDataHeader();

        // Summary
        var users = entries.Select(e => e.UserLogin).Distinct().ToList();
        sb.AppendLine($"\"{T["report.title.user_access"]}\"");
        sb.AppendLine($"\"{T["report.stat.users_audited"]}\",\"{users.Count}\"");
        sb.AppendLine($"\"{T["report.stat.total_accesses"]}\",\"{entries.Count}\"");
        sb.AppendLine($"\"{T["report.text.generated"]}\",\"{DateTime.Now:yyyy-MM-dd HH:mm:ss}\"");
        sb.AppendLine();

        sb.AppendLine(fullHeader);
        foreach (var entry in entries)
        {
            sb.AppendLine(string.Join(",", new[]
            {
                Csv(entry.UserDisplayName),
                Csv(entry.UserLogin),
                Csv(entry.SiteTitle),
                Csv(entry.ObjectType),
                Csv(entry.ObjectTitle),
                Csv(entry.ObjectUrl),
                Csv(entry.PermissionLevel),
                Csv(entry.AccessType.ToString()),
                Csv(entry.GrantedThrough),
                Csv(entry.TargetLabel ?? string.Empty),
                Csv(entry.TargetUrl ?? string.Empty),
                Csv(entry.SharingLinkType ?? string.Empty)
            }));
        }

        await ExportFileWriter.WriteCsvAsync(filePath, sb.ToString(), ct);
        }
    }

    /// <summary>RFC 4180 CSV field escaping with formula-injection guard.</summary>
    private static string Csv(string value) => CsvSanitizer.Escape(value);

    private static string SanitizeFileName(string name)
    {
        var invalid = Path.GetInvalidFileNameChars();
        var sb = new StringBuilder(name.Length);
        foreach (var c in name)
            sb.Append(invalid.Contains(c) ? '_' : c);
        return sb.ToString();
    }
}
