<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('care_devices', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained('care_branches')->nullOnDelete();
            $table->string('name');
            $table->string('type'); // barcode_scanner, thermometer, …
            $table->string('status')->default('offline'); // active, offline, disabled
            $table->string('connection_mode')->default('manual'); // browser_wedge, agent, web_bluetooth, manual
            $table->string('device_token_hash')->nullable()->unique();
            $table->timestamp('last_seen_at')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['organization_id', 'branch_id', 'status']);
            $table->index(['owner_ref', 'type']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('care_devices');
    }
};
