<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $employees = $db->select("SELECT id, branch_id FROM employees WHERE branch_id IS NOT NULL AND is_archived = 0");

    foreach ($employees as $emp) {
        $exists = $db->selectOne(
            "SELECT 1 FROM employee_branches WHERE employee_id = ? AND branch_id = ?",
            [(int) $emp['id'], (int) $emp['branch_id']]
        );

        if (!$exists) {
            $db->insert('employee_branches', [
                'employee_id' => (int) $emp['id'],
                'branch_id'   => (int) $emp['branch_id'],
                'is_primary'  => 1,
                'assigned_at' => date('Y-m-d H:i:s'),
            ]);
        }
    }
};
