<?php
require __DIR__ . '/vendor/autoload.php';
$app = require_once __DIR__ . '/bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

use Illuminate\Support\Facades\DB;

$user = App\Models\User::where('email', 'admin@csc-eifelgreen.org')->first();
if (!$user) {
    echo "USER_NOT_FOUND\n";
    exit(1);
}

// Attach user to root team (team 0) as owner
$exists = DB::table('team_user')->where('team_id', 0)->where('user_id', $user->id)->exists();
if (!$exists) {
    DB::table('team_user')->insert([
        'team_id' => 0,
        'user_id' => $user->id,
        'role' => 'owner',
        'created_at' => now(),
        'updated_at' => now(),
    ]);
    echo "USER_ADDED_TO_ROOT_TEAM\n";
} else {
    echo "USER_ALREADY_IN_ROOT_TEAM\n";
}

// Verify
$members = DB::select("SELECT user_id, role FROM team_user WHERE team_id = 0");
foreach ($members as $m) {
    echo "ROOT_TEAM_MEMBER user_id:{$m->user_id} role:{$m->role}\n";
}
