// Mock Bookmark API Service
export interface BookmarkItem {
  id: string;
  title: string;
  description: string;
  image: string;
  rating: number;
  reviewCount: number;
  location: string;
  address: string;
  phone: string;
  category: string;
  price?: string;
  isVerified: boolean;
  isFeatured: boolean;
  amenities: string[];
  coordinates: {
    lat: number;
    lng: number;
  };
  gallery: string[];
  openingHours: {
    day: string;
    hours: string;
  }[];
}

// Mock data that matches offers from /explore
const mockBookmarks: BookmarkItem[] = [
  {
    id: "1",
    title: "Luxury Beach Resort Punta Cana",
    description: "Experience paradise at our exclusive beachfront resort with world-class amenities",
    image: "/src/assets/punta-cana.jpg",
    rating: 4.8,
    reviewCount: 1247,
    location: "Punta Cana",
    address: "Playa Bavaro, Punta Cana 23000, Dominican Republic",
    phone: "+1 809-555-0123",
    category: "Resort",
    price: "$299/night",
    isVerified: true,
    isFeatured: true,
    amenities: ["WiFi", "Pool", "Beach Access", "Spa", "Restaurant"],
    coordinates: { lat: 18.5601, lng: -68.3725 },
    gallery: [
      "/src/assets/punta-cana.jpg",
      "/src/assets/hero-beach.jpg"
    ],
    openingHours: [
      { day: "Monday", hours: "24/7" },
      { day: "Tuesday", hours: "24/7" },
      { day: "Wednesday", hours: "24/7" },
      { day: "Thursday", hours: "24/7" },
      { day: "Friday", hours: "24/7" },
      { day: "Saturday", hours: "24/7" },
      { day: "Sunday", hours: "24/7" }
    ]
  },
  {
    id: "2",
    title: "Historic Old San Juan Tour",
    description: "Discover the rich history and vibrant culture of Old San Juan with expert guides",
    image: "/src/assets/san-juan.jpg",
    rating: 4.6,
    reviewCount: 892,
    location: "San Juan",
    address: "Calle del Cristo, Old San Juan, San Juan 00901, Puerto Rico",
    phone: "+1 787-555-0456",
    category: "Tour",
    price: "$45/person",
    isVerified: true,
    isFeatured: false,
    amenities: ["Guide", "Walking Tour", "Historical Sites", "Photography"],
    coordinates: { lat: 18.4655, lng: -66.1057 },
    gallery: [
      "/src/assets/san-juan.jpg",
      "/src/assets/hero-beach.jpg"
    ],
    openingHours: [
      { day: "Monday", hours: "9:00 AM - 5:00 PM" },
      { day: "Tuesday", hours: "9:00 AM - 5:00 PM" },
      { day: "Wednesday", hours: "9:00 AM - 5:00 PM" },
      { day: "Thursday", hours: "9:00 AM - 5:00 PM" },
      { day: "Friday", hours: "9:00 AM - 5:00 PM" },
      { day: "Saturday", hours: "9:00 AM - 6:00 PM" },
      { day: "Sunday", hours: "10:00 AM - 4:00 PM" }
    ]
  },
  {
    id: "3",
    title: "Santo Domingo Colonial Experience",
    description: "Explore the first city of the Americas with authentic colonial architecture",
    image: "/src/assets/santo-domingo.jpg",
    rating: 4.5,
    reviewCount: 654,
    location: "Santo Domingo",
    address: "Zona Colonial, Santo Domingo 10210, Dominican Republic",
    phone: "+1 809-555-0789",
    category: "Cultural",
    price: "$35/person",
    isVerified: true,
    isFeatured: true,
    amenities: ["Guide", "Cultural Sites", "Museums", "Architecture"],
    coordinates: { lat: 18.4861, lng: -69.9312 },
    gallery: [
      "/src/assets/santo-domingo.jpg",
      "/src/assets/hero-beach.jpg"
    ],
    openingHours: [
      { day: "Monday", hours: "8:00 AM - 6:00 PM" },
      { day: "Tuesday", hours: "8:00 AM - 6:00 PM" },
      { day: "Wednesday", hours: "8:00 AM - 6:00 PM" },
      { day: "Thursday", hours: "8:00 AM - 6:00 PM" },
      { day: "Friday", hours: "8:00 AM - 6:00 PM" },
      { day: "Saturday", hours: "8:00 AM - 7:00 PM" },
      { day: "Sunday", hours: "9:00 AM - 5:00 PM" }
    ]
  },
  {
    id: "4",
    title: "Caribbean Culinary Adventure",
    description: "Taste authentic Caribbean flavors with our premium food tour experience",
    image: "/src/assets/hero-beach.jpg",
    rating: 4.7,
    reviewCount: 1123,
    location: "Multiple Locations",
    address: "Various restaurants across the Caribbean",
    phone: "+1 829-555-0321",
    category: "Food & Drink",
    price: "$85/person",
    isVerified: true,
    isFeatured: false,
    amenities: ["Food Tasting", "Local Guide", "Transportation", "Recipes"],
    coordinates: { lat: 18.2208, lng: -66.5901 },
    gallery: [
      "/src/assets/hero-beach.jpg",
      "/src/assets/punta-cana.jpg"
    ],
    openingHours: [
      { day: "Monday", hours: "11:00 AM - 8:00 PM" },
      { day: "Tuesday", hours: "11:00 AM - 8:00 PM" },
      { day: "Wednesday", hours: "11:00 AM - 8:00 PM" },
      { day: "Thursday", hours: "11:00 AM - 8:00 PM" },
      { day: "Friday", hours: "11:00 AM - 9:00 PM" },
      { day: "Saturday", hours: "10:00 AM - 9:00 PM" },
      { day: "Sunday", hours: "10:00 AM - 8:00 PM" }
    ]
  },
  {
    id: "5",
    title: "Tropical Spa & Wellness Center",
    description: "Rejuvenate your body and soul with our premium spa treatments",
    image: "/src/assets/punta-cana.jpg",
    rating: 4.9,
    reviewCount: 567,
    location: "Bavaro",
    address: "Av. Alemania, Bavaro, Punta Cana 23000, Dominican Republic",
    phone: "+1 809-555-0654",
    category: "Wellness",
    price: "$120/session",
    isVerified: true,
    isFeatured: true,
    amenities: ["Spa", "Massage", "Wellness", "Relaxation", "Beauty"],
    coordinates: { lat: 18.6821, lng: -68.4645 },
    gallery: [
      "/src/assets/punta-cana.jpg",
      "/src/assets/santo-domingo.jpg"
    ],
    openingHours: [
      { day: "Monday", hours: "8:00 AM - 8:00 PM" },
      { day: "Tuesday", hours: "8:00 AM - 8:00 PM" },
      { day: "Wednesday", hours: "8:00 AM - 8:00 PM" },
      { day: "Thursday", hours: "8:00 AM - 8:00 PM" },
      { day: "Friday", hours: "8:00 AM - 9:00 PM" },
      { day: "Saturday", hours: "7:00 AM - 9:00 PM" },
      { day: "Sunday", hours: "8:00 AM - 7:00 PM" }
    ]
  },
  {
    id: "6",
    title: "Adventure Water Sports Center",
    description: "Thrilling water activities and equipment rental for all skill levels",
    image: "/src/assets/san-juan.jpg",
    rating: 4.4,
    reviewCount: 890,
    location: "Boca Chica",
    address: "Playa Boca Chica, Santo Domingo Este, Dominican Republic",
    phone: "+1 809-555-0987",
    category: "Adventure",
    price: "$60/activity",
    isVerified: true,
    isFeatured: false,
    amenities: ["Water Sports", "Equipment Rental", "Instruction", "Safety Gear"],
    coordinates: { lat: 18.4484, lng: -69.6113 },
    gallery: [
      "/src/assets/san-juan.jpg",
      "/src/assets/hero-beach.jpg"
    ],
    openingHours: [
      { day: "Monday", hours: "7:00 AM - 6:00 PM" },
      { day: "Tuesday", hours: "7:00 AM - 6:00 PM" },
      { day: "Wednesday", hours: "7:00 AM - 6:00 PM" },
      { day: "Thursday", hours: "7:00 AM - 6:00 PM" },
      { day: "Friday", hours: "7:00 AM - 7:00 PM" },
      { day: "Saturday", hours: "6:00 AM - 7:00 PM" },
      { day: "Sunday", hours: "7:00 AM - 6:00 PM" }
    ]
  }
];

