import { z } from 'zod';

export const CreateCustomerSchema = z.object({
  name: z.string().min(1),
  phone: z.string().min(1),
  email: z.string().email().optional().or(z.literal('')).transform(v => v || null),
  address: z.string().optional().nullable(),
  notes: z.string().optional().nullable(),
  tier: z.enum(['none', 'silver', 'gold', 'bronze']).default('none'),
});

export const UpdateCustomerSchema = CreateCustomerSchema.partial();

export const CreateVehicleSchema = z.object({
  customerId: z.string().min(1),
  year: z.number().int().min(1900).max(2100),
  make: z.string().min(1),
  model: z.string().min(1),
  plate: z.string().min(1),
  vin: z.string().optional().nullable(),
  miles: z.number().int().min(0).default(0),
  color: z.string().optional().nullable(),
});

export const UpdateVehicleSchema = CreateVehicleSchema.partial().omit({ customerId: true });

export const CreateOrderSchema = z.object({
  customerId: z.string().min(1),
  vehicleId: z.string().min(1),
  assignedToId: z.string().optional().nullable(),
  mileageIn: z.number().int().min(0).optional(),
  notes: z.string().optional().nullable(),
});

export const UpdateOrderSchema = z.object({
  notes: z.string().optional().nullable(),
  mileageIn: z.number().int().min(0).optional(),
  discount: z.number().min(0).optional(),
  taxRate: z.number().min(0).max(1).optional(),
  assignedToId: z.string().optional().nullable(),
});

export const UpdateOrderStatusSchema = z.object({
  status: z.enum(['draft', 'in_progress', 'waiting_on_parts', 'ready', 'overdue', 'paid', 'closed']),
});

export const CreateLineItemSchema = z.object({
  kind: z.enum(['part', 'labor', 'custom']),
  description: z.string().min(1),
  qty: z.number().positive(),
  unitPrice: z.number().min(0),
  inventoryItemId: z.string().optional().nullable(),
});

export const UpdateLineItemSchema = CreateLineItemSchema.partial();

export const FinalizeOrderSchema = z.object({
  method: z.enum(['card', 'cash', 'invoice', 'bank_transfer']).default('card'),
});

export const CreateInventoryItemSchema = z.object({
  sku: z.string().min(1),
  name: z.string().min(1),
  category: z.string().min(1),
  stock: z.number().int().min(0).default(0),
  reorderLevel: z.number().int().min(0).default(0),
  cost: z.number().min(0),
  price: z.number().min(0),
});

export const UpdateInventoryItemSchema = CreateInventoryItemSchema.partial();

export const CreateUserSchema = z.object({
  name: z.string().min(1),
  email: z.string().email(),
  password: z.string().min(8),
  role: z.enum(['admin', 'staff', 'technician', 'accounts']),
  phone: z.string().optional().nullable(),
});

export const UpdateUserSchema = z.object({
  name: z.string().min(1).optional(),
  email: z.string().email().optional(),
  role: z.enum(['admin', 'staff', 'technician', 'accounts']).optional(),
  phone: z.string().optional().nullable(),
  active: z.boolean().optional(),
});

export const LoginSchema = z.object({
  email: z.string().email(),
  password: z.string().min(1),
});

export const UpdateShopSchema = z.object({
  name: z.string().min(1).optional(),
  address: z.string().optional(),
  phone: z.string().optional(),
  email: z.string().email().optional(),
  taxRate: z.number().min(0).max(1).optional(),
  laborRate: z.number().min(0).optional(),
  openHour: z.number().int().min(0).max(23).optional(),
  closeHour: z.number().int().min(1).max(24).optional(),
  slotCapacity: z.number().int().min(1).max(20).optional(),
});

export const PortalApproveSchema = z.object({
  itemIds: z.array(z.string()).min(1),
});

export const PortalDeclineSchema = z.object({
  reason: z.string().max(500).optional(),
});

const DateStr = z.string().regex(/^\d{4}-\d{2}-\d{2}$/, 'Expected YYYY-MM-DD');
const SlotStr = z.string().regex(/^\d{2}:\d{2}$/, 'Expected HH:MM');

export const CreateAppointmentSchema = z.object({
  serviceId: z.string().min(1),
  date: DateStr,
  slot: SlotStr,
  name: z.string().min(1).max(120),
  phone: z.string().min(7).max(30),
  email: z.string().email().optional().or(z.literal('')).transform(v => v || null),
  vehicleYear: z.number().int().min(1900).max(2100).optional().nullable(),
  vehicleMake: z.string().max(60).optional().nullable(),
  vehicleModel: z.string().max(60).optional().nullable(),
  vehicleId: z.string().optional().nullable(),
  notes: z.string().max(1000).optional().nullable(),
  portalToken: z.string().optional().nullable(),
});

export const UpdateAppointmentStatusSchema = z.object({
  status: z.enum(['confirmed', 'completed', 'cancelled']),
  cancelReason: z.string().max(500).optional().nullable(),
});

export const UpdateAppointmentSchema = z.object({
  date: DateStr.optional(),
  slot: SlotStr.optional(),
  internalNotes: z.string().max(2000).optional().nullable(),
  customerId: z.string().optional().nullable(),
  vehicleId: z.string().optional().nullable(),
});

export const CreateServiceSchema = z.object({
  slug: z.string().min(1).regex(/^[a-z0-9-]+$/),
  name: z.string().min(1),
  description: z.string().min(1),
  category: z.string().min(1),
  price: z.number().min(0),
  durationMin: z.number().int().min(15).max(480).default(60),
  active: z.boolean().default(true),
  sortOrder: z.number().int().default(0),
});

export const UpdateServiceSchema = CreateServiceSchema.partial();

export const DateRangeQuerySchema = z.object({
  from: DateStr.optional(),
  to: DateStr.optional(),
});

export const AvailabilityQuerySchema = z.object({
  from: DateStr.optional(),
  days: z.coerce.number().int().min(1).max(14).default(7),
});

export type CreateCustomerInput = z.infer<typeof CreateCustomerSchema>;
export type UpdateCustomerInput = z.infer<typeof UpdateCustomerSchema>;
export type CreateVehicleInput = z.infer<typeof CreateVehicleSchema>;
export type UpdateVehicleInput = z.infer<typeof UpdateVehicleSchema>;
export type CreateOrderInput = z.infer<typeof CreateOrderSchema>;
export type UpdateOrderInput = z.infer<typeof UpdateOrderSchema>;
export type CreateLineItemInput = z.infer<typeof CreateLineItemSchema>;
export type UpdateLineItemInput = z.infer<typeof UpdateLineItemSchema>;
export type FinalizeOrderInput = z.infer<typeof FinalizeOrderSchema>;
export type CreateInventoryItemInput = z.infer<typeof CreateInventoryItemSchema>;
export type UpdateInventoryItemInput = z.infer<typeof UpdateInventoryItemSchema>;
export type CreateUserInput = z.infer<typeof CreateUserSchema>;
export type UpdateUserInput = z.infer<typeof UpdateUserSchema>;
export type LoginInput = z.infer<typeof LoginSchema>;
export type UpdateShopInput = z.infer<typeof UpdateShopSchema>;
export type CreateAppointmentInput = z.infer<typeof CreateAppointmentSchema>;
export type UpdateAppointmentStatusInput = z.infer<typeof UpdateAppointmentStatusSchema>;
export type UpdateAppointmentInput = z.infer<typeof UpdateAppointmentSchema>;
export type CreateServiceInput = z.infer<typeof CreateServiceSchema>;
export type UpdateServiceInput = z.infer<typeof UpdateServiceSchema>;
