<?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('ops_protocols', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->string('description')->nullable();
            $table->unsignedInteger('sort')->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('ops_softwares', function (Blueprint $table) {
            $table->id();
            $table->foreignId('ops_protocol_id')->constrained('ops_protocols')->cascadeOnDelete();
            $table->string('name');
            $table->string('client_path')->nullable();
            $table->unsignedInteger('sort')->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->unique(['ops_protocol_id', 'name']);
        });

        Schema::create('user_ops_software_preferences', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('ops_protocol_id')->constrained('ops_protocols')->cascadeOnDelete();
            $table->foreignId('ops_software_id')->constrained('ops_softwares')->cascadeOnDelete();
            $table->timestamps();

            $table->unique(['user_id', 'ops_protocol_id'], 'uq_user_ops_protocol');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('user_ops_software_preferences');
        Schema::dropIfExists('ops_softwares');
        Schema::dropIfExists('ops_protocols');
    }
};

