import { z } from 'zod';

// A probed value is `up` (confirmed healthy), `down` (confirmed unhealthy/inactive),
// or `unknown` (the probe itself could not run — command missing, timed out, or no
// permission). This lets the UI distinguish "definitely down" from "couldn't tell".
export const ProbeStatusSchema = z.enum(['up', 'down', 'unknown']);
export type ProbeStatus = z.infer<typeof ProbeStatusSchema>;

export const HermesOpsTimerSchema = z.object({
  name: z.string(),
  active: z.boolean(),
  status: ProbeStatusSchema,
  nextRun: z.string().nullable(),
  lastRun: z.string().nullable(),
});
export type HermesOpsTimer = z.infer<typeof HermesOpsTimerSchema>;

export const HermesOpsRepoSchema = z.object({
  path: z.string(),
  branch: z.string().nullable(),
  clean: z.boolean(),
  head: z.string().nullable(),
  lastCommitAt: z.string().nullable(),
  size: z.string().nullable(),
  // `up` = HEAD was readable; `unknown` = git could not be read (path/permission).
  status: ProbeStatusSchema,
});
export type HermesOpsRepo = z.infer<typeof HermesOpsRepoSchema>;

export const HermesOpsGoogleSchema = z.object({
  workspaceToken: z.boolean(),
  driveFolder: z.string(),
});
export type HermesOpsGoogle = z.infer<typeof HermesOpsGoogleSchema>;

export const HermesOpsGatewaySchema = z.object({
  service: z.string(),
  active: z.boolean(),
  enabled: z.boolean(),
  status: ProbeStatusSchema,
});

export const HermesOpsDashboardSchema = z.object({
  service: z.string(),
  active: z.boolean(),
  url: z.string(),
  status: ProbeStatusSchema,
});

export const HermesOpsInstanceSchema = z.object({
  id: z.enum(['vijay', 'bheem']),
  label: z.string(),
  hermesHome: z.string(),
  gateway: HermesOpsGatewaySchema,
  dashboard: HermesOpsDashboardSchema,
  backup: z.object({
    timer: HermesOpsTimerSchema,
    repo: HermesOpsRepoSchema,
    restoredFileCount: z.number().nullable(),
    restoredCronJobs: z.number().nullable(),
  }),
  google: HermesOpsGoogleSchema,
});
export type HermesOpsInstance = z.infer<typeof HermesOpsInstanceSchema>;

export const HermesOpsSessionSummarySchema = z.object({
  active: z.number(),
  updatedAt: z.string().nullable(),
});
export type HermesOpsSessionSummary = z.infer<typeof HermesOpsSessionSummarySchema>;

export const HermesOpsCronJobSchema = z.object({
  name: z.string(),
  label: z.string(),
  active: z.boolean(),
  status: ProbeStatusSchema,
  nextRun: z.string().nullable(),
  lastRun: z.string().nullable(),
});
export type HermesOpsCronJob = z.infer<typeof HermesOpsCronJobSchema>;

export const HermesOpsLinkSchema = z.object({
  label: z.string(),
  href: z.string(),
  description: z.string(),
});
export type HermesOpsLink = z.infer<typeof HermesOpsLinkSchema>;

export const HermesOpsSnapshotSchema = z.object({
  generatedAt: z.string(),
  // True when this payload was served from the short-TTL cache rather than freshly probed.
  cached: z.boolean(),
  tailscaleIp: z.string().nullable(),
  emergencyDriveUpload: HermesOpsTimerSchema,
  activeSessions: HermesOpsSessionSummarySchema,
  cronJobs: z.array(HermesOpsCronJobSchema),
  recentAlerts: z.array(z.string()),
  quickLinks: z.array(HermesOpsLinkSchema),
  instances: z.array(HermesOpsInstanceSchema),
  warnings: z.array(z.string()),
});
export type HermesOpsSnapshot = z.infer<typeof HermesOpsSnapshotSchema>;
