// Instagram API utilities for fetching posts and media
// Based on Instagram Graph API documentation from Context7

export interface InstagramPost {
  id: string;
  caption: string;
  media_url: string;
  permalink: string;
  timestamp: string;
  media_type: string;
  thumbnail_url?: string;
}

// Instagram Graph API oEmbed endpoint for getting embeddable HTML
export async function getInstagramEmbedHtml(postUrl: string): Promise<string | null> {
  try {
    const response = await fetch(
      `https://graph.facebook.com/v23.0/instagram_oembed?url=${encodeURIComponent(postUrl)}`
    );
    
    if (!response.ok) {
      throw new Error('Failed to fetch Instagram embed');
    }
    
    const data = await response.json();
    return data.html || null;
  } catch (error) {
    console.error('Error fetching Instagram embed:', error);
    return null;
  }
}

// Fetch Instagram page media using Graph API (requires access token)
export async function getInstagramPageMedia(
  pageId: string, 
  accessToken: string,
  limit: number = 6
): Promise<InstagramPost[]> {
  try {
    const response = await fetch(
      `https://graph.facebook.com/v23.0/${pageId}/media?fields=id,caption,media_url,permalink,timestamp,media_type,thumbnail_url&limit=${limit}&access_token=${accessToken}`
    );
    
    if (!response.ok) {
      throw new Error('Failed to fetch Instagram media');
    }
    
    const data = await response.json();
    return data.data || [];
  } catch (error) {
    console.error('Error fetching Instagram media:', error);
    return [];
  }
}

// Alternative: Use a third-party service like EmbedSocial or SnapWidget
export function getEmbedSocialWidget(username: string): string {
  // This would return the embed code for services like EmbedSocial
  return `
    <div class="embedsocial-hashtag" data-ref="instagram" data-hashtag="${username}">
      <a class="feed-powered-by-es" href="https://embedsocial.com/instagram/" target="_blank" title="Widget by EmbedSocial">Widget by EmbedSocial</a>
    </div>
  `;
}

// Sample data for development/demo purposes
export const sampleInstagramPosts: InstagramPost[] = [
  {
    id: "1",
    caption: "Working on some sick VFX for an upcoming project... 🔥",
    media_url: "https://picsum.photos/400/400?random=1",
    permalink: "https://www.instagram.com/p/sample1/",
    timestamp: "2025-01-10T10:00:00Z",
    media_type: "IMAGE"
  },
  {
    id: "2", 
    caption: "Behind the scenes of our latest work 🎬",
    media_url: "https://picsum.photos/400/400?random=2",
    permalink: "https://www.instagram.com/p/sample2/",
    timestamp: "2025-01-08T15:30:00Z",
    media_type: "VIDEO",
    thumbnail_url: "https://picsum.photos/400/400?random=2"
  },
  {
    id: "3",
    caption: "Studio life - coffee, code, and VFX magic ✨",
    media_url: "https://picsum.photos/400/400?random=3",
    permalink: "https://www.instagram.com/p/sample3/",
    timestamp: "2025-01-05T12:00:00Z",
    media_type: "IMAGE"
  },
  {
    id: "4",
    caption: "Client project reveal - can't wait to share this one! 🚀",
    media_url: "https://picsum.photos/400/400?random=4",
    permalink: "https://www.instagram.com/p/sample4/",
    timestamp: "2025-01-03T14:20:00Z",
    media_type: "IMAGE"
  },
  {
    id: "5",
    caption: "Late night render session - the magic happens when everyone else is sleeping 🌙",
    media_url: "https://picsum.photos/400/400?random=5",
    permalink: "https://www.instagram.com/p/sample5/",
    timestamp: "2025-01-01T22:30:00Z",
    media_type: "VIDEO",
    thumbnail_url: "https://picsum.photos/400/400?random=5"
  },
  {
    id: "6",
    caption: "New year, new projects, same dedication to quality VFX 💪",
    media_url: "https://picsum.photos/400/400?random=6",
    permalink: "https://www.instagram.com/p/sample6/",
    timestamp: "2024-12-30T09:00:00Z",
    media_type: "IMAGE"
  }
];
