using System.Text;
using Core.Entities;
namespace GoogleSheetsScheduleImport;
///
/// Loads minimal rows from Tests-style CSV (column "Event").
///
public static class EventDefinitionsCsvLoader
{
public static List Load(string path)
{
var list = new List();
var lines = File.ReadAllLines(path);
if (lines.Length < 2)
return list;
var header = ParseCsvLine(lines[0]);
var eventIdx = header.FindIndex(h => h.Equals("Event", StringComparison.OrdinalIgnoreCase));
if (eventIdx < 0)
throw new InvalidOperationException($"CSV '{path}' must contain an 'Event' column header.");
for (var i = 1; i < lines.Length; i++)
{
if (string.IsNullOrWhiteSpace(lines[i]))
continue;
var cols = ParseCsvLine(lines[i]);
if (eventIdx >= cols.Count)
continue;
var name = cols[eventIdx].Trim();
if (string.IsNullOrWhiteSpace(name))
continue;
list.Add(new EventDefinition
{
Id = i,
Name = name,
ShortName = name,
Eligibility = "",
EventFormat = EventFormat.Team
});
}
return list;
}
private static List ParseCsvLine(string line)
{
var result = new List();
var cur = new StringBuilder();
var inQuotes = false;
for (var i = 0; i < line.Length; i++)
{
var ch = line[i];
if (inQuotes)
{
if (ch == '"')
{
if (i + 1 < line.Length && line[i + 1] == '"')
{
cur.Append('"');
i++;
}
else inQuotes = false;
}
else cur.Append(ch);
}
else
{
if (ch == '"') inQuotes = true;
else if (ch == ',')
{
result.Add(cur.ToString());
cur.Clear();
}
else cur.Append(ch);
}
}
result.Add(cur.ToString());
return result;
}
}