// src/types/meta.ts
// Type Definitions für Meta Graph API (Facebook + Instagram)

// =============================================================================
// OAuth & Auth Types
// =============================================================================

export interface MetaTokens {
  access_token: string
  token_type: string
  expires_in?: number
}

export interface MetaLongLivedToken {
  access_token: string
  token_type: string
  expires_in: number // Typischerweise 5184000 (60 Tage)
}

export interface MetaTokenDebugInfo {
  app_id: string
  type: string
  application: string
  data_access_expires_at: number
  expires_at: number
  is_valid: boolean
  scopes: string[]
  user_id: string
}

// =============================================================================
// User & Account Types
// =============================================================================

export interface MetaUser {
  id: string
  name: string
  email?: string
}

export interface FacebookPage {
  id: string
  name: string
  access_token: string // Page-spezifischer Token
  category: string
  category_list?: Array<{ id: string; name: string }>
  tasks?: string[]
  instagram_business_account?: {
    id: string
    username?: string
  }
}

export interface InstagramBusinessAccount {
  id: string
  username: string
  name?: string
  profile_picture_url?: string
  followers_count: number
  follows_count: number
  media_count: number
  biography?: string
  website?: string
}

// =============================================================================
// Facebook Types
// =============================================================================

export interface FacebookPost {
  id: string
  message?: string
  story?: string
  full_picture?: string
  permalink_url: string
  created_time: string
  updated_time?: string
  shares?: { count: number }
  reactions?: { summary: { total_count: number } }
  comments?: { summary: { total_count: number } }
  attachments?: {
    data: Array<{
      type: string
      url?: string
      media?: { image: { src: string } }
    }>
  }
}

export interface FacebookComment {
  id: string
  message: string
  from: {
    id: string
    name: string
  }
  created_time: string
  like_count: number
  comment_count: number
  is_hidden: boolean
  can_hide: boolean
  can_reply_privately: boolean
  parent?: {
    id: string
  }
  attachment?: {
    type: string
    url?: string
    media?: { image: { src: string } }
  }
}

export interface FacebookConversation {
  id: string
  participants: {
    data: Array<{
      id: string
      name: string
      email?: string
    }>
  }
  updated_time: string
  message_count: number
  unread_count: number
  can_reply: boolean
}

export interface FacebookMessage {
  id: string
  message: string
  from: {
    id: string
    name: string
    email?: string
  }
  to: {
    data: Array<{
      id: string
      name: string
    }>
  }
  created_time: string
  attachments?: {
    data: Array<{
      id: string
      mime_type: string
      name: string
      file_url?: string
      image_data?: {
        url: string
        width: number
        height: number
      }
    }>
  }
}

export interface FacebookPageInsight {
  name: string
  period: string
  values: Array<{
    value: number | Record<string, number>
    end_time: string
  }>
  title: string
  description: string
  id: string
}

// =============================================================================
// Instagram Types
// =============================================================================

export interface InstagramMedia {
  id: string
  media_type: 'IMAGE' | 'VIDEO' | 'CAROUSEL_ALBUM' | 'REELS'
  media_url?: string
  thumbnail_url?: string
  permalink: string
  caption?: string
  timestamp: string
  like_count: number
  comments_count: number
  children?: {
    data: Array<{
      id: string
      media_type: string
      media_url: string
    }>
  }
}

export interface InstagramComment {
  id: string
  text: string
  username: string
  timestamp: string
  like_count: number
  replies?: {
    data: InstagramComment[]
  }
  from?: {
    id: string
    username: string
  }
  hidden: boolean
}

export interface InstagramMention {
  id: string
  caption?: string
  media_type: string
  media_url?: string
  permalink: string
  timestamp: string
  username: string
  comments_count?: number
  like_count?: number
}

export interface InstagramStoryMention {
  id: string
  media_type: 'IMAGE' | 'VIDEO'
  media_url: string
  timestamp: string
  // Story mentions expire after 24h!
}

export interface InstagramConversation {
  id: string
  participants: {
    data: Array<{
      id: string
      username: string
    }>
  }
  updated_time: string
  messages?: {
    data: InstagramMessage[]
  }
}

export interface InstagramMessage {
  id: string
  message: string
  from: {
    id: string
    username: string
  }
  created_time: string
  attachments?: {
    data: Array<{
      type: string
      url: string
    }>
  }
}

export interface InstagramInsight {
  name: string
  period: string
  values: Array<{
    value: number
    end_time?: string
  }>
  title: string
  description: string
  id: string
}

// =============================================================================
// API Response Types
// =============================================================================

export interface MetaPaginatedResponse<T> {
  data: T[]
  paging?: {
    cursors?: {
      before: string
      after: string
    }
    next?: string
    previous?: string
  }
  summary?: {
    total_count?: number
  }
}

export interface MetaErrorResponse {
  error: {
    message: string
    type: string
    code: number
    error_subcode?: number
    fbtrace_id: string
  }
}

// =============================================================================
// Sync Types
// =============================================================================

export interface MetaSyncResult {
  accountId: number
  accountName: string
  platform: 'facebook' | 'instagram'
  postsProcessed: number
  newComments: number
  updatedComments: number
  newMessages: number
  errors: string[]
  duration: number
}

export interface MetaSyncOptions {
  socialAccountId: number
  sinceDate?: Date
  maxItems?: number
  syncComments?: boolean
  syncMessages?: boolean
  syncMentions?: boolean
  analyzeWithAI?: boolean
}

// =============================================================================
// OAuth State Types
// =============================================================================

export interface MetaOAuthState {
  returnUrl: string
  accountType: 'facebook' | 'instagram' | 'both'
  userId?: number
  socialAccountId?: number
  timestamp: number
}

// =============================================================================
// Webhook Types (für zukünftige Implementierung)
// =============================================================================

export interface MetaWebhookEntry {
  id: string
  time: number
  changes?: Array<{
    field: string
    value: Record<string, unknown>
  }>
  messaging?: Array<{
    sender: { id: string }
    recipient: { id: string }
    timestamp: number
    message?: {
      mid: string
      text?: string
      attachments?: Array<{
        type: string
        payload: { url: string }
      }>
    }
  }>
}

export interface MetaWebhookPayload {
  object: 'page' | 'instagram'
  entry: MetaWebhookEntry[]
}
