<?php
/**
 * Check Device Validation Settings
 * URL: https://absen.ptack.co.id/check-device-validation.php?token=diagnose-2026
 */

$token = 'diagnose-2026';

if (!isset($_GET['token']) || $_GET['token'] !== $token) {
    die('Unauthorized. Use ?token=' . $token);
}

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

echo "<pre style='font-family: monospace; font-size: 12px;'>";
echo "=== DEVICE VALIDATION CHECK ===\n\n";

// 1. Check companies device_validation_enabled
echo "1. COMPANIES SETTINGS\n";
$companies = \App\Models\Company::all();
foreach ($companies as $company) {
    echo "   Company: {$company->name} (ID: {$company->id})\n";
    echo "   - device_validation_enabled: " . ($company->device_validation_enabled ? 'TRUE' : 'FALSE') . "\n";
    echo "   - allow_editable_location: " . ($company->allow_editable_location ? 'TRUE' : 'FALSE') . "\n";
    echo "   - max_devices_per_user: {$company->max_devices_per_user}\n";
    echo "\n";
}

// 2. Check user devices
echo "2. USER DEVICES\n";
$devices = \App\Models\UserDevice::with('user')->get();
echo "   Total devices: " . $devices->count() . "\n\n";

foreach ($devices as $device) {
    echo "   Device ID: " . substr($device->device_id, 0, 20) . "...\n";
    echo "   - User: {$device->user?->name} ({$device->user?->email})\n";
    echo "   - Status: {$device->status}\n";
    echo "   - Model: {$device->device_brand} {$device->device_model}\n";
    echo "\n";
}

// 3. Test specific device_id conflict
echo "3. TESTING DEVICE CONFLICT\n";
$testEmail1 = 'riel.msf28@gmail.com';
$testEmail2 = 'arioraga@gmail.com';

$user1 = \App\Models\User::where('email', $testEmail1)->first();
$user2 = \App\Models\User::where('email', $testEmail2)->first();

if ($user1 && $user2) {
    echo "   User 1: {$user1->name} ({$user1->email})\n";
    echo "   User 2: {$user2->name} ({$user2->email})\n";

    // Get devices for both users
    $devices1 = \App\Models\UserDevice::where('user_id', $user1->id)->get();
    $devices2 = \App\Models\UserDevice::where('user_id', $user2->id)->get();

    echo "\n   User 1 devices:\n";
    foreach ($devices1 as $d) {
        echo "   - {$d->device_id} (status: {$d->status})\n";
    }

    echo "\n   User 2 devices:\n";
    foreach ($devices2 as $d) {
        echo "   - {$d->device_id} (status: {$d->status})\n";
    }

    // Check for overlapping device_ids
    $device1Ids = $devices1->pluck('device_id')->toArray();
    $device2Ids = $devices2->pluck('device_id')->toArray();
    $overlap = array_intersect($device1Ids, $device2Ids);

    echo "\n   Overlapping device_ids: " . count($overlap) . "\n";
    foreach ($overlap as $id) {
        echo "   - $id\n";
    }
} else {
    echo "   Users not found. User1: " . ($user1 ? 'found' : 'not found') . ", User2: " . ($user2 ? 'found' : 'not found') . "\n";
}

// 4. Check AuthController logic
echo "\n4. AUTH CONTROLLER VALIDATION LOGIC\n";
echo "   The validation in AuthController checks:\n";
echo "   - device_id is provided in request\n";
echo "   - company exists AND device_validation_enabled=true\n";
echo "   - If device is registered to another user with status APPROVED or PENDING\n";
echo "\n   If device_validation_enabled=FALSE, validation is SKIPPED!\n";

echo "\n=== CHECK COMPLETE ===\n";
echo "\nPENTING: Hapus file ini setelah selesai!\n";
echo "</pre>";
