#!/usr/bin/env php
<?php
/**
 * Test script to verify health data and medical report endpoints
 * Run: php test-health-endpoints.php
 */

require_once __DIR__ . '/api/lib.php';

echo "Health Data & Medical Report API Test\n";
echo "=====================================\n\n";

try {
    $db = hw_db();
    
    // Test 1: Create health_metrics table
    echo "1. Creating health_metrics table...\n";
    $db->exec("
        CREATE TABLE IF NOT EXISTS health_metrics (
            id INT AUTO_INCREMENT PRIMARY KEY,
            user_id INT NOT NULL,
            blood_pressure VARCHAR(20),
            heart_rate INT,
            sleep_hours DECIMAL(3,1),
            weight DECIMAL(5,1),
            session_notes LONGTEXT,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
            KEY (user_id, created_at)
        )
    ");
    echo "   ✓ health_metrics table ready\n\n";
    
    // Test 2: Create medical_reports table
    echo "2. Creating medical_reports table...\n";
    $db->exec("
        CREATE TABLE IF NOT EXISTS medical_reports (
            id INT AUTO_INCREMENT PRIMARY KEY,
            user_id INT NOT NULL,
            filename VARCHAR(255) NOT NULL,
            original_filename VARCHAR(255) NOT NULL,
            file_path VARCHAR(512) NOT NULL,
            file_size INT NOT NULL,
            mime_type VARCHAR(100),
            uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
            KEY (user_id, uploaded_at)
        )
    ");
    echo "   ✓ medical_reports table ready\n\n";
    
    // Test 3: Verify upload directory
    echo "3. Creating upload directory...\n";
    $uploadDir = __DIR__ . '/uploads/medical-reports';
    if (!is_dir($uploadDir)) {
        mkdir($uploadDir, 0755, true);
    }
    echo "   ✓ Upload directory ready at: " . $uploadDir . "\n\n";
    
    // Test 4: Check endpoints exist
    echo "4. Checking API endpoints...\n";
    $endpoints = [
        '/api/users/share-health-data.php',
        '/api/users/upload-medical-report.php'
    ];
    
    foreach ($endpoints as $endpoint) {
        $path = __DIR__ . $endpoint;
        if (file_exists($path)) {
            echo "   ✓ $endpoint exists\n";
        } else {
            echo "   ✗ $endpoint missing\n";
        }
    }
    
    echo "\n✓ All tests passed! Endpoints are ready.\n";
    echo "\nEndpoint Details:\n";
    echo "  Health Data: POST /api/users/share-health-data.php\n";
    echo "    - Accepts: blood_pressure, heart_rate, sleep_hours, weight, session_notes\n";
    echo "    - Returns: {success: bool, message: string, id: int}\n\n";
    echo "  Medical Reports: POST /api/users/upload-medical-report.php\n";
    echo "    - Accepts: multipart file (pdf, jpg, jpeg, png, csv, txt)\n";
    echo "    - Max size: 10 MB\n";
    echo "    - Returns: {success: bool, message: string, id: int, filename: string}\n";
    
} catch (Exception $e) {
    echo "\n✗ Error: " . $e->getMessage() . "\n";
    exit(1);
}
