import { z } from "zod";

export const PlanStatusSchema = z.enum([
  "PROPOSED",
  "ACTIVE",
  "IDLE",
  "BLOCKED",
  "DONE",
  "SUPERSEDED",
]);

export type PlanStatus = z.infer<typeof PlanStatusSchema>;

export const IndexPlanRowSchema = z
  .object({
    priority: z.string().min(1),
    status: PlanStatusSchema,
    title: z.string().min(1),
    href: z.string().min(1),
    scope: z.string(),
    indexReceipt: z.string(),
  })
  .strict();

export type IndexPlanRow = z.infer<typeof IndexPlanRowSchema>;

export const PlanFrontmatterSchema = z
  .object({
    status: z.string().min(1),
    worker: z.string().optional(),
    taskIds: z.string().optional(),
    planReceipt: z.string().optional(),
    nextAction: z.string().optional(),
  })
  .strict();

export type PlanFrontmatter = z.infer<typeof PlanFrontmatterSchema>;

export const ActiveSessionSchema = z
  .object({
    ledgerId: z.string().min(1),
    runtime: z.string().min(1),
    host: z.string().min(1),
    startedAt: z.string().min(1),
    launchedBy: z.string().optional(),
    worktree: z.string().optional(),
  })
  .strict();

export type ActiveSession = z.infer<typeof ActiveSessionSchema>;

export const LiveReportPlanSchema = z
  .object({
    priority: z.string().min(1),
    indexStatus: PlanStatusSchema,
    fileStatus: z.string().optional(),
    statusMismatch: z.boolean(),
    title: z.string().min(1),
    href: z.string().min(1),
    scope: z.string(),
    indexReceipt: z.string(),
    worker: z.string().optional(),
    taskIds: z.string().optional(),
    planReceipt: z.string().optional(),
    nextAction: z.string().optional(),
  })
  .strict();

export type LiveReportPlan = z.infer<typeof LiveReportPlanSchema>;

export const LiveReportDataSchema = z
  .object({
    generatedAt: z.string().datetime(),
    sourceRevision: z.string().min(1),
    indexPath: z.string().min(1),
    plansDir: z.string().min(1),
    plans: z.array(LiveReportPlanSchema),
    activeWorkers: z.array(
      z
        .object({
          planTitle: z.string().min(1),
          worker: z.string().min(1),
          href: z.string().min(1),
          receipt: z.string(),
        })
        .strict(),
    ),
    blockers: z.array(
      z
        .object({
          planTitle: z.string().min(1),
          href: z.string().min(1),
          receipt: z.string(),
        })
        .strict(),
    ),
    activeSessions: z.array(ActiveSessionSchema),
    warnings: z.array(z.string()),
  })
  .strict();

export type LiveReportData = z.infer<typeof LiveReportDataSchema>;
