<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once __DIR__ . '/../app/config/db.php';
$pdo = crm_pdo();
$results = [];

$alters = [
    // ── Existing columns (safe IF NOT EXISTS) ──────────────────────────────
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_theme`     ENUM('light','dark','system') NOT NULL DEFAULT 'system'",
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_expenses`   TINYINT(1) NOT NULL DEFAULT 0",
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_reports`    TINYINT(1) NOT NULL DEFAULT 0",
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_settings`   TINYINT(1) NOT NULL DEFAULT 0",
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_branding`   TINYINT(1) NOT NULL DEFAULT 0",
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_users`      TINYINT(1) NOT NULL DEFAULT 0",
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_logs`       TINYINT(1) NOT NULL DEFAULT 0",
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_payroll`    TINYINT(1) NOT NULL DEFAULT 0",
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_dispatch`   TINYINT(1) NOT NULL DEFAULT 0",
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_warehouse`  TINYINT(1) NOT NULL DEFAULT 0",

    // ── NEW v1.3.1 columns ─────────────────────────────────────────────────
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_costings`   TINYINT(1) NOT NULL DEFAULT 0 COMMENT 'View project costings & partner quotes'",
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_assign`     TINYINT(1) NOT NULL DEFAULT 0 COMMENT 'Assign jobs to partners & employees'",
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_customers`  TINYINT(1) NOT NULL DEFAULT 0 COMMENT 'Create & edit customers'",
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_invoices`   TINYINT(1) NOT NULL DEFAULT 0 COMMENT 'View & manage invoices / payments'",
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_financials` TINYINT(1) NOT NULL DEFAULT 0 COMMENT 'View full financial summaries & P&L'",
    "ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `perm_photos`     TINYINT(1) NOT NULL DEFAULT 0 COMMENT 'Admin gallery — manage all project photos'",
];

foreach ($alters as $sql) {
    try {
        $pdo->exec($sql);
        $results[] = "✅ OK: " . substr($sql, 0, 80) . "...";
    } catch (Exception $e) {
        $results[] = "❌ ERR: " . $e->getMessage();
    }
}

// Grant all perms to admin/manager
try {
    $pdo->exec("UPDATE users SET
        perm_expenses=1, perm_reports=1, perm_settings=1, perm_branding=1,
        perm_users=1, perm_logs=1, perm_payroll=1, perm_dispatch=1, perm_warehouse=1,
        perm_costings=1, perm_assign=1, perm_customers=1, perm_invoices=1,
        perm_financials=1, perm_photos=1
        WHERE role IN ('admin','manager')");
    $results[] = "✅ OK: admin & manager — all permissions granted";
} catch (Exception $e) {
    $results[] = "❌ ERR: " . $e->getMessage();
}

echo '<pre style="font-family:monospace;background:#111;color:#0f0;padding:1rem;border-radius:8px">';
foreach ($results as $r) echo htmlspecialchars($r) . "\n";
echo '</pre>';
echo '<h2>Done! Delete this file from the server after deployment.</h2>';