📄 src/Cli/Program.cs
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Slopper.Cli;
using Slopper.Cli.YouTubeAuth;
using Slopper.Domain;
using Slopper.Infrastructure.Ai;
using Slopper.Infrastructure.Database;
using Slopper.Infrastructure.Ffmpeg;
using Slopper.Infrastructure.YouTube;

var builder = Host.CreateApplicationBuilder();

builder.ConfigureOpenTelemetry();

builder.Services.AddClipSelector().AddClipGenerator();

builder.Services.AddJellyfinDatabase().AddSlopperDatabase().AddFfmpegServices().AddAi().AddYouTubeUploader();

builder.Services.AddYouTubeAuth();

using var app = builder.Build();

await app.StartAsync();

var lifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();
var logger = app.Services.GetRequiredService<ILogger<Program>>();

using var scope = app.Services.CreateScope();
var clipRepository = scope.ServiceProvider.GetRequiredService<IClipRepository>();

var clip = await clipRepository
    .GetLatest(limit: 1, cancellationToken: lifetime.ApplicationStopping)
    .FirstOrDefaultAsync(lifetime.ApplicationStopping);

if (clip is null)
{
    logger.LogInformation("No clips in repository");
    return;
}

logger.LogInformation("Uploading {ClipId}", clip.Id);

var youTubeUploader = app.Services.GetRequiredKeyedService<IUploader>("YouTube");
var upload = await youTubeUploader.Upload(clip, lifetime.ApplicationStopping);

logger.LogInformation("Clip uploaded at {Url}", upload.CanonicalUrl);

await app.StopAsync();