using File_Folder_Helper.Models;
using Microsoft.Extensions.Logging;
using System.Text.Json;

namespace File_Folder_Helper.ADO2024.PI2;

internal static partial class Helper20240518
{

    internal static void PersonKeyToImmichImport(ILogger<Worker> logger, List<string> args)
    {
        string name;
        string birthDate;
        string ownerId = args[5];
        List<string> inserts = [];
        string tableName = args[3];
        string birthDateFormat = args[6];
        string[] columns = args[4].Split(',');
        string json = File.ReadAllText(args[2]);
        string root = Path.GetFullPath(args[0]);
        Dictionary<long, Person> people = JsonSerializer.Deserialize(json, PeopleSourceGenerationContext.Default.DictionaryInt64Person) ?? throw new NullReferenceException();
        foreach (KeyValuePair<long, Person> keyValuePair in people)
        {
            if (keyValuePair.Value.Birth?.Note is null || keyValuePair.Value.Name?.ForwardSlashFull is null || keyValuePair.Value.Birth?.Date is null)
                continue;
            birthDate = keyValuePair.Value.Birth.Date.Value.ToString(birthDateFormat);
            name = keyValuePair.Value.Name.ForwardSlashFull.Replace("/", string.Empty);
            inserts.Add($"insert into \"{tableName}\" (\"{string.Join("\", \"", columns)}\") values ('{ownerId}', '{name}', '{birthDate}');");
        }
        string file = Path.Combine(root, $"{DateTime.Now.Ticks}.sql");
        logger.LogInformation("<{file}> saved", file);
        File.WriteAllLines(file, inserts);
    }

}