<?php
header('Content-Type: application/json; charset=UTF-8');

$videoId = $_POST['video_id'];
$apiTitle = $_POST['title'];
$apiDescription = $_POST['description'];

if (!empty($videoId)) {
    $apiKey = "AIzaSyB0q4jEy_FJQrCjiztf8zBo6ynz-6shYYM"; 
    $apiUrl = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" . $videoId . "&key=" . $apiKey;
    
    $response = @file_get_contents($apiUrl);
    if ($response === FALSE) {
        echo json_encode(["status" => "error", "message" => "YouTube API error"]);
        exit();
    }
    
    $data = json_decode($response, true);
    if (!empty($data['items'])) {
        $apiTitle = $data['items'][0]['snippet']['title'];
        $apiDescription = $data['items'][0]['snippet']['description'];
        
        echo json_encode([
            "status" => "success", 
            "message" => "Data received and processed successfully",
            "title" => $apiTitle,
            "description" => $apiDescription
        ]);
    } else {
        echo json_encode(["status" => "error", "message" => "Video not found"]);
    }

} else {
    echo json_encode(["status" => "error", "message" => "Invalid Request or Missing Video ID"]);
}
?>