using ClipForge.Api.Endpoints; using ClipForge.Infrastructure; using ClipForge.Infrastructure.Data; using ClipForge.Workers.Jobs; using Hangfire; using Hangfire.PostgreSql; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); // Add Infrastructure (DI, DbContext, Services) builder.Services.AddInfrastructure(builder.Configuration); // Workers builder.Services.AddScoped(); // Hangfire builder.Services.AddHangfire(config => config.UsePostgreSqlStorage(options => options.UseNpgsqlConnection(builder.Configuration.GetConnectionString("DefaultConnection")))); builder.Services.AddHangfireServer(); var app = builder.Build(); // Apply migrations on startup using (var scope = app.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); await db.Database.MigrateAsync(); } // Hangfire Dashboard app.UseHangfireDashboard("/hangfire"); // Serve static files (frontend) app.UseDefaultFiles(); app.UseStaticFiles(); // Map API endpoints app.MapIdeaEndpoints(); app.MapScriptEndpoints(); app.MapVoiceEndpoints(); app.MapVideoEndpoints(); app.MapPipelineEndpoints(); app.MapGet("/api/status", () => Results.Ok(new { Name = "ClipForge API", Version = "1.0.0", Status = "Running" })); // Temporary cleanup endpoint app.MapDelete("/api/admin/cleanup/{keepVideoId}", async (Guid keepVideoId, ClipForge.Infrastructure.Data.AppDbContext db) => { var sql = $@" DELETE FROM ""PublishJobs"" WHERE ""VideoAssetId"" != '{keepVideoId}'; DELETE FROM ""CaptionAssets"" WHERE ""VideoAssetId"" != '{keepVideoId}'; DELETE FROM ""VideoAssets"" WHERE ""Id"" != '{keepVideoId}'; DELETE FROM ""VoiceAssets"" WHERE ""VideoScriptId"" NOT IN (SELECT ""VideoScriptId"" FROM ""VideoAssets"" WHERE ""Id"" = '{keepVideoId}'); DELETE FROM ""VideoScripts"" WHERE ""Id"" NOT IN (SELECT ""VideoScriptId"" FROM ""VideoAssets"" WHERE ""Id"" = '{keepVideoId}'); DELETE FROM ""VideoIdeas"" WHERE ""Id"" NOT IN (SELECT ""VideoIdeaId"" FROM ""VideoScripts"" WHERE ""Id"" IN (SELECT ""VideoScriptId"" FROM ""VideoAssets"" WHERE ""Id"" = '{keepVideoId}')); "; var rows = await db.Database.ExecuteSqlRawAsync(sql); return Results.Ok(new { Message = "Cleanup done", RowsDeleted = rows }); }); app.MapDelete("/api/admin/clear-all", async (ClipForge.Infrastructure.Data.AppDbContext db) => { var sql = @" DELETE FROM ""PublishJobs""; DELETE FROM ""CaptionAssets""; DELETE FROM ""VideoAssets""; DELETE FROM ""VoiceAssets""; DELETE FROM ""VideoScripts""; DELETE FROM ""VideoIdeas""; "; var rows = await db.Database.ExecuteSqlRawAsync(sql); // Clean output files var outputPath = Path.Combine(app.Environment.ContentRootPath, "output"); if (Directory.Exists(outputPath)) { foreach (var dir in Directory.GetDirectories(outputPath)) Directory.Delete(dir, true); foreach (var dir in new[] { "audio", "videos", "thumbnails", "subtitles", "temp" }) Directory.CreateDirectory(Path.Combine(outputPath, dir)); } return Results.Ok(new { Message = "All data cleared", RowsDeleted = rows }); }); app.Run();