export type VehicleType = 'CAR' | 'SUV' | 'VAN' | 'TRUCK' | 'MOTORCYCLE' | 'BUS' | 'LUXURY' | 'ELECTRIC';
export type FuelType = 'PETROL' | 'DIESEL' | 'ELECTRIC' | 'HYBRID';
export type Transmission = 'AUTOMATIC' | 'MANUAL';
export type RentalStatus = 'pending' | 'confirmed' | 'active' | 'completed' | 'cancelled' | 'overdue';

export interface Vehicle {
  _id: string;
  organizationId: string;
  userId: string;
  make: string;
  model: string;
  year: number;
  type: VehicleType;
  color?: string;
  licensePlate?: string;
  seats?: number;
  fuelType?: FuelType;
  transmission?: Transmission;
  pricePerDay: number;        // in fils/cents (multiply by 100 from QAR)
  pricePerWeek?: number;
  pricePerMonth?: number;
  currency: string;
  depositAmount?: number;
  descriptionEn?: string;
  descriptionAr?: string;
  features?: string[];
  isAvailable: boolean;
  isActive: boolean;
  totalRentals: number;
  totalRevenue: number;
  createdAt: number;
  updatedAt: number;
}

export interface VehicleRental {
  _id: string;
  organizationId: string;
  vehicleId: string;
  customerName: string;
  customerPhone: string;
  customerEmail?: string;
  customerLicenseNumber?: string;
  pickupDate: number;           // Unix ms timestamp
  returnDate: number;
  actualReturnDate?: number;
  pickupLocation?: string;
  dropoffLocation?: string;
  durationDays: number;
  dailyRate: number;
  totalAmount: number;
  depositAmount?: number;
  depositPaid: boolean;
  amountPaid: number;
  currency: string;
  status: RentalStatus;
  mileageAtPickup?: number;
  mileageAtReturn?: number;
  fuelLevelAtPickup?: string;
  fuelLevelAtReturn?: string;
  damageNotes?: string;
  notes?: string;
  source?: string;
  createdAt: number;
  updatedAt: number;
  // Joined (from API response)
  vehicle?: Vehicle;
}

/** Calculate rental price based on duration and vehicle rates */
export function calculateRentalPrice(
  vehicle: Vehicle,
  durationDays: number,
): { dailyRate: number; totalAmount: number } {
  // Use monthly rate if duration >= 28 days and rate exists
  if (durationDays >= 28 && vehicle.pricePerMonth) {
    const months = durationDays / 30;
    const totalAmount = Math.round(months * vehicle.pricePerMonth);
    return { dailyRate: Math.round(totalAmount / durationDays), totalAmount };
  }
  // Use weekly rate if duration >= 7 days and rate exists
  if (durationDays >= 7 && vehicle.pricePerWeek) {
    const weeks = Math.floor(durationDays / 7);
    const remainingDays = durationDays % 7;
    const totalAmount = weeks * vehicle.pricePerWeek + remainingDays * vehicle.pricePerDay;
    return { dailyRate: Math.round(totalAmount / durationDays), totalAmount };
  }
  // Daily rate
  return { dailyRate: vehicle.pricePerDay, totalAmount: vehicle.pricePerDay * durationDays };
}

/** Format price from fils/cents to display (e.g. 15000 -> "150.00") */
export function formatVehiclePrice(amount: number, currency = 'QAR'): string {
  return `${currency} ${(amount / 100).toFixed(2)}`;
}

/** Vehicle type display labels */
export const VEHICLE_TYPE_LABELS: Record<VehicleType, { en: string; ar: string }> = {
  CAR: { en: 'Car', ar: 'سيارة' },
  SUV: { en: 'SUV', ar: 'سيارة دفع رباعي' },
  VAN: { en: 'Van', ar: 'فان' },
  TRUCK: { en: 'Truck', ar: 'شاحنة' },
  MOTORCYCLE: { en: 'Motorcycle', ar: 'دراجة نارية' },
  BUS: { en: 'Bus', ar: 'حافلة' },
  LUXURY: { en: 'Luxury', ar: 'فاخر' },
  ELECTRIC: { en: 'Electric', ar: 'كهربائي' },
};

export const RENTAL_STATUS_CONFIG: Record<RentalStatus, { label: { en: string; ar: string }; color: string }> = {
  pending:   { label: { en: 'Pending',   ar: 'معلق'    }, color: 'bg-yellow-100 text-yellow-700' },
  confirmed: { label: { en: 'Confirmed', ar: 'مؤكد'    }, color: 'bg-blue-100 text-blue-700' },
  active:    { label: { en: 'Active',    ar: 'نشط'     }, color: 'bg-green-100 text-green-700' },
  completed: { label: { en: 'Completed', ar: 'مكتمل'   }, color: 'bg-gray-100 text-gray-700' },
  cancelled: { label: { en: 'Cancelled', ar: 'ملغى'    }, color: 'bg-red-100 text-red-700' },
  overdue:   { label: { en: 'Overdue',   ar: 'متأخر'   }, color: 'bg-orange-100 text-orange-700' },
};
