<?php
/**
 * Migration Runner - Add Dashboard Granular Permissions
 * This script adds granular dashboard permissions to control which dashboard elements users can see
 * Access this file in your browser: http://localhost/TellerPro/run_dashboard_permissions_migration.php
 */

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

echo '<!DOCTYPE html>
<html>
<head>
    <title>Running Dashboard 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>Dashboard Permissions Migration</h1>';

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

    $permissions = [
        ['name' => 'dashboard.sales_today', 'display_name' => 'View Sales Today', 'description' => 'View Sales Today stat card', 'category' => 'overview'],
        ['name' => 'dashboard.purchase_today', 'display_name' => 'View Purchase Today', 'description' => 'View Purchase Today stat card', 'category' => 'overview'],
        ['name' => 'dashboard.total_products', 'display_name' => 'View Total Products', 'description' => 'View Total Products stat card', 'category' => 'overview'],
        ['name' => 'dashboard.alerts', 'display_name' => 'View Alerts', 'description' => 'View Alerts stat card', 'category' => 'overview'],
        ['name' => 'dashboard.total_month', 'display_name' => 'View Total Month', 'description' => 'View Total This Month stat card', 'category' => 'overview'],
        ['name' => 'dashboard.revenue_chart', 'display_name' => 'View Revenue Chart', 'description' => 'View Revenue chart (Last 7 Days)', 'category' => 'overview'],
        ['name' => 'dashboard.recent_sales', 'display_name' => 'View Recent Sales', 'description' => 'View Recent Sales table', 'category' => 'overview'],
        ['name' => 'dashboard.low_stock', 'display_name' => 'View Low Stock', 'description' => 'View Low Stock section', 'category' => 'overview'],
        ['name' => 'dashboard.expiring', 'display_name' => 'View Expiring', 'description' => 'View Expiring Products section', 'category' => 'overview'],
    ];

    $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 dashboard 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>';
