"use server";

import { db } from "@/lib/db";
import axios from "axios";

const handleUpdateCoins = async (youtubeUrl: string, coinValue: number) => {
    // const response = await fetch("/api/admin/update-coins", {
    //     method: "POST",
    //     headers: { "Content-Type": "application/json" },
    //     body: JSON.stringify(userCoins),
    // });
    // if (response.ok) {
    //     alert("User coins updated successfully!");
    //     setUserCoins({ userId: "", coins: 0 });
    // } else {
    //     const error = await response.json();
    //     alert(`Failed to update user coins: ${error.error}`);
    // }

    const apiKey = process.env.GOOGLE_YOUTUBE_API_KEY;

    if (!apiKey) {
        throw new Error("YouTube API key is not defined in the environment variables.");
    }

    try {
        const videoId = new URL(youtubeUrl).searchParams.get("v");
        if (!videoId) {
            throw new Error("Invalid YouTube URL. Could not extract video ID.");
        }

        const params = {
            part: "snippet",
            videoId,
            key: apiKey,
            maxResults: 100,
            pageToken: "",
        };

        let nextPageToken;
        const allAuthors: string[] = [];

        while (true) {
            params.pageToken = nextPageToken || "";
            const response = await axios.get(
                `https://www.googleapis.com/youtube/v3/commentThreads`,
                { params }
            );

            const pageAuthors = response.data.items.map(
                (item: any) => item.snippet.topLevelComment.snippet.authorDisplayName
            );
            allAuthors.push(...pageAuthors);
            
            nextPageToken = response.data.nextPageToken;
            if (!nextPageToken) {
                break; // Exit loop if no more pages
            }
        }

        const uniqueAuthors = Array.from(new Set(allAuthors));
        
        
        // console.log("Unique authors:", uniqueAuthors);

        // Use a transaction to ensure all updates are atomic
        await db.$transaction(async (prisma) => {
            // loop through the unique authors and update their coins
            for (const author of uniqueAuthors) {
                // console.log(`Updating coins for ${author} with value ${coinValue}`);
                try {
                    await prisma.user.update({
                        where: { youtubeHandle: author },
                        data: { coins: { increment: coinValue } },
                    });
                    console.log(`Updating coins for ${author} with value ${coinValue}`);
                } catch {
                    // console.log(`User with YouTube handle ${author} not found, skipping`);
                    // Continue with the next author
                }
            }
        },{timeout: 60000}); // Set a timeout of 60 seconds for the transaction (Modify as needed)

        console.log("Coins updated successfully for all authors.");
        
        return uniqueAuthors;
    } catch (error) {
        console.error("Error fetching comments from YouTube API:", error);
        throw error;
    }
};

export default handleUpdateCoins;