import { z } from 'zod';

const editorModeEnum = z.enum(['VISUAL', 'CODE']);
const mkdocsExportModeEnum = z.enum(['THEMED', 'STANDALONE']);

export const createLandingPageSchema = z.object({
  title: z.string().min(1, 'Title is required'),
  description: z.string().optional(),
  editorMode: editorModeEnum.optional().default('VISUAL'),
  blocks: z.any().optional().default({}),
  htmlOutput: z.string().optional(),
  cssOutput: z.string().optional(),
  mkdocsPath: z.string().optional(),
  mkdocsExportMode: mkdocsExportModeEnum.optional().default('THEMED'),
  mkdocsHideNav: z.boolean().optional().default(true),
  mkdocsHideToc: z.boolean().optional().default(true),
  mkdocsSkipExport: z.boolean().optional().default(false),
  published: z.boolean().optional().default(false),
  listed: z.boolean().optional().default(false),
  seoTitle: z.string().optional(),
  seoDescription: z.string().optional(),
  seoImage: z.string().optional(),
});

export const updateLandingPageSchema = z.object({
  title: z.string().min(1).optional(),
  description: z.string().nullable().optional(),
  editorMode: editorModeEnum.optional(),
  blocks: z.any().optional(),
  htmlOutput: z.string().nullable().optional(),
  cssOutput: z.string().nullable().optional(),
  mkdocsPath: z.string().nullable().optional(),
  mkdocsExportMode: mkdocsExportModeEnum.optional(),
  mkdocsHideNav: z.boolean().optional(),
  mkdocsHideToc: z.boolean().optional(),
  mkdocsSkipExport: z.boolean().optional(),
  published: z.boolean().optional(),
  listed: z.boolean().optional(),
  seoTitle: z.string().nullable().optional(),
  seoDescription: z.string().nullable().optional(),
  seoImage: z.string().nullable().optional(),
});

export const listLandingPagesSchema = z.object({
  page: z.coerce.number().int().positive().default(1),
  limit: z.coerce.number().int().positive().max(100).default(20),
  search: z.string().optional(),
  published: z.enum(['true', 'false']).optional(),
});

export const createPageBlockSchema = z.object({
  type: z.string().min(1, 'Type is required'),
  label: z.string().min(1, 'Label is required'),
  schema: z.any().optional().default({}),
  defaults: z.any().optional().default({}),
  thumbnail: z.string().optional(),
  category: z.string().optional(),
  sortOrder: z.number().int().optional().default(0),
});

export const updatePageBlockSchema = z.object({
  type: z.string().min(1).optional(),
  label: z.string().min(1).optional(),
  schema: z.any().optional(),
  defaults: z.any().optional(),
  thumbnail: z.string().nullable().optional(),
  category: z.string().nullable().optional(),
  sortOrder: z.number().int().optional(),
});

export const listPageBlocksSchema = z.object({
  category: z.string().optional(),
});

export const pageIdSchema = z.object({
  id: z.string().min(1),
});

export type CreateLandingPageInput = z.infer<typeof createLandingPageSchema>;
export type UpdateLandingPageInput = z.infer<typeof updateLandingPageSchema>;
export type ListLandingPagesInput = z.infer<typeof listLandingPagesSchema>;
export type CreatePageBlockInput = z.infer<typeof createPageBlockSchema>;
export type UpdatePageBlockInput = z.infer<typeof updatePageBlockSchema>;
export type ListPageBlocksInput = z.infer<typeof listPageBlocksSchema>;
