// Mock data for Karibeo API simulation
export const mockUsers = [
  {
    id: '1',
    email: 'usuario@karibeo.com',
    password: '123456',
    name: 'Juan Pérez',
    type: 'tourist',
    avatar: 'https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=150',
    location: { lat: 18.4861, lng: -69.9312 }, // Santo Domingo
    preferences: { language: 'es' }
  },
  {
    id: '2',
    email: 'comercio@karibeo.com',
    password: '123456',
    name: 'María García',
    type: 'business',
    avatar: 'https://images.unsplash.com/photo-1494790108755-2616b612b829?w=150',
    location: { lat: 18.5204, lng: -69.9055 }, // Santo Domingo
    preferences: { language: 'es' }
  }
];

export const mockListings = [
  {
    id: '1',
    title: 'Hotel Colonial Boutique',
    category: 'hotel',
    location: { lat: 18.4861, lng: -69.9312, address: 'Zona Colonial, Santo Domingo' },
    price: 120,
    rating: 4.8,
    images: ['https://images.unsplash.com/photo-1566073771259-6a8506099945?w=400'],
    description: 'Hermoso hotel boutique en el corazón de la Zona Colonial',
    amenities: ['WiFi', 'Desayuno', 'Piscina', 'Spa'],
    ownerId: '2'
  },
  {
    id: '2',
    title: 'Restaurante El Malecón',
    category: 'restaurant',
    location: { lat: 18.4692, lng: -69.9014, address: 'Malecón, Santo Domingo' },
    price: 45,
    rating: 4.6,
    images: ['https://images.unsplash.com/photo-1414235077428-338989a2e8c0?w=400'],
    description: 'Exquisita comida dominicana con vista al mar',
    amenities: ['Vista al mar', 'Terraza', 'Música en vivo'],
    ownerId: '2'
  },
  {
    id: '3',
    title: 'Tour Guiado Zona Colonial',
    category: 'tour',
    location: { lat: 18.4861, lng: -69.9312, address: 'Plaza de Armas, Santo Domingo' },
    price: 25,
    rating: 4.9,
    images: ['https://images.unsplash.com/photo-1539650116574-75c0c6d73c1e?w=400'],
    description: 'Descubre la historia de la primera ciudad de América',
    amenities: ['Guía certificado', 'Grupo pequeño', 'Incluye entradas'],
    ownerId: '2'
  },
  {
    id: '4',
    title: 'Resort Playa Bávaro',
    category: 'hotel',
    location: { lat: 18.6813, lng: -68.4090, address: 'Playa Bávaro, Punta Cana' },
    price: 280,
    rating: 4.7,
    images: ['https://images.unsplash.com/photo-1571003123894-1f0594d2b5d9?w=400'],
    description: 'Resort todo incluido frente al mar Caribe',
    amenities: ['Todo incluido', 'Playa privada', 'Spa', 'Golf'],
    ownerId: '2'
  },
  {
    id: '5',
    title: 'Excursión Isla Saona',
    category: 'tour',
    location: { lat: 18.1667, lng: -68.8000, address: 'Isla Saona, La Romana' },
    price: 85,
    rating: 4.8,
    images: ['https://images.unsplash.com/photo-1559827260-dc66d52bef19?w=400'],
    description: 'Día completo en la paradisíaca Isla Saona',
    amenities: ['Transporte incluido', 'Almuerzo', 'Snorkel', 'Guía'],
    ownerId: '2'
  },
  {
    id: '6',
    title: 'Casa Colonial Airbnb',
    category: 'apartment',
    location: { lat: 18.4756, lng: -69.9390, address: 'Gazcue, Santo Domingo' },
    price: 60,
    rating: 4.5,
    images: ['https://images.unsplash.com/photo-1502672260266-1c1ef2d93688?w=400'],
    description: 'Acogedora casa colonial en zona histórica',
    amenities: ['WiFi', 'Cocina', 'Aire acondicionado', 'Jardín'],
    ownerId: '2'
  },
  {
    id: '7',
    title: 'Restaurante Lucia',
    category: 'restaurant',
    location: { lat: 18.4853, lng: -69.9314, address: 'Calle Hostos, Zona Colonial' },
    price: 65,
    rating: 4.9,
    images: ['https://images.unsplash.com/photo-1517248135467-4c7edcad34c4?w=400'],
    description: 'Restaurante gourmet en casa colonial del siglo XVI',
    amenities: ['Terraza', 'Wine bar', 'Chef internacional', 'Ambiente histórico'],
    ownerId: '2'
  },
  {
    id: '8',
    title: 'Apartamento Moderno Piantini',
    category: 'apartment',
    location: { lat: 18.4736, lng: -69.9506, address: 'Piantini, Santo Domingo' },
    price: 95,
    rating: 4.6,
    images: ['https://images.unsplash.com/photo-1522708323590-d24dbb6b0267?w=400'],
    description: 'Moderno apartamento en zona exclusiva',
    amenities: ['Piscina', 'Gimnasio', 'Seguridad 24h', 'Vista panorámica'],
    ownerId: '2'
  }
];

