import { z } from 'zod';

import { gameServerStatusSchema, serverEditionSchema } from './servers';

export const edgeBackendSchema = z.object({
  host: z.string(),
  port: z.number().int(),
});

export const edgeActionSchema = z.enum(['proxy', 'start', 'wait', 'reject']);

export const edgeResolveResponseSchema = z.object({
  serverId: z.string().uuid(),
  slug: z.string(),
  status: gameServerStatusSchema,
  joinToStartEnabled: z.boolean(),
  edition: serverEditionSchema,
  backend: edgeBackendSchema.nullable(),
  action: edgeActionSchema,
  message: z.string().optional(),
});

export const edgeStartRequestSchema = z.object({
  clientIp: z.string().min(1).max(64),
});

export const edgeStartResponseSchema = z.object({
  resolve: edgeResolveResponseSchema,
  started: z.boolean(),
  jobId: z.string().optional(),
  queuePosition: z.number().int().optional(),
});

export type EdgeBackend = z.infer<typeof edgeBackendSchema>;
export type EdgeAction = z.infer<typeof edgeActionSchema>;
export type EdgeResolveResponse = z.infer<typeof edgeResolveResponseSchema>;
export type EdgeStartRequest = z.infer<typeof edgeStartRequestSchema>;
export type EdgeStartResponse = z.infer<typeof edgeStartResponseSchema>;
