using NTP.Application.Grains.Permits;
namespace NTP.Application.Jobs;
///
/// The jobs the scheduler can run (ports of the legacy jobs\*.cfm files). To add one: add an entry here and make sure its code is safe to
/// run repeatedly. Everything about WHEN it runs lives in the database (screen "المهام المجدولة", SU only).
///
public static class JobCatalog
{
private static T Get(IServiceProvider sp) where T : class => (T)(sp.GetService(typeof(T)) ?? throw new InvalidOperationException($"{typeof(T).Name} is not registered"));
public static readonly IReadOnlyList All =
[
new("activate-company-destinations",
"تفعيل وجهات الشركات المعلقة", "Activate pending company destinations",
"jobs/activateGrainsDestination.cfm: الوجهات التي مضى على تقديمها أكثر من 3 أيام (معلقة) تصبح فعالة، وتلغى الوجهات الفعالة السابقة لنفس القاطرة.",
"jobs/activateGrainsDestination.cfm: destinations pending for more than 3 days become active and the truck's previous active destinations are deactivated.",
async (sp, ct) =>
{
var n = await Get(sp).ActivatePendingCompanyDestinationsAsync(ct);
return new JobOutcome(n, $"تم تفعيل {n} وجهة");
}),
new("close-old-permits",
"إغلاق الارساليات القديمة", "Close old permits",
"jobs/GRAINS_Close_Old_Permits.cfm: إغلاق الارساليات التي مضى على تحميلها 48 ساعة، أو التي أكملت التفريغ ولم تغلق، أو المكررة لنفس القاطرة، بحدث \"System Close.\".",
"jobs/GRAINS_Close_Old_Permits.cfm: closes permits loaded more than 48 hours ago, finished but not closed, or duplicated for the same truck, with the event \"System Close.\".",
async (sp, ct) =>
{
var n = await Get(sp).CloseOldPermitsAsync(ct);
return new JobOutcome(n, $"تم إغلاق {n} إرسالية");
}),
new("fill-company-shares",
"استكمال ارساليات الشركات حسب الحصة", "Top up company permits to their share",
"jobs/parseGrainsPermits.cfm: لكل شركة، إنشاء إرساليات مفتوحة (على الدور) لتساوي حصتها من الدور في المشول والسائب.",
"jobs/parseGrainsPermits.cfm: for every company, creates open permits (on the turn) up to its queue share, for packed and bulk.",
async (sp, ct) =>
{
var n = await Get(sp).FillCompanySharesAsync(ct);
return new JobOutcome(n, $"تم إنشاء {n} إرسالية");
}),
];
public static JobDefinition? Find(string key) => All.FirstOrDefault(j => string.Equals(j.Key, key, StringComparison.OrdinalIgnoreCase));
}