import { z } from 'zod'
import { sanitizeAndFormat } from '../../lib/inputValidation'
import { carTextField, upperField, optionalUpperField } from '../../lib/zodValidation'

export const vehicleSchema = z.object({
  make:         carTextField('carMark'),
  model:        carTextField('carModel'),
  year:         z.number().int().min(1990).max(new Date().getFullYear() + 1),
  color:        z.string().optional().default('').transform((v: string) => v ? sanitizeAndFormat(v, 'carColor') : ''),
  licensePlate: upperField('licensePlate'),
  vin:          optionalUpperField('vin'),
  category:     z.enum(['ECONOMY', 'COMPACT', 'MIDSIZE', 'FULLSIZE', 'SUV', 'LUXURY', 'VAN', 'TRUCK']),
  seats:        z.number().int().min(1).max(20).default(5),
  transmission: z.enum(['AUTOMATIC', 'MANUAL']).default('AUTOMATIC'),
  fuelType:     z.enum(['GASOLINE', 'DIESEL', 'ELECTRIC', 'HYBRID']).default('GASOLINE'),
  features:     z.array(z.string()).default([]),
  dailyRate:    z.number().int().min(0).optional(),
  mileage:      z.number().int().optional(),
  notes:        z.string().optional(),
  isPublished:  z.boolean().default(true),
  status:       z.enum(['AVAILABLE', 'RESERVED', 'READY', 'RENTED', 'RETURNED', 'NEEDS_CLEANING', 'MAINTENANCE', 'DAMAGE_REVIEW', 'OUT_OF_SERVICE']).optional(),
  pickupLocations: z.array(z.string().trim().min(1)).default([]),
  allowDifferentDropoff: z.boolean().default(false),
  dropoffLocations: z.array(z.string().trim().min(1)).default([]),
})

export const listQuerySchema = z.object({
  status:   z.string().optional(),
  category: z.string().optional(),
  published: z.string().optional(),
  page:     z.coerce.number().int().min(1).default(1),
  pageSize: z.coerce.number().int().min(1).max(100).default(20),
})

export const availabilityQuerySchema = z.object({
  startDate: z.string(),
  endDate:   z.string(),
})

export const calendarQuerySchema = z.object({
  year:  z.coerce.number().int(),
  month: z.coerce.number().int().min(1).max(12),
})

export const calendarBlockSchema = z.object({
  startDate: z.string().datetime({ offset: true }).or(z.string().regex(/^\d{4}-\d{2}-\d{2}$/)),
  endDate:   z.string().datetime({ offset: true }).or(z.string().regex(/^\d{4}-\d{2}-\d{2}$/)),
  reason:    z.string().optional(),
  type:      z.enum(['MANUAL', 'MAINTENANCE']).default('MANUAL'),
})

export const maintenanceLogSchema = z.object({
  type:           z.string().min(1),
  description:    z.string().optional(),
  cost:           z.number().int().optional(),
  mileage:        z.number().int().optional(),
  performedAt:    z.string().datetime(),
  nextDueAt:      z.string().datetime().optional(),
  nextDueMileage: z.number().int().optional(),
})

const nullableRateSchema = z.number().int().min(0).nullable().optional()

export const pricingConfigSchema = z.object({
  pricingMode: z.enum(['MANUAL', 'AUTOMATIC']),
  baseDailyRate: z.number().int().min(0),
  weeklyRate: nullableRateSchema,
  weekendRate: nullableRateSchema,
  holidayRate: nullableRateSchema,
  monthlyRate: nullableRateSchema,
  longTermDailyRate: nullableRateSchema,
  minimumDailyRate: nullableRateSchema,
  maximumDailyRate: nullableRateSchema,
  maxDailyPriceMovementPct: z.number().int().min(0).max(100).default(10),
  automaticPricingEnabled: z.boolean().default(false),
  approvalRequired: z.boolean().default(false),
  note: z.string().trim().min(1).max(500).optional(),
})

export const pricingRuleSchema = z.object({
  name: z.string().trim().min(1).max(120),
  ruleType: z.enum(['DATE_RANGE', 'SEASONAL', 'HOLIDAY', 'WEEKEND', 'SPECIAL_EVENT', 'MANUAL_OVERRIDE']).default('DATE_RANGE'),
  startDate: z.string().datetime({ offset: true }).or(z.string().regex(/^\d{4}-\d{2}-\d{2}$/)),
  endDate: z.string().datetime({ offset: true }).or(z.string().regex(/^\d{4}-\d{2}-\d{2}$/)),
  dailyRate: nullableRateSchema,
  weeklyRate: nullableRateSchema,
  weekendRate: nullableRateSchema,
  monthlyRate: nullableRateSchema,
  minDailyRate: nullableRateSchema,
  maxDailyRate: nullableRateSchema,
  automaticAdjustmentPct: z.number().int().min(-100).max(200).nullable().optional(),
  priceAdjustment: z.number().int().nullable().optional(),
  isActive: z.boolean().default(true),
  sortOrder: z.number().int().min(0).max(1000).default(0),
})

export const publishSchema = z.object({ isPublished: z.boolean() })
export const statusSchema  = z.object({ status: z.enum(['AVAILABLE', 'RESERVED', 'READY', 'RENTED', 'RETURNED', 'NEEDS_CLEANING', 'MAINTENANCE', 'DAMAGE_REVIEW', 'OUT_OF_SERVICE']) })

export const idParamSchema    = z.object({ id: z.string() })
export const photoIdxSchema   = z.object({ id: z.string(), idx: z.string() })
export const blockIdParamSchema = z.object({ id: z.string(), blockId: z.string() })
export const pricingRuleParamSchema = z.object({ id: z.string(), ruleId: z.string() })
