<?php
// create-logs-table.php
// Script untuk membuat tabel logs jika belum ada

// Include file konfigurasi
require_once 'includes/config.php';
require_once 'includes/database.php';

// Aktifkan error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Buat koneksi database
$db = new Database();

try {
    // Cek apakah tabel logs sudah ada
    $db->query("SHOW TABLES LIKE 'logs'");
    $table_exists = $db->rowCount() > 0;
    
    if (!$table_exists) {
        // Buat tabel logs
        $db->query("CREATE TABLE logs (
            id INT AUTO_INCREMENT PRIMARY KEY,
            user_id INT NOT NULL,
            activity VARCHAR(255) NOT NULL,
            entity_type ENUM('kampanye', 'donasi', 'user', 'berita', 'system') NOT NULL,
            entity_id INT NULL,
            description TEXT NULL,
            ip_address VARCHAR(45) NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )");
        
        if ($db->execute()) {
            echo "Tabel logs berhasil dibuat.";
        } else {
            echo "Gagal membuat tabel logs.";
        }
    } else {
        echo "Tabel logs sudah ada.";
    }
    
    // Cek apakah tabel notifications sudah ada
    $db->query("SHOW TABLES LIKE 'notifications'");
    $table_exists = $db->rowCount() > 0;
    
    if (!$table_exists) {
        // Buat tabel notifications
        $db->query("CREATE TABLE notifications (
            id INT AUTO_INCREMENT PRIMARY KEY,
            user_id INT NOT NULL,
            title VARCHAR(255) NOT NULL,
            message TEXT NOT NULL,
            is_read BOOLEAN DEFAULT FALSE,
            link VARCHAR(255) NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )");
        
        if ($db->execute()) {
            echo "<br>Tabel notifications berhasil dibuat.";
        } else {
            echo "<br>Gagal membuat tabel notifications.";
        }
    } else {
        echo "<br>Tabel notifications sudah ada.";
    }
    
    // Cek apakah tabel financial_reports sudah ada
    $db->query("SHOW TABLES LIKE 'financial_reports'");
    $table_exists = $db->rowCount() > 0;
    
    if (!$table_exists) {
        // Buat tabel financial_reports
        $db->query("CREATE TABLE financial_reports (
            id INT AUTO_INCREMENT PRIMARY KEY,
            title VARCHAR(255) NOT NULL,
            description TEXT NULL,
            period_start DATE NOT NULL,
            period_end DATE NOT NULL,
            total_income DECIMAL(15,2) NOT NULL DEFAULT 0,
            total_expense DECIMAL(15,2) NOT NULL DEFAULT 0,
            file_path VARCHAR(255) NULL,
            created_by INT NOT NULL,
            approved_by INT NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        )");
        
        if ($db->execute()) {
            echo "<br>Tabel financial_reports berhasil dibuat.";
        } else {
            echo "<br>Gagal membuat tabel financial_reports.";
        }
    } else {
        echo "<br>Tabel financial_reports sudah ada.";
    }
    
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}