<?php
// Test script for Species API functionality
require_once 'api/config/database.php';
require_once 'api/models/Species.php';
require_once 'api/controllers/SpeciesController.php';

echo "=== Testing Species API Functionality ===\n\n";

try {
    // Initialize database connection
    $database = new Database();
    $db = $database->getConnection();
    
    if (!$db) {
        die("Error: Could not connect to database\n");
    }
    
    echo "✓ Database connection successful\n";
    
    // Test Species model
    $species = new Species($db);
    
    // Test 1: Initialize Guatemala species catalog
    echo "\n1. Testing species catalog initialization...\n";
    $initResult = $species->initializeGuatemalaCatalog();
    echo "   Inserted: {$initResult['inserted']} species\n";
    echo "   Total available: {$initResult['total']} species\n";
    if (!empty($initResult['errors'])) {
        echo "   Errors: " . implode(', ', $initResult['errors']) . "\n";
    }
    
    // Test 2: Get all species
    echo "\n2. Testing get all species...\n";
    $stmt = $species->read();
    $allSpecies = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo "   Found: " . count($allSpecies) . " active species\n";
    
    // Show first 5 species
    echo "   First 5 species:\n";
    for ($i = 0; $i < min(5, count($allSpecies)); $i++) {
        $sp = $allSpecies[$i];
        echo "   - {$sp['nombre_comun']} ({$sp['nombre_cientifico']})\n";
    }
    
    // Test 3: Get recommended species
    echo "\n3. Testing recommended species...\n";
    $recommended = $species->getRecommendedForInventory();
    echo "   Found: " . count($recommended) . " recommended species\n";
    
    // Test 4: Search functionality
    echo "\n4. Testing search functionality...\n";
    $searchTerm = 'Pinus';
    $stmt = $species->search($searchTerm);
    $searchResults = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo "   Search for '{$searchTerm}': " . count($searchResults) . " results\n";
    foreach ($searchResults as $result) {
        echo "   - {$result['nombre_comun']} ({$result['nombre_cientifico']})\n";
    }
    
    // Test 5: Most used species (will be empty initially)
    echo "\n5. Testing most used species...\n";
    $mostUsed = $species->getMostUsedSpecies(5);
    echo "   Found: " . count($mostUsed) . " most used species\n";
    
    // Test 6: Usage statistics
    echo "\n6. Testing usage statistics...\n";
    $stats = $species->getUsageStats();
    echo "   Total species: {$stats['total_especies']}\n";
    echo "   Active species: {$stats['especies_activas']}\n";
    echo "   Used species: {$stats['especies_usadas']}\n";
    echo "   Total records: {$stats['total_registros']}\n";
    
    // Test 7: Species validation
    echo "\n7. Testing species validation...\n";
    $testSpecies = new Species($db);
    $testSpecies->nombre_cientifico = 'Testus invalidus';
    $testSpecies->nombre_comun = 'Test Species';
    $errors = $testSpecies->validate();
    if (empty($errors)) {
        echo "   ✓ Valid species data\n";
    } else {
        echo "   ✗ Validation errors: " . implode(', ', $errors) . "\n";
    }
    
    // Test invalid scientific name format
    $testSpecies->nombre_cientifico = 'invalid name format';
    $errors = $testSpecies->validate();
    if (!empty($errors)) {
        echo "   ✓ Correctly detected invalid scientific name format\n";
    }
    
    echo "\n=== Species API Tests Completed Successfully ===\n";
    
} catch (Exception $e) {
    echo "Error during testing: " . $e->getMessage() . "\n";
    echo "Stack trace: " . $e->getTraceAsString() . "\n";
}
?>