<?php
include 'core/vendor/autoload.php';
$app = include_once 'core/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

header('Content-Type: text/plain');
echo "--- REMOTE DATABASE AND CREDENTIALS STATUS ---\n\n";

try {
    $dbName = DB::connection()->getDatabaseName();
    echo "Connected successfully to Database: " . $dbName . "\n\n";
    
    // --- 1. Master Admin (role = 1) ---
    echo "Checking Master Admins (role = 1):\n";
    $master = DB::table('admins')->where('role', 1)->first();
    if (!$master) {
        $passwordHash = Hash::make('admin123');
        DB::table('admins')->insert([
            'name' => 'Master Admin',
            'username' => 'admin',
            'email' => 'admin@example.com',
            'password' => $passwordHash,
            'role' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
        echo "Created Master Admin: admin / admin123\n";
    } else {
        $passwordHash = Hash::make('admin123');
        DB::table('admins')->where('id', $master->id)->update([
            'username' => 'admin',
            'password' => $passwordHash,
            'updated_at' => now(),
        ]);
        echo "Updated Master Admin: admin / admin123\n";
    }
    echo "\n";

    // --- 2. Sub Admin (role = 2) ---
    echo "Checking Sub Admins (role = 2):\n";
    $sub = DB::table('admins')->where('role', 2)->first();
    if (!$sub) {
        $passwordHash = Hash::make('subadmin123');
        DB::table('admins')->insert([
            'name' => 'Sub Admin 1',
            'username' => 'subadmin1',
            'email' => 'subadmin1@example.com',
            'password' => $passwordHash,
            'role' => 2,
            'unique_code' => 'SA001',
            'created_at' => now(),
            'updated_at' => now(),
        ]);
        echo "Created Sub Admin: subadmin1 / subadmin123\n";
    } else {
        $passwordHash = Hash::make('subadmin123');
        DB::table('admins')->where('id', $sub->id)->update([
            'username' => 'subadmin1',
            'password' => $passwordHash,
            'updated_at' => now(),
        ]);
        echo "Updated Sub Admin: subadmin1 / subadmin123\n";
    }
    echo "\n";

    // --- 3. Simple User (user1 / user123) ---
    echo "Checking Regular Users:\n";
    $user = DB::table('users')->where('username', 'user1')->first();
    $passwordHash = Hash::make('user123');
    if (!$user) {
        DB::table('users')->insert([
            'firstname' => 'Test',
            'lastname' => 'User1',
            'username' => 'user1',
            'email' => 'user1@example.com',
            'password' => $passwordHash,
            'balance' => 5000.00,
            'status' => 1,
            'ev' => 1,
            'sv' => 1,
            'subadmin_id' => isset($sub) ? $sub->id : 3,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
        echo "Created User: user1 / user123\n";
    } else {
        DB::table('users')->where('id', $user->id)->update([
            'password' => $passwordHash,
            'updated_at' => now(),
        ]);
        echo "Updated User: user1 / user123\n";
    }
    echo "\n";

} catch (\Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

// Securely delete itself
unlink(__FILE__);
echo "Script self-deleted successfully for security.\n";
