<?php
require_once '../config/database.php';

echo "<h2>Populating Sample Data for Income Report</h2>";

try {
    // Check if we have any fee payments
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM fee_payments");
    $count = $stmt->fetch()['count'];
    
    echo "<p>Current fee_payments records: $count</p>";
    
    if ($count == 0) {
        echo "<p>Creating sample fee payment records...</p>";
        
        // First, check if we have fees
        $stmt = $pdo->query("SELECT COUNT(*) as count FROM fees");
        $fee_count = $stmt->fetch()['count'];
        
        if ($fee_count == 0) {
            echo "<p>No fees found. Creating sample fees first...</p>";
            
            // Create sample students if needed
            $stmt = $pdo->query("SELECT COUNT(*) as count FROM students");
            $student_count = $stmt->fetch()['count'];
            
            if ($student_count == 0) {
                echo "<p>Creating sample student...</p>";
                $pdo->exec("INSERT INTO students (student_id, first_name, last_name, email, phone, class_id, roll_number, admission_date) VALUES ('STU001', 'রহিম', 'খান', 'rahim@example.com', '01712345678', 1, 1, '2026-01-15')");
            }
            
            // Create sample fees
            $pdo->exec("INSERT INTO fees (student_id, fee_type, amount, due_date, paid_amount, paid_date, status) VALUES (1, 'মাসিক ফি', 1000.00, '2026-01-31', 1000.00, '2026-01-20', 'paid')");
            $pdo->exec("INSERT INTO fees (student_id, fee_type, amount, due_date, paid_amount, paid_date, status) VALUES (1, 'পরীক্ষা ফি', 500.00, '2026-02-28', 500.00, '2026-01-15', 'paid')");
            echo "<p>✅ Sample fees created</p>";
        }
        
        // Create sample fee payments
        $payments = [
            ['fee_id' => 1, 'amount' => 1000.00, 'payment_date' => '2026-01-20', 'payment_method' => 'cash', 'created_by' => 1],
            ['fee_id' => 2, 'amount' => 500.00, 'payment_date' => '2026-01-15', 'payment_method' => 'bank_transfer', 'created_by' => 1],
            ['fee_id' => 1, 'amount' => 1000.00, 'payment_date' => '2026-01-25', 'payment_method' => 'mobile_banking', 'created_by' => 1],
            ['fee_id' => 2, 'amount' => 500.00, 'payment_date' => '2026-01-10', 'payment_method' => 'cash', 'created_by' => 1],
        ];
        
        foreach ($payments as $payment) {
            $stmt = $pdo->prepare("INSERT INTO fee_payments (fee_id, amount, payment_date, payment_method, created_by) VALUES (?, ?, ?, ?, ?)");
            $stmt->execute([$payment['fee_id'], $payment['amount'], $payment['payment_date'], $payment['payment_method'], $payment['created_by']]);
        }
        
        echo "<p>✅ Sample fee payment records created</p>";
        
        // Verify insertion
        $stmt = $pdo->query("SELECT COUNT(*) as count FROM fee_payments");
        $new_count = $stmt->fetch()['count'];
        echo "<p>New total fee_payments records: $new_count</p>";
        
    } else {
        echo "<p>✅ Existing data found. No need to populate sample data.</p>";
    }
    
    // Show recent payments
    echo "<h3>Recent Payments:</h3>";
    $stmt = $pdo->query("
        SELECT fp.*, f.fee_type, s.student_name 
        FROM fee_payments fp 
        LEFT JOIN fees f ON fp.fee_id = f.id 
        LEFT JOIN students s ON f.student_id = s.id 
        ORDER BY fp.payment_date DESC 
        LIMIT 10
    ");
    $payments = $stmt->fetchAll();
    
    if (!empty($payments)) {
        echo "<table border='1' cellpadding='5'>";
        echo "<tr><th>Date</th><th>Student</th><th>Fee Type</th><th>Amount</th><th>Method</th></tr>";
        foreach ($payments as $payment) {
            echo "<tr>";
            echo "<td>" . $payment['payment_date'] . "</td>";
            echo "<td>" . ($payment['student_name'] ?? 'N/A') . "</td>";
            echo "<td>" . ($payment['fee_type'] ?? 'N/A') . "</td>";
            echo "<td>৳" . number_format($payment['amount'], 2) . "</td>";
            echo "<td>" . $payment['payment_method'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    
    echo "<hr>";
    echo "<p><a href='income_report.php'>Test Income Report Page</a></p>";
    
} catch (Exception $e) {
    echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
}
?>