class BookmarkApiService {
  private baseUrl: string;

  constructor() {
    // Use environment variable or fallback to mock
    this.baseUrl = import.meta.env.VITE_BOOKMARK_API_URL || 'mock';
  }

  async getBookmarks(userId?: string): Promise<BookmarkItem[]> {
    try {
      if (this.baseUrl === 'mock' || import.meta.env.DEV) {
        // Return mock data for development
        await new Promise(resolve => setTimeout(resolve, 500)); // Simulate API delay
        return mockBookmarks;
      }

      // Real API call would go here
      const response = await fetch(`${this.baseUrl}/bookmarks?userId=${userId}`);
      if (!response.ok) {
        throw new Error('Failed to fetch bookmarks');
      }
      return await response.json();
    } catch (error) {
      console.error('Error fetching bookmarks:', error);
      // Fallback to mock data
      return mockBookmarks;
    }
  }

  async addBookmark(itemId: string, userId: string): Promise<boolean> {
    try {
      if (this.baseUrl === 'mock' || import.meta.env.DEV) {
        // Simulate successful bookmark addition
        await new Promise(resolve => setTimeout(resolve, 300));
        return true;
      }

      const response = await fetch(`${this.baseUrl}/bookmarks`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ itemId, userId }),
      });

      return response.ok;
    } catch (error) {
      console.error('Error adding bookmark:', error);
      return false;
    }
  }

  async removeBookmark(itemId: string, userId: string): Promise<boolean> {
    try {
      if (this.baseUrl === 'mock' || import.meta.env.DEV) {
        // Simulate successful bookmark removal
        await new Promise(resolve => setTimeout(resolve, 300));
        return true;
      }

      const response = await fetch(`${this.baseUrl}/bookmarks/${itemId}`, {
        method: 'DELETE',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ userId }),
      });

      return response.ok;
    } catch (error) {
      console.error('Error removing bookmark:', error);
      return false;
    }
  }

  async isBookmarked(itemId: string, userId: string): Promise<boolean> {
    try {
      if (this.baseUrl === 'mock' || import.meta.env.DEV) {
        // Simulate checking bookmark status
        await new Promise(resolve => setTimeout(resolve, 200));
        return mockBookmarks.some(item => item.id === itemId);
      }

      const response = await fetch(`${this.baseUrl}/bookmarks/check?itemId=${itemId}&userId=${userId}`);
      if (!response.ok) {
        return false;
      }
      const result = await response.json();
      return result.isBookmarked;
    } catch (error) {
      console.error('Error checking bookmark status:', error);
      return false;
    }
  }
}

export const bookmarkApi = new BookmarkApiService();
export { mockBookmarks };