using System.CommandLine;
using Electricity.Migrator;
using Electricity.Migrator.Options;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var rootCommand = new RootCommand("This is a migrator tool to migrate file logs to an Sqlite3 database");

var connectionStringArgument = new Option<string>(
    name: "--connection-string",
    description: "Filepath to the Sqlite3 database file (*.db) to create");
rootCommand.AddOption(connectionStringArgument);

var logFilePathArgument = new Option<string>(
    name: "--log-files",
    description: "Filepath directory containing the year directories of the log files");
rootCommand.AddOption(logFilePathArgument);

var result = rootCommand.Parse(args);

if (result.Errors.Any())
{
    foreach (var error in result.Errors)
    {
        Console.WriteLine(error.Message);
    }
    return 1;
}

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddDbContext<DatabaseContext>((DbContextOptionsBuilder builder) =>
{
    builder.UseSqlite($"Data Source={result.GetValueForOption(connectionStringArgument)}");
});

builder.Services.AddSingleton(new LogFileOption(result.GetValueForOption(logFilePathArgument)));

builder.Services.AddHostedService<MigratorHostedService>();

var host = builder.Build();

host.Run();

return 0;