import { z } from 'zod';

const sourcesSchema = z.object({
  osm: z.boolean().default(false),
  nar: z.union([
    z.boolean(),
    z.object({
      residentialOnly: z.boolean().default(true),
    }),
  ]).default(false),
  reverseGeocode: z.union([
    z.boolean(),
    z.object({
      gridSpacingMeters: z.number().min(20).max(500).default(100),
      maxPoints: z.number().min(10).max(2000).default(500),
    }),
  ]).default(false),
});

export const areaImportPreviewSchema = z.object({
  areaType: z.enum(['cut', 'viewport']),
  cutId: z.string().optional(),
  center: z.object({ lat: z.number(), lng: z.number() }).optional(),
  zoom: z.number().optional(),
  viewportWidth: z.number().optional(),
  viewportHeight: z.number().optional(),
  sources: sourcesSchema,
}).refine(
  (data) => {
    if (data.areaType === 'cut') return !!data.cutId;
    if (data.areaType === 'viewport') return data.center && data.zoom !== undefined;
    return false;
  },
  { message: 'Cut ID required for cut area type, center+zoom required for viewport type' },
);

export const areaImportStartSchema = z.object({
  areaType: z.enum(['cut', 'viewport']),
  cutId: z.string().optional(),
  center: z.object({ lat: z.number(), lng: z.number() }).optional(),
  zoom: z.number().optional(),
  viewportWidth: z.number().optional(),
  viewportHeight: z.number().optional(),
  sources: sourcesSchema,
  deduplicateRadius: z.number().min(0).max(100).default(5),
  batchSize: z.number().int().min(100).max(5000).default(1000),
}).refine(
  (data) => {
    if (data.areaType === 'cut') return !!data.cutId;
    if (data.areaType === 'viewport') return data.center && data.zoom !== undefined;
    return false;
  },
  { message: 'Cut ID required for cut area type, center+zoom required for viewport type' },
);

export type AreaImportPreviewInput = z.infer<typeof areaImportPreviewSchema>;
export type AreaImportStartInput = z.infer<typeof areaImportStartSchema>;
