/**
 * @ai-summary Request validation schemas for community stations API
 * @ai-context Uses Zod for runtime validation and type safety
 */

import { z } from 'zod';

export const submitCommunityStationSchema = z.object({
  name: z.string().min(1, 'Station name is required').max(200, 'Station name too long'),
  address: z.string().min(1, 'Address is required'),
  city: z.string().max(100, 'City too long').optional(),
  state: z.string().max(50, 'State too long').optional(),
  zipCode: z.string().max(20, 'Zip code too long').optional(),
  latitude: z.number().min(-90).max(90, 'Invalid latitude'),
  longitude: z.number().min(-180).max(180, 'Invalid longitude'),
  brand: z.string().max(100, 'Brand too long').optional(),
  has93Octane: z.boolean().optional().default(true),
  has93OctaneEthanolFree: z.boolean().optional().default(false),
  price93: z.number().min(0, 'Price must be positive').optional(),
  notes: z.string().optional()
});

export const reviewStationSchema = z.object({
  status: z.enum(['approved', 'rejected'], {
    errorMap: () => ({ message: 'Status must be either approved or rejected' })
  }),
  rejectionReason: z.string().optional()
});

export const communityStationFiltersSchema = z.object({
  status: z.enum(['pending', 'approved', 'rejected', 'removed']).optional(),
  submittedBy: z.string().optional(),
  limit: z.coerce.number().min(1).max(1000).default(100),
  offset: z.coerce.number().min(0).default(0)
});

export const nearbyStationsSchema = z.object({
  latitude: z.number().min(-90).max(90, 'Invalid latitude'),
  longitude: z.number().min(-180).max(180, 'Invalid longitude'),
  radiusKm: z.number().min(1).max(500, 'Radius must be between 1 and 500 km').optional().default(50)
});

export const stationIdSchema = z.object({
  id: z.string().uuid('Invalid station ID')
});

export const paginationSchema = z.object({
  limit: z.coerce.number().min(1).max(1000).default(100),
  offset: z.coerce.number().min(0).default(0)
});

export const boundsStationsSchema = z.object({
  north: z.number().min(-90).max(90, 'Invalid north latitude'),
  south: z.number().min(-90).max(90, 'Invalid south latitude'),
  east: z.number().min(-180).max(180, 'Invalid east longitude'),
  west: z.number().min(-180).max(180, 'Invalid west longitude')
});

export const removalReportSchema = z.object({
  reason: z.string().optional().default('No longer has Premium 93')
});

// Type exports for use in controllers and routes
export type SubmitCommunityStationInput = z.infer<typeof submitCommunityStationSchema>;
export type ReviewStationInput = z.infer<typeof reviewStationSchema>;
export type CommunityStationFiltersInput = z.infer<typeof communityStationFiltersSchema>;
export type NearbyStationsInput = z.infer<typeof nearbyStationsSchema>;
export type StationIdInput = z.infer<typeof stationIdSchema>;
export type PaginationInput = z.infer<typeof paginationSchema>;
export type BoundsStationsInput = z.infer<typeof boundsStationsSchema>;
export type RemovalReportInput = z.infer<typeof removalReportSchema>;
