<?php
header('Content-Type: application/json');
include 'functions.php'; // Adjust path as necessary

$totalFiles = 0;
$fileCounts = [
    'Word Documents' => 0,
    'Excel Spreadsheets' => 0,
    'PowerPoint Presentations' => 0,
    'PDF Files' => 0,
    'Image Files' => 0,
    'Video Files' => 0,
    'Audio Files' => 0,
    'Other Files' => 0,
];

$result = $conn->query("SELECT * FROM pmed_documents");
while ($row = $result->fetch_assoc()) {
    $totalFiles++;
    switch ($row['pmed_file_type']) {
        case 'doc':
        case 'docx':
            $fileCounts['Word Documents']++;
            break;
        case 'pdf':
            $fileCounts['PDF Files']++;
            break;
        case 'xls':
        case 'xlsx':
            $fileCounts['Excel Spreadsheets']++;
            break;
        case 'ppt':
        case 'pptx':
            $fileCounts['PowerPoint Presentations']++;
            break;
        case 'jpg':
        case 'jpeg':
        case 'png':
        case 'gif':
            $fileCounts['Image Files']++;
            break;
        case 'mp4':
        case 'mpeg':
        case 'avi':
        case 'mkv':
        case 'mov':
        case 'wmv':
            $fileCounts['Video Files']++;
            break;
        case 'mp3':
        case 'acc':
        case 'wav':
        case 'aiff':
        case 'pcm':
            $fileCounts['Audio Files']++;
            break;
        default:
            $fileCounts['Other Files']++;
            break;
    }
}

echo json_encode(['total_files' => $totalFiles, 'file_counts' => $fileCounts]);
?>
