using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using LaDOSE.Business.Interface; using LaDOSE.Entity; using Microsoft.Extensions.Logging; namespace LaDOSE.Business.Provider.SheetsProvider { /// /// Writes the export to the log instead of to Google, and reports success. /// /// This exists so the whole feature — button, ordering, cumulative prefixes, payload shape — /// can be exercised end to end before any Google account, key or sharing exists. Set /// GoogleSheets:Writer to "Logging" and read the API log. It is also the safe way to try a /// selection without touching the real spreadsheet. /// public class LoggingSheetsWriter : ISheetsWriter { private readonly ILogger _logger; public LoggingSheetsWriter(ILogger logger) { _logger = logger; } public string Name => "Logging"; public bool IsConfigured => true; public Task WriteTablesAsync(SheetExportRequest request, CancellationToken ct = default) { var tabs = request.Tabs ?? new List(); _logger.LogInformation( "Sheets export (Logging writer): spreadsheet {SpreadsheetId}, {TabCount} tabs", request.SpreadsheetId, tabs.Count); foreach (var tab in tabs) { var header = tab.Header ?? new List(); var rows = tab.Rows ?? new List(); _logger.LogInformation(" [{Name}] event {EventId}: {RowCount} rows x {ColumnCount} columns | {Header}", tab.Name, tab.EventId, rows.Count, header.Count, string.Join(" | ", header)); foreach (var row in rows) { _logger.LogInformation(" {Player} | {Points} | {Total}", row.Player, string.Join(" | ", row.Points ?? new List()), row.Total); } foreach (var line in tab.Footer ?? new List()) { _logger.LogInformation(" -- {Line}", line); } } return Task.FromResult(new SheetExportResult { SpreadsheetId = request.SpreadsheetId, SpreadsheetUrl = SheetUrl(request.SpreadsheetId), Writer = Name, Tabs = tabs.Select(tab => new SheetTabResult { RequestedName = tab.Name, Name = tab.Name, Rows = (tab.Rows?.Count ?? 0) + 1, Columns = tab.Header?.Count ?? 0, // Nothing was inspected, so this is a claim rather than an observation. Created = true }).ToList(), Warnings = new List { "Writer is 'Logging': nothing was written to Google. The payload is in the API log." } }); } internal static string SheetUrl(string spreadsheetId) { return string.IsNullOrWhiteSpace(spreadsheetId) ? null : $"https://docs.google.com/spreadsheets/d/{spreadsheetId}/edit"; } } }