/**
 * @ai-summary Type definitions for maintenance feature
 * @ai-context Supports three categories with specific subtypes, multiple selections allowed
 */

// Category types
export type MaintenanceCategory = 'routine_maintenance' | 'repair' | 'performance_upgrade';

// Schedule types
export type ScheduleType = 'interval' | 'fixed_date' | 'time_since_last';

// Subtype definitions (constants for validation)
export const ROUTINE_MAINTENANCE_SUBTYPES = [
  'Accelerator Pedal',
  'Air Filter Element',
  'Brakes and Traction Control',
  'Cabin Air Filter / Purifier',
  'Coolant',
  'Doors',
  'Drive Belt',
  'Engine Oil',
  'Evaporative Emissions System',
  'Exhaust System',
  'Fluid - A/T',
  'Fluid - Differential',
  'Fluid - M/T',
  'Fluid Filter - A/T',
  'Fluids',
  'Fuel Delivery and Air Induction',
  'Hood Shock / Support',
  'Neutral Safety Switch',
  'Parking Brake System',
  'Restraints and Safety Systems',
  'Shift Interlock, A/T',
  'Spark Plug',
  'Steering and Suspension',
  'Tires',
  'Trunk / Liftgate Shock / Support',
  'Washer Fluid',
  'Wiper Blade'
] as const;

export const REPAIR_SUBTYPES = [
  'Engine',
  'Transmission',
  'Drivetrain',
  'Exterior',
  'Interior'
] as const;

export const PERFORMANCE_UPGRADE_SUBTYPES = [
  'Engine',
  'Drivetrain',
  'Suspension',
  'Wheels/Tires',
  'Exterior'
] as const;

// Database record types (camelCase)
export interface MaintenanceRecord {
  id: string;
  userId: string;
  vehicleId: string;
  category: MaintenanceCategory;
  subtypes: string[];
  date: string;
  odometerReading?: number;
  cost?: number;
  shopName?: string;
  notes?: string;
  receiptDocumentId?: string | null;
  createdAt: string;
  updatedAt: string;
}

export interface MaintenanceSchedule {
  id: string;
  userId: string;
  vehicleId: string;
  category: MaintenanceCategory;
  subtypes: string[];
  scheduleType: ScheduleType;
  intervalMonths?: number;
  intervalMiles?: number;
  fixedDueDate?: string | null;
  lastServiceDate?: string;
  lastServiceMileage?: number;
  nextDueDate?: string;
  nextDueMileage?: number;
  isActive: boolean;
  emailNotifications?: boolean;
  reminderDays1?: number | null;
  reminderDays2?: number | null;
  reminderDays3?: number | null;
  createdAt: string;
  updatedAt: string;
}

// Request types (camelCase)
export interface CreateMaintenanceRecordRequest {
  vehicleId: string;
  category: MaintenanceCategory;
  subtypes: string[];
  date: string;
  odometerReading?: number;
  cost?: number;
  shopName?: string;
  notes?: string;
  receiptDocumentId?: string;
}

// Receipt document metadata returned on GET
export interface ReceiptDocumentMeta {
  documentId: string;
  fileName: string;
  contentType: string;
  storageKey: string;
}

export interface UpdateMaintenanceRecordRequest {
  category?: MaintenanceCategory;
  subtypes?: string[];
  date?: string;
  odometerReading?: number | null;
  cost?: number | null;
  shopName?: string | null;
  notes?: string | null;
}

export interface CreateScheduleRequest {
  vehicleId: string;
  category: MaintenanceCategory;
  subtypes: string[];
  scheduleType?: ScheduleType;
  intervalMonths?: number;
  intervalMiles?: number;
  fixedDueDate?: string;
  emailNotifications?: boolean;
  reminderDays1?: number;
  reminderDays2?: number;
  reminderDays3?: number;
}

export interface UpdateScheduleRequest {
  category?: MaintenanceCategory;
  subtypes?: string[];
  scheduleType?: ScheduleType;
  intervalMonths?: number | null;
  intervalMiles?: number | null;
  fixedDueDate?: string | null;
  isActive?: boolean;
  emailNotifications?: boolean;
  reminderDays1?: number | null;
  reminderDays2?: number | null;
  reminderDays3?: number | null;
}

// Response types (camelCase)
export interface MaintenanceRecordResponse extends MaintenanceRecord {
  subtypeCount: number;
  receiptDocument?: ReceiptDocumentMeta | null;
}

export interface MaintenanceScheduleResponse extends MaintenanceSchedule {
  subtypeCount: number;
  isDueSoon?: boolean;
  isOverdue?: boolean;
}

// Validation helpers
export function getSubtypesForCategory(category: MaintenanceCategory): readonly string[] {
  switch (category) {
    case 'routine_maintenance': return ROUTINE_MAINTENANCE_SUBTYPES;
    case 'repair': return REPAIR_SUBTYPES;
    case 'performance_upgrade': return PERFORMANCE_UPGRADE_SUBTYPES;
  }
}

export function validateSubtypes(category: MaintenanceCategory, subtypes: string[]): boolean {
  if (!subtypes || subtypes.length === 0) return false;
  const validSubtypes = getSubtypesForCategory(category);
  return subtypes.every(st => validSubtypes.includes(st as any));
}

export function getCategoryDisplayName(category: MaintenanceCategory): string {
  switch (category) {
    case 'routine_maintenance': return 'Routine Maintenance';
    case 'repair': return 'Repair';
    case 'performance_upgrade': return 'Performance Upgrade';
  }
}
