import { z } from 'zod';

export const gameServerStatusSchema = z.enum([
  'DRAFT',
  'PROVISIONING',
  'INSTALLING',
  'STOPPED',
  'STARTING',
  'RUNNING',
  'STOPPING',
  'ERROR',
  'DELETING',
  'DELETED',
]);

export const serverEditionSchema = z.enum(['JAVA', 'BEDROCK']);

export const softwareFamilySchema = z.enum([
  'VANILLA',
  'PAPER',
  'PURPUR',
  'FABRIC',
  'FORGE',
  'NEOFORGE',
  'QUILT',
  'BEDROCK_DEDICATED',
]);

export const createServerRequestSchema = z.object({
  name: z.string().min(1).max(64),
  edition: serverEditionSchema.default('JAVA'),
  softwareFamily: softwareFamilySchema.default('VANILLA'),
  minecraftVersion: z.string().min(1).max(32).default('1.21.1'),
  eulaAccepted: z.literal(true, {
    errorMap: () => ({ message: 'EULA must be accepted' }),
  }),
  ramMb: z.number().int().min(512).max(65536).optional(),
  planId: z.string().uuid().optional(),
});

export const serverResponseSchema = z.object({
  id: z.string().uuid(),
  userId: z.string().uuid(),
  planId: z.string().uuid().nullable(),
  nodeId: z.string().uuid().nullable(),
  name: z.string(),
  status: gameServerStatusSchema,
  version: z.number().int(),
  edition: serverEditionSchema,
  softwareFamily: softwareFamilySchema,
  minecraftVersion: z.string(),
  eulaAccepted: z.boolean(),
  hostPort: z.number().int().nullable(),
  containerId: z.string().nullable(),
  dataPath: z.string().nullable(),
  ramMb: z.number().int(),
  createdAt: z.string().datetime(),
  updatedAt: z.string().datetime(),
});

export const serverListResponseSchema = z.object({
  servers: z.array(serverResponseSchema),
});

export const serverActionResponseSchema = z.object({
  server: serverResponseSchema,
  jobId: z.string().optional(),
});

export type GameServerStatus = z.infer<typeof gameServerStatusSchema>;
export type ServerEdition = z.infer<typeof serverEditionSchema>;
export type SoftwareFamily = z.infer<typeof softwareFamilySchema>;
export type CreateServerRequest = z.infer<typeof createServerRequestSchema>;
export type ServerResponse = z.infer<typeof serverResponseSchema>;
export type ServerListResponse = z.infer<typeof serverListResponseSchema>;
export type ServerActionResponse = z.infer<typeof serverActionResponseSchema>;
