<?php
/* ============================================================================
 *  admin/setup_delegate.php — let a manager delegate their approval authority
 *  to another staff member while away. Run once, then delete.
 * ==========================================================================*/
session_start();
require_once '../config/database.php';
header('Content-Type: text/plain; charset=utf-8');

try {
    $pdo->exec("CREATE TABLE IF NOT EXISTS approval_delegates (
        id           INT AUTO_INCREMENT PRIMARY KEY,
        company_id   INT NOT NULL,
        from_user_id INT NOT NULL,          -- the manager delegating
        to_user_id   INT NOT NULL,          -- the stand-in approver
        until_date   DATE NULL,             -- optional end date (NULL = until removed)
        created_at   DATETIME NOT NULL,
        UNIQUE KEY uniq_from (company_id, from_user_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
    echo "OK — approval_delegates table ready\n";
} catch (Throwable $e) { echo "ERR — {$e->getMessage()}\n"; }

echo "\nDone. Managers can now delegate from Company → Approval Settings. Delete this file when finished.\n";