import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsString, IsNotEmpty, Matches } from 'class-validator';

/**
 * X/Twitter URL'si ile tweet çekme DTO'su.
 * Desteklenen URL formatları:
 * - https://x.com/username/status/1234567890
 * - https://twitter.com/username/status/1234567890
 * - https://x.com/i/status/1234567890
 */
export class FetchTweetDto {
  @ApiProperty({
    description: 'X/Twitter tweet URL\'si',
    example: 'https://x.com/elonmusk/status/1893456789012345678',
  })
  @IsString()
  @IsNotEmpty({ message: 'Tweet URL\'si boş olamaz' })
  @Matches(
    /^https?:\/\/(x\.com|twitter\.com)\/([\w]+\/status\/\d+|i\/status\/\d+)/,
    { message: 'Geçerli bir X/Twitter tweet URL\'si girin' },
  )
  tweetUrl: string;
}

/**
 * FXTwitter API'den dönen ham tweet verisi.
 */
export interface FxTweetResponse {
  code: number;
  message: string;
  tweet?: {
    id: string;
    url: string;
    text: string;
    created_at: string;
    author: {
      id: string;
      name: string;
      screen_name: string;
      avatar_url: string;
      followers_count: number;
      following_count: number;
      description: string;
      verified?: boolean;
    };
    replies: number;
    retweets: number;
    likes: number;
    views: number;
    media?: {
      all: Array<{
        type: 'photo' | 'video' | 'gif';
        url: string;
        thumbnail_url?: string;
        width: number;
        height: number;
      }>;
    };
    quote?: FxTweetResponse['tweet'];
    // Thread bilgisi
    replying_to?: string | null;
    replying_to_status?: string | null;
  };
}

/**
 * Parse edilmiş ve normalize edilmiş tweet verisi.
 */
export interface ParsedTweet {
  id: string;
  url: string;
  text: string;
  createdAt: string;
  author: {
    id: string;
    name: string;
    username: string;
    avatarUrl: string;
    followersCount: number;
    verified: boolean;
  };
  metrics: {
    replies: number;
    retweets: number;
    likes: number;
    views: number;
    engagementRate: number; // (likes + retweets + replies) / views * 100
  };
  media: Array<{
    type: 'photo' | 'video' | 'gif';
    url: string;
    thumbnailUrl?: string;
    width: number;
    height: number;
  }>;
  quotedTweet?: ParsedTweet;
  isThread: boolean;
  threadTweets?: ParsedTweet[];
}

/**
 * Tweet ön izleme API response'u.
 */
export interface TweetPreviewResponse {
  tweet: ParsedTweet;
  suggestedTitle: string;
  suggestedPrompt: string;
  viralScore: number; // 0-100
  contentType: 'tweet' | 'thread' | 'quote_tweet';
  estimatedDuration: number; // saniye
}
