import { z } from 'zod'
import { textField, optionalTextField, optionalEmailField, upperField, optionalUpperField, optionalPhoneField } from '../../lib/zodValidation'

export const additionalDriverSchema = z.object({
  firstName:       textField('name'),
  lastName:        textField('name'),
  email:           optionalEmailField(),
  phone:           optionalPhoneField(),
  driverLicense:   upperField('driverLicenseNumber'),
  licenseExpiry:   z.string().datetime().optional(),
  licenseIssuedAt: z.string().datetime().optional(),
  dateOfBirth:     z.string().datetime().optional(),
  nationality:     optionalTextField('nationality'),
})

export const inspectionSchema = z.object({
  mileage:          z.number().int().optional(),
  fuelLevel:        z.enum(['FULL', 'SEVEN_EIGHTHS', 'THREE_QUARTERS', 'FIVE_EIGHTHS', 'HALF', 'THREE_EIGHTHS', 'QUARTER', 'ONE_EIGHTH', 'EMPTY']),
  fuelCharge:       z.number().int().min(0).optional(),
  generalCondition: z.string().optional(),
  employeeNotes:    z.string().optional(),
  customerAgreed:   z.boolean().default(false),
  damagePoints: z.array(z.object({
    viewType:      z.enum(['TOP', 'FRONT', 'REAR', 'LEFT', 'RIGHT']),
    x:             z.number(),
    y:             z.number(),
    damageType:    z.enum(['SCRATCH', 'DENT', 'CRACK', 'CHIP', 'MISSING', 'STAIN', 'OTHER']),
    severity:      z.enum(['MINOR', 'MODERATE', 'MAJOR']).default('MINOR'),
    description:   z.string().optional(),
    isPreExisting: z.boolean().default(false),
  })).default([]),
})

export const createSchema = z.object({
  vehicleId:      z.string().cuid(),
  customerId:     z.string().cuid(),
  startDate:      z.string().datetime(),
  endDate:        z.string().datetime(),
  pickupLocation: optionalTextField('pickupLocation'),
  returnLocation: optionalTextField('returnLocation'),
  offerId:        z.string().cuid().optional(),
  promoCodeUsed:  z.string().optional(),
  depositAmount:  z.number().int().min(0).default(0),
  paymentMode:    z.string().max(50).optional(),
  notes:          z.string().optional(),
  selectedInsurancePolicyIds: z.array(z.string()).default([]),
  additionalDrivers:          z.array(additionalDriverSchema).default([]),
})

export const updateSchema = z.object({
  startDate:          z.string().datetime().optional(),
  endDate:            z.string().datetime().optional(),
  pickupLocation:     optionalTextField('pickupLocation').nullable(),
  returnLocation:     optionalTextField('returnLocation').nullable(),
  depositAmount:      z.number().int().min(0).optional(),
  notes:              z.string().optional().nullable(),
  paymentMode:        z.string().max(50).optional().nullable(),
  damageChargeAmount: z.number().int().min(0).optional(),
  damageChargeNote:   z.string().optional().nullable(),
})

export const listQuerySchema = z.object({
  status:    z.string().optional(),
  vehicleId: z.string().optional(),
  source:    z.string().optional(),
  startDate: z.string().optional(),
  endDate:   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 checkinSchema     = z.object({ mileage: z.number().int().optional() })
export const checkoutSchema    = z.object({ mileage: z.number().int().optional() })
export const extendSchema      = z.object({ newEndDate: z.string().datetime(), reason: z.string().min(1) })
export const cancelSchema      = z.object({ reason: z.string().optional() })
export const approvalSchema    = z.object({ approved: z.boolean(), note: z.string().optional() })
export const inspectionTypeParam = z.enum(['CHECKIN', 'CHECKOUT'])

export const idParamSchema         = z.object({ id: z.string() })
export const driverParamSchema     = z.object({ id: z.string(), driverId: z.string() })
export const inspectionParamSchema = z.object({ id: z.string(), type: z.string() })
