<?php
/**
 * LEAD Marketing Conference - YouTube Video Fetcher
 * Pulls all videos from the channel, generates summaries via Claude API,
 * and outputs a CSV with URL, date, and summary.
 *
 * Usage: php fetch_lead_videos.php
 * Output: lead_videos.csv
 */

// ─── CONFIG ──────────────────────────────────────────────────────────────────

$YOUTUBE_API_KEY  = 'YOUR_YOUTUBE_API_KEY';   // Replace with your key
$CLAUDE_API_KEY   = 'YOUR_CLAUDE_API_KEY';    // Replace with your Anthropic key
$CHANNEL_ID       = 'UCh1a-GcToUxQNhG0xCwW63Q';
$OUTPUT_FILE      = 'lead_videos.csv';
$CLAUDE_MODEL     = 'claude-opus-4-5-20251101';
$MAX_TOKENS       = 300;
$DELAY_SECONDS    = 1; // Pause between Claude calls to avoid rate limits

// ─── STEP 1: GET UPLOADS PLAYLIST ID ─────────────────────────────────────────

function get_uploads_playlist_id($channel_id, $api_key) {
    $url = "https://www.googleapis.com/youtube/v3/channels?"
         . http_build_query([
               'part'   => 'contentDetails',
               'id'     => $channel_id,
               'key'    => $api_key,
           ]);

    $response = json_decode(file_get_contents($url), true);

    if (empty($response['items'])) {
        die("ERROR: Could not find channel. Check CHANNEL_ID and API key.\n");
    }

    return $response['items'][0]['contentDetails']['relatedPlaylists']['uploads'];
}

// ─── STEP 2: FETCH ALL VIDEOS FROM UPLOADS PLAYLIST ──────────────────────────

function get_all_videos($playlist_id, $api_key) {
    $videos    = [];
    $page_token = null;

    do {
        $params = [
            'part'       => 'snippet',
            'playlistId' => $playlist_id,
            'maxResults' => 50,
            'key'        => $api_key,
        ];
        if ($page_token) {
            $params['pageToken'] = $page_token;
        }

        $url      = "https://www.googleapis.com/youtube/v3/playlistItems?" . http_build_query($params);
        $response = json_decode(file_get_contents($url), true);

        if (isset($response['error'])) {
            die("YouTube API error: " . $response['error']['message'] . "\n");
        }

        foreach ($response['items'] as $item) {
            $snippet  = $item['snippet'];
            $video_id = $snippet['resourceId']['videoId'];

            $videos[] = [
                'title'        => $snippet['title'],
                'url'          => "https://www.youtube.com/watch?v={$video_id}",
                'published_at' => substr($snippet['publishedAt'], 0, 10), // YYYY-MM-DD
                'description'  => trim($snippet['description']),
            ];
        }

        $page_token = $response['nextPageToken'] ?? null;

    } while ($page_token);

    return $videos;
}

// ─── STEP 3: GENERATE SUMMARY VIA CLAUDE API ─────────────────────────────────

function get_summary($title, $description, $claude_api_key, $model, $max_tokens) {
    $prompt = "You are summarizing a conference presentation video for a CPG industry audience.\n\n"
            . "Title: {$title}\n\n"
            . "Description:\n{$description}\n\n"
            . "Write a concise 2-3 sentence summary of what this presentation covers and who presented it. "
            . "Focus on the topic, key takeaways, and speaker/company if mentioned. "
            . "If the description is empty or very sparse, base the summary on the title alone.";

    $payload = json_encode([
        'model'      => $model,
        'max_tokens' => $max_tokens,
        'messages'   => [
            ['role' => 'user', 'content' => $prompt],
        ],
    ]);

    $ch = curl_init('https://api.anthropic.com/v1/messages');
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => $payload,
        CURLOPT_HTTPHEADER     => [
            'Content-Type: application/json',
            'x-api-key: ' . $claude_api_key,
            'anthropic-version: 2023-06-01',
        ],
    ]);

    $result   = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($http_code !== 200) {
        echo "  WARNING: Claude API returned HTTP {$http_code} for \"{$title}\"\n";
        return '(summary unavailable)';
    }

    $data = json_decode($result, true);
    return trim($data['content'][0]['text'] ?? '(summary unavailable)');
}

// ─── MAIN ─────────────────────────────────────────────────────────────────────

echo "Fetching uploads playlist ID...\n";
$playlist_id = get_uploads_playlist_id($CHANNEL_ID, $YOUTUBE_API_KEY);
echo "Playlist ID: {$playlist_id}\n";

echo "Fetching all videos...\n";
$videos = get_all_videos($playlist_id, $YOUTUBE_API_KEY);
echo "Found " . count($videos) . " videos.\n\n";

// Open CSV for writing
$fh = fopen($OUTPUT_FILE, 'w');
fputcsv($fh, ['Title', 'URL', 'Date Published', 'Summary']);

foreach ($videos as $i => $video) {
    $num = $i + 1;
    echo "[{$num}/" . count($videos) . "] Summarizing: {$video['title']}\n";

    $summary = get_summary(
        $video['title'],
        $video['description'],
        $CLAUDE_API_KEY,
        $CLAUDE_MODEL,
        $MAX_TOKENS
    );

    fputcsv($fh, [
        $video['title'],
        $video['url'],
        $video['published_at'],
        $summary,
    ]);

    // Flush so you can tail -f the output file as it runs
    fflush($fh);

    if ($num < count($videos)) {
        sleep($DELAY_SECONDS);
    }
}

fclose($fh);
echo "\nDone. Output written to: {$OUTPUT_FILE}\n";
