<?php
include '../intel/connect.php';

if (isset($_GET['user_id'])) { 
    $user_id = $_GET['user_id'];
    $_SESSION['user_id'] = $user_id;
}

$user = User::getData($user_id);
$images = Sweat::images($user_id);
$videos = Sweat::videos($user_id);
$who_users = Subscribe::whoTosubscribe($user_id);
$notify_count = User::CountNotification($user_id);

$videoData = array();  // Create an array to store video data

$mysqli = new mysqli('localhost', 'root', 'sneaKer123@', 'sweatportal');

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Get the page number from the request
$pageNumber = isset($_GET['page']) ? (int)$_GET['page'] : 1;

// Get the total count of videos
$sqlCount = "SELECT COUNT(*) as total FROM sweat_video ";
$resultCount = $mysqli->query($sqlCount);

if ($resultCount->num_rows > 0) {
    $row = $resultCount->fetch_assoc();
    $totalPosts = $row["total"];

    // Calculate total pages
    $postsPerPage = 10;  // Assuming 10 posts per page
    $totalPages = ceil($totalPosts / $postsPerPage);

    // Ensure $pageNumber is within valid range
    $pageNumber = max(1, min($pageNumber, $totalPages));
} else {
    $totalPosts = 0;
    $totalPages = 0;
}

$offset = ($pageNumber - 1) * $postsPerPage;

// Modify your main query to use the calculated offset
$sqlVideos = "SELECT * FROM sweat_video
    WHERE (video_id = ? OR image_id = ? OR reel_id = ? OR uid = ?)
    OR video_id OR user_id OR uid OR reel_id IN (
        SELECT subscribed_id FROM subscribe WHERE subscriber_id = ?
    )
    ORDER BY RAND() DESC LIMIT ? OFFSET ?";
$stmt = $mysqli->prepare($sqlVideos);
$stmt->bind_param("sssssss", $user_id, $user_id, $user_id, $user_id, $user_id, $postsPerPage, $offset);
$stmt->execute();
$resultVideos = $stmt->get_result();

while ($video = $resultVideos->fetch_assoc()) {
    $share_sign = false;
    $share_view = false;
    $qoq = false;

	 if (Sweat::isVideo($video['post_id'])) {
        $sweat_user = User::getDatanow($video['user_id']);

        $sweat_reals = Sweat::getVideonow($video['post_id']);
        $timeAgo = Sweat::getTimeAgo($video['date']);
        $likes_count = Sweat::countLikes($video['post_id']);
        $user_like_it = Sweat::userLikeIt($user_id, $video['post_id']);
        $sweat_link = $video['post_id'];

       if ($share_sign) {
    $view_count = Sweat::countViews($shared_tweet['post_id']);
} else {
    $view_count = Sweat::countViews($video['post_id']);
}

		$fileas = $sweat_reals->reels;
		$fileasklo = $sweat_reals->video;
		$file = $sweat_reals->img;

		$fileS = $sweat_reals->pro_img;
		$ext = pathinfo($file, PATHINFO_EXTENSION);

		$extss = pathinfo($fileS, PATHINFO_EXTENSION);
		$extz = pathinfo($fileas, PATHINFO_EXTENSION);
		$extzA = pathinfo($fileasklo, PATHINFO_EXTENSION);
		$result = '';

     if ($ext == "jpg" || $ext == 'png' || $ext == 'webp' || $ext == 'jfif') {
            // Code for image case
            $result = 'image';	
			
    } else if ($extz == "mp4" || $extz == 'mkv') {
            // Code for video case
            $result = 'reels';
			
	} else if ($extzA == "mp4" || $extzA == 'mkv') {
            // Code for reels case
            $result = ' video';	
			
    } else if ($extss == "jpg" || $extss == 'png' || $extss == 'webp' || $extss == 'jfif') {
            // Code for progress case
            $result = 'progress';
        }

        // Add video data to the array
       $videoData[] = array(
    'sweat_user' => $sweat_user,
    'sweat_reals' => $sweat_reals,
    'timeAgo' => $timeAgo,
    'likes_count' => $likes_count,
    'user_like_it' => $user_like_it,
    'result' => $result
);
    }
}
$pageNumber++;
// Close the database connection
$mysqli->close();

// Return the video data as JSON
header('Content-Type: application/json');
echo json_encode(['data' => $videoData, 'totalPosts' => $totalPosts, 'totalPages' => $totalPages, 'pageNumber' => $pageNumber]);
?>