export const mockBookings = [
  {
    id: '1',
    listingId: '1',
    userId: '1',
    checkIn: '2024-02-15',
    checkOut: '2024-02-18',
    guests: 2,
    total: 360,
    status: 'confirmed',
    paymentMethod: 'paypal'
  },
  {
    id: '2',
    listingId: '2',
    userId: '1',
    date: '2024-02-20',
    guests: 4,
    total: 180,
    status: 'pending',
    paymentMethod: 'credit_card'
  }
];

export const mockWalletData = {
  balance: 1250.50,
  transactions: [
    {
      id: '1',
      type: 'income',
      amount: 120,
      description: 'Pago por reserva Hotel Colonial',
      date: '2024-02-10',
      status: 'completed'
    },
    {
      id: '2',
      type: 'expense',
      amount: -45,
      description: 'Comisión plataforma',
      date: '2024-02-10',
      status: 'completed'
    },
    {
      id: '3',
      type: 'income',
      amount: 180,
      description: 'Pago restaurante El Malecón',
      date: '2024-02-12',
      status: 'pending'
    }
  ]
};

// Simulate API calls with promises
export const mockApi = {
  // Authentication
  login: async (email: string, password: string) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const user = mockUsers.find(u => u.email === email && u.password === password);
        if (user) {
          const { password: _, ...userWithoutPassword } = user;
          resolve({ user: userWithoutPassword, token: 'mock-jwt-token' });
        } else {
          reject(new Error('Credenciales inválidas'));
        }
      }, 1000);
    });
  },

  register: async (userData: any) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const newUser = {
          id: String(mockUsers.length + 1),
          ...userData,
          avatar: 'https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=150'
        };
        mockUsers.push(newUser);
        const { password: _, ...userWithoutPassword } = newUser;
        resolve({ user: userWithoutPassword, token: 'mock-jwt-token' });
      }, 1000);
    });
  },

  // Listings
  getListings: async (location?: { lat: number, lng: number }, radius = 10) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        let filteredListings = mockListings;
        
        if (location) {
          // Simulate distance filtering (simplified)
          filteredListings = mockListings.filter(listing => {
            const distance = Math.sqrt(
              Math.pow(listing.location.lat - location.lat, 2) + 
              Math.pow(listing.location.lng - location.lng, 2)
            ) * 111; // Rough km conversion
            return distance <= radius;
          });
        }
        
        resolve(filteredListings);
      }, 800);
    });
  },

  getUserListings: async (userId: string) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const userListings = mockListings.filter(listing => listing.ownerId === userId);
        resolve(userListings);
      }, 500);
    });
  },

  createListing: async (listingData: any) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const newListing = {
          id: String(mockListings.length + 1),
          ...listingData,
          rating: 0,
          images: [listingData.image || 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400']
        };
        mockListings.push(newListing);
        resolve(newListing);
      }, 1000);
    });
  },

  // Bookings
  getBookings: async (userId: string) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const userBookings = mockBookings
          .filter(booking => booking.userId === userId)
          .map(booking => ({
            ...booking,
            listing: mockListings.find(l => l.id === booking.listingId)
          }));
        resolve(userBookings);
      }, 500);
    });
  },

  createBooking: async (bookingData: any) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const newBooking = {
          id: String(mockBookings.length + 1),
          ...bookingData,
          status: 'pending'
        };
        mockBookings.push(newBooking);
        resolve(newBooking);
      }, 800);
    });
  },

  // Wallet
  getWallet: async (userId: string) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(mockWalletData);
      }, 500);
    });
  },

  // Analytics for dashboard
  getDashboardStats: async (userId: string) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          totalIncome: 5899,
          incomeChange: 20.9,
          visitors: 780192,
          visitorsChange: 20,
          totalOrders: 796542,
          ordersChange: -9.01,
          recentBookings: mockBookings.slice(0, 5).map(booking => ({
            ...booking,
            listing: mockListings.find(l => l.id === booking.listingId),
            user: mockUsers.find(u => u.id === booking.userId)
          }))
        });
      }, 600);
    });
  }
};