<?php
/**
 * Migration Runner - Add Missing Report Permissions
 * This script adds missing report permissions to cover all reports in the system
 * Access this file in your browser: http://localhost/TellerPro/run_report_permissions_migration.php
 */

require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/config/database.php';

echo '<!DOCTYPE html>
<html>
<head>
    <title>Running Report Permissions Migration</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 20px; max-width: 800px; margin: 0 auto; }
        .success { color: green; }
        .error { color: red; }
        .info { color: blue; }
    </style>
</head>
<body>';

echo '<h1>Report Permissions Migration</h1>';

try {
    echo '<p class="info">Starting migration...</p>';

    $permissions = [
        ['name' => 'reports.product_sales', 'display_name' => 'Product Sales Reports', 'description' => 'View product sales reports', 'category' => 'reports'],
        ['name' => 'reports.daily_stock_monitoring', 'display_name' => 'Daily Stock Monitoring', 'description' => 'View daily stock monitoring reports', 'category' => 'reports'],
        ['name' => 'reports.stock_movement', 'display_name' => 'Stock Movement', 'description' => 'View stock movement reports', 'category' => 'reports'],
        ['name' => 'reports.stock_trending', 'display_name' => 'Stock Trending', 'description' => 'View stock trending reports', 'category' => 'reports'],
        ['name' => 'reports.ai_analytics', 'display_name' => 'AI Analytics', 'description' => 'View AI analytics reports', 'category' => 'reports'],
        ['name' => 'reports.tax', 'display_name' => 'Tax Reports', 'description' => 'View tax reports', 'category' => 'reports'],
        ['name' => 'reports.stock_summary', 'display_name' => 'Stock Summary', 'description' => 'View stock summary reports', 'category' => 'reports'],
        ['name' => 'reports.purchase_items', 'display_name' => 'Purchase Items Reports', 'description' => 'View purchase items reports', 'category' => 'reports'],
        ['name' => 'reports.top_suppliers', 'display_name' => 'Top Suppliers', 'description' => 'View top suppliers reports', 'category' => 'reports'],
        ['name' => 'reports.purchase_pay_due', 'display_name' => 'Purchase Pay Due', 'description' => 'View purchase payment due reports', 'category' => 'reports'],
    ];

    $added = 0;
    foreach ($permissions as $perm) {
        $sql = "INSERT IGNORE INTO permissions (name, display_name, description, category) VALUES (?, ?, ?, ?)";
        $stmt = db()->prepare($sql);
        $stmt->execute([$perm['name'], $perm['display_name'], $perm['description'], $perm['category']]);
        if ($stmt->rowCount() > 0) {
            $added++;
            echo "<p class='success'>Added permission: {$perm['display_name']}</p>";
        }
    }

    echo '<h2 class="success">Migration completed successfully!</h2>';
    echo "<p>Added {$added} new report permissions.</p>";
    echo '<p><a href="' . APP_URL . '/roles/create">Go to Create New Role</a></p>';

} catch (Exception $e) {
    echo '<h2 class="error">Migration failed</h2>';
    echo '<pre class="error">' . htmlspecialchars($e->getMessage()) . '</pre>';
    echo '<p>Please check your database configuration and try again.</p>';
}

echo '</body>
</html>';
