using ClassNotify.Server.Data; using Microsoft.EntityFrameworkCore; namespace ClassNotify.VoucherTool; public class Program { public static async Task Main(string[] args) { Console.WriteLine("================================================================================"); Console.WriteLine(" ClassNotify Commercial Voucher Batch & Administration Tool (v1.0.0)"); Console.WriteLine(" Zero Plaintext in Database | 16-Char Unbiased RNG | Safe Atomic Export Pipeline"); Console.WriteLine("================================================================================"); var connStr = Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection") ?? "Host=127.0.0.1;Port=5432;Database=classnotify;Username=classnotify;Password=CHANGEME_DEV;"; var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseNpgsql(connStr); await using var db = new ClassNotifyDbContext(optionsBuilder.Options); if (args.Length == 0 || args[0] is "--help" or "-h" or "help") { PrintUsage(); return 0; } var command = args[0].ToLowerInvariant(); try { switch (command) { case "generate": { int count = 100; int duration = 365; string outFile = $"vouchers_export_{DateTime.UtcNow:yyyyMMdd_HHmmss}.csv"; for (int i = 1; i < args.Length; i++) { if (args[i] == "--count" && i + 1 < args.Length) count = int.Parse(args[++i]); else if (args[i] == "--duration" && i + 1 < args.Length) duration = int.Parse(args[++i]); else if (args[i] == "--out" && i + 1 < args.Length) outFile = args[++i]; } await VoucherGenerator.ExecuteBatchExportAsync(db, count, duration, outFile); return 0; } case "stats": { var total = await db.SubscriptionVouchers.CountAsync(); var used = await db.SubscriptionVouchers.CountAsync(v => v.IsUsed); var unused = total - used; Console.WriteLine($"[VOUCHER STATS]"); Console.WriteLine($" Total Vouchers in DB: {total}"); Console.WriteLine($" Available (Unused): {unused}"); Console.WriteLine($" Redeemed (Used): {used}"); return 0; } case "verify": { if (args.Length < 2) { Console.Error.WriteLine("Error: Missing voucher key to verify. Usage: verify <16-char-code>"); return 1; } var code = args[1]; var hash = VoucherGenerator.ComputeSha256(code); var voucher = await db.SubscriptionVouchers.FirstOrDefaultAsync(v => v.CodeHash == hash); if (voucher == null) { Console.WriteLine($"[RESULT] Code '{code}' NOT FOUND in database."); return 2; } Console.WriteLine($"[RESULT] Voucher Found!"); Console.WriteLine($" Status: {(voucher.IsUsed ? "REDEEMED (Used)" : "VALID (Unused)")}"); Console.WriteLine($" Duration: {voucher.DurationDays} Days"); Console.WriteLine($" Generated At: {voucher.CreatedAt:O}"); if (voucher.IsUsed) { Console.WriteLine($" Used By Teacher: {voucher.UsedByTeacherId}"); Console.WriteLine($" Used At: {voucher.UsedAt:O}"); } return 0; } default: Console.Error.WriteLine($"Unknown command '{command}'."); PrintUsage(); return 1; } } catch (Exception ex) { Console.Error.WriteLine($"[FATAL ERROR] {ex.Message}"); return 99; } } private static void PrintUsage() { Console.WriteLine("\nUsage:"); Console.WriteLine(" dotnet run -- generate --count --duration --out "); Console.WriteLine(" dotnet run -- stats"); Console.WriteLine(" dotnet run -- verify <16-char-code>"); Console.WriteLine("\nExamples:"); Console.WriteLine(" generate --count 50 --duration 365 --out export_50_keys.csv"); Console.WriteLine(" verify 2345-6789-ABCD-EFGH"); Console.WriteLine(" stats\n"); } }