/**
 * Mock data for tests
 */

// Mock user data
export const mockUser = {
  id: '1',
  username: 'testuser',
  email: 'test@example.com',
  firstName: 'Test',
  lastName: 'User',
  isVerified: true,
  role: 'user' as const,
  addresses: [],
};

export const mockUnverifiedUser = {
  ...mockUser,
  id: '2',
  email: 'unverified@example.com',
  isVerified: false,
};

// Mock item data
export const mockItem = {
  id: '1',
  name: 'Test Item',
  description: 'A test item for rental',
  pricePerDay: 25,
  replacementCost: 500,
  condition: 'excellent' as const,
  isAvailable: true,
  images: ['image1.jpg'],
  ownerId: '2',
  city: 'Test City',
  state: 'California',
  createdAt: new Date().toISOString(),
  updatedAt: new Date().toISOString(),
};

// Mock rental data
export const mockRental = {
  id: '1',
  itemId: '1',
  renterId: '1',
  ownerId: '2',
  startDateTime: new Date().toISOString(),
  endDateTime: new Date(Date.now() + 86400000).toISOString(),
  totalAmount: 25,
  status: 'pending' as const,
  paymentStatus: 'pending' as const,
  createdAt: new Date().toISOString(),
  updatedAt: new Date().toISOString(),
};

// Mock API response helpers
export const createMockResponse = <T>(data: T, status = 200) => ({
  data,
  status,
  statusText: 'OK',
  headers: {},
  config: {},
});

export const createMockError = (message: string, status: number, code?: string) => {
  const error = new Error(message) as any;
  error.response = {
    status,
    data: { message, code },
  };
  return error;
};

// Extended mock user data for testing
export const mockAdminUser = {
  ...mockUser,
  id: '3',
  email: 'admin@example.com',
  role: 'admin' as const,
};

// Mock Item factory with all fields
export const createMockItem = (overrides: Partial<typeof mockItem> = {}) => ({
  ...mockItem,
  id: overrides.id || String(Math.floor(Math.random() * 1000)),
  ...overrides,
});

// Full mock item with owner details
export const mockItemWithOwner = {
  ...mockItem,
  owner: {
    id: '2',
    firstName: 'Owner',
    lastName: 'User',
    email: 'owner@example.com',
  },
  pricePerHour: 10,
  pricePerWeek: 150,
  pricePerMonth: 500,
  latitude: 37.7749,
  longitude: -122.4194,
  address1: '123 Test St',
  city: 'Test City',
  state: 'California',
  zipCode: '94102',
  availableAfter: '09:00',
  availableBefore: '21:00',
  specifyTimesPerDay: false,
  weeklyTimes: null,
  rules: 'Test rules',
  imageFilenames: ['items/test-image-1.jpg', 'items/test-image-2.jpg'],
};

// Mock Rental factory with all fields
export const createMockRental = (overrides: Partial<typeof mockRental & {
  item?: typeof mockItem;
  renter?: typeof mockUser;
  owner?: typeof mockUser;
  displayStatus?: string;
  paymentStatus?: string;
  declineReason?: string;
  refundAmount?: number;
  intendedUse?: string;
}> = {}) => ({
  ...mockRental,
  id: overrides.id || String(Math.floor(Math.random() * 1000)),
  item: overrides.item || mockItemWithOwner,
  renter: overrides.renter || mockUser,
  owner: overrides.owner || { ...mockUser, id: '2' },
  displayStatus: overrides.displayStatus || overrides.status || 'pending',
  ...overrides,
});

// Mock rentals with different statuses for testing
export const mockPendingRental = createMockRental({
  id: '1',
  status: 'pending',
  displayStatus: 'pending',
  paymentStatus: 'pending',
});

export const mockConfirmedRental = createMockRental({
  id: '2',
  status: 'confirmed',
  displayStatus: 'confirmed',
  paymentStatus: 'paid',
});

export const mockActiveRental = createMockRental({
  id: '3',
  status: 'confirmed',
  displayStatus: 'active',
  paymentStatus: 'paid',
});

export const mockCompletedRental = createMockRental({
  id: '4',
  status: 'completed',
  displayStatus: 'completed',
  paymentStatus: 'paid',
});

export const mockDeclinedRental = createMockRental({
  id: '5',
  status: 'declined',
  displayStatus: 'declined',
  declineReason: 'Item not available during requested period',
});

// Mock Condition Check factory
export const createMockConditionCheck = (overrides: {
  id?: string;
  rentalId?: string;
  checkType?: string;
  status?: string;
  images?: string[];
  notes?: string;
  createdAt?: string;
} = {}) => ({
  id: overrides.id || String(Math.floor(Math.random() * 1000)),
  rentalId: overrides.rentalId || '1',
  checkType: overrides.checkType || 'pre_rental_owner',
  status: overrides.status || 'completed',
  images: overrides.images || ['condition/check-1.jpg'],
  notes: overrides.notes || 'Item in good condition',
  createdAt: overrides.createdAt || new Date().toISOString(),
});

// Mock Address factory
export const createMockAddress = (overrides: {
  id?: string;
  address1?: string;
  address2?: string;
  city?: string;
  state?: string;
  zipCode?: string;
  country?: string;
  latitude?: number;
  longitude?: number;
  isPrimary?: boolean;
} = {}) => ({
  id: overrides.id || String(Math.floor(Math.random() * 1000)),
  address1: overrides.address1 || '123 Main St',
  address2: overrides.address2 || '',
  city: overrides.city || 'San Francisco',
  state: overrides.state || 'CA',
  zipCode: overrides.zipCode || '94102',
  country: overrides.country || 'US',
  latitude: overrides.latitude || 37.7749,
  longitude: overrides.longitude || -122.4194,
  isPrimary: overrides.isPrimary ?? true,
});

// Mock paginated response helper
export const createMockPaginatedResponse = <T>(
  items: T[],
  page = 1,
  totalPages = 1,
  totalItems?: number
) => ({
  items,
  page,
  totalPages,
  totalItems: totalItems ?? items.length,
});

// Mock available checks response
export const createMockAvailableChecks = (rentalId: string, checkTypes: string[] = ['pre_rental_owner']) => ({
  availableChecks: checkTypes.map(checkType => ({
    rentalId,
    checkType,
  })),
});
