import { JobsManager } from '@/components/owner/owner-forms';
import { getLocale, t } from '@/lib/i18n';
import { getAllQueueStats } from '@/lib/owner-jobs';
import { prisma } from '@/lib/prisma';

export default async function OwnerJobsPage() {
  const locale = await getLocale();
  const [queues, migrations] = await Promise.all([
    getAllQueueStats(),
    prisma.$queryRaw<Array<{ migration_name: string; finished_at: Date | null }>>`
      SELECT migration_name, finished_at FROM "_prisma_migrations" ORDER BY finished_at DESC NULLS LAST
    `
  ]);

  return (
    <div className="space-y-6">
      <div>
        <h1 className="text-2xl font-semibold">{t(locale, 'owner.jobs.title')}</h1>
        <p className="text-sm text-muted-foreground">{t(locale, 'owner.jobs.subtitle')}</p>
      </div>
      <JobsManager
        initialQueues={queues}
        initialMigrations={migrations.map((row) => ({
          name: row.migration_name,
          finishedAt: row.finished_at?.toISOString() ?? null
        }))}
      />
    </div>
  );
}
