import { defineSocketContract } from 'redweb/contract';
import { z } from 'zod';

const room = z.string().regex(/^[a-z0-9-]{1,32}$/);
export const snapshot = z.object({
    board: z.array(z.enum(['X', 'O']).nullable()).length(9),
    players: z.array(z.string()).max(2), revision: z.number().int().min(0).max(9),
    result: z.enum(['X', 'O', 'draw']).nullable(), turn: z.enum(['X', 'O']), you: z.enum(['X', 'O']),
    online: z.array(z.string()).max(2), room,
}).strict();
export type Snapshot = z.infer<typeof snapshot>;

export const match = defineSocketContract('1', {
    join: z.object({ room }).strict(),
    resume: z.object({ room }).strict(),
    move: z.object({ cell: z.number().int().min(0).max(8), revision: z.number().int().min(0).max(9) }).strict(),
    state: snapshot,
    notice: z.object({ text: z.string() }).strict(),
});
