/**
 * Download Feature Types
 * Social Media Video Downloader
 */

// Supported platforms
export type Platform =
  | "youtube"
  | "instagram"
  | "tiktok"
  | "twitter"
  | "facebook"
  | "unknown";

// Video quality options
export type VideoQuality = "max" | "1080" | "720" | "480" | "360";

// Audio format options
export type AudioFormat = "mp3" | "opus" | "wav" | "best";

// API Request types
export interface DownloadRequest {
  url: string;
  quality?: VideoQuality;
  audioFormat?: AudioFormat;
  audioBitrate?: "320" | "256" | "128" | "64";
}

export interface InfoRequest {
  url: string;
}

// API Response types
export interface CobaltErrorResponse {
  error: {
    code: CobaltErrorCode;
    message?: string;
    context?: {
      service?: string;
    };
  };
}

export interface CobaltSuccessResponse {
  status: "redirect" | "stream" | "picker";
  url?: string;
  picker?: PickerItem[];
  filename?: string;
}

export type CobaltResponse = CobaltErrorResponse | CobaltSuccessResponse;

export interface PickerItem {
  type: "video" | "photo";
  url: string;
  thumb?: string;
}

export type CobaltErrorCode =
  | "error.api.link.invalid"
  | "error.api.fetch.fail"
  | "error.api.content.unavailable"
  | "error.api.rate_exceeded"
  | "error.api.fetch.rate"
  | "error.api.content.video_region"
  | "error.api.content.post.private"
  | "error.api.fetch.short"
  | "error.api.fetch.critical"
  | "error.api.fetch.timeout"
  | "error.api.info.fail";

// Media type
export type MediaType = 'video' | 'image' | 'audio';

// Our API Response types
export interface DownloadResponse {
  success: boolean;
  data?: {
    downloadUrl: string;
    filename: string;
    platform: Platform;
    title?: string;
    thumbnail?: string;
    mediaType?: MediaType;
    duration?: string;
    author?: string;
    likes?: string;
    views?: string;
  };
  error?: {
    code: string;
    message: string;
  };
}

export interface InfoResponse {
  success: boolean;
  data?: {
    platform: Platform;
    title?: string;
    thumbnail?: string;
    duration?: number;
    qualities?: VideoQuality[];
  };
  error?: {
    code: string;
    message: string;
  };
}

// Platform detection result
export interface PlatformDetectionResult {
  platform: Platform;
  confidence: "high" | "low";
  originalUrl: string;
  normalizedUrl?: string;
}

// Rate limiting
export interface RateLimitInfo {
  ip: string;
  requests: number;
  lastRequest: number;
  blocked: boolean;
  blockUntil?: number;
}

// Supported domains map
export const SUPPORTED_DOMAINS: Record<Platform, string[]> = {
  youtube: [
    "youtube.com",
    "youtu.be",
    "music.youtube.com",
    "youtube-nocookie.com",
  ],
  instagram: ["instagram.com", "instagr.am"],
  tiktok: ["tiktok.com", "vm.tiktok.com", "vt.tiktok.com"],
  twitter: ["twitter.com", "x.com", "t.co"],
  facebook: ["facebook.com", "fb.watch", "fb.com"],
  unknown: [],
};

// Platform display info
export const PLATFORM_INFO: Record<
  Platform,
  { name: string; color: string; icon: string }
> = {
  youtube: { name: "YouTube", color: "#FF0000", icon: "youtube" },
  instagram: { name: "Instagram", color: "#E4405F", icon: "instagram" },
  tiktok: { name: "TikTok", color: "#000000", icon: "tiktok" },
  twitter: { name: "X (Twitter)", color: "#1DA1F2", icon: "twitter" },
  facebook: { name: "Facebook", color: "#1877F2", icon: "facebook" },
  unknown: { name: "Bilinmeyen", color: "#6B7280", icon: "question" },
};
