<?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('nas_mikrotiks', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->string('connection_type', 20);
            $table->string('host');
            $table->unsignedSmallInteger('api_port')->nullable()->default(8728);
            $table->string('api_username')->nullable();
            $table->text('api_password')->nullable();
            $table->unsignedSmallInteger('radius_auth_port')->nullable()->default(1812);
            $table->unsignedSmallInteger('radius_accounting_port')->nullable()->default(1813);
            $table->text('radius_secret')->nullable();
            $table->unsignedSmallInteger('timeout')->default(10);
            $table->string('status', 20)->default('active');
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->unique(['tenant_id', 'name']);
            $table->index(['tenant_id', 'connection_type', 'status']);
        });

        Schema::create('nas_package_profiles', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->string('service_type', 20);
            $table->string('external_profile_name')->nullable();
            $table->unsignedBigInteger('download_kbps')->nullable();
            $table->unsignedBigInteger('upload_kbps')->nullable();
            $table->string('local_address')->nullable();
            $table->string('remote_address_pool')->nullable();
            $table->unsignedInteger('session_timeout')->nullable();
            $table->unsignedInteger('idle_timeout')->nullable();
            $table->unsignedInteger('shared_users')->default(1);
            $table->decimal('price', 15, 2)->default(0);
            $table->string('status', 20)->default('active');
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->unique(['tenant_id', 'name']);
            $table->index(['tenant_id', 'service_type', 'status']);
        });

        Schema::create('nas_olts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->string('vendor')->nullable();
            $table->string('model')->nullable();
            $table->string('host');
            $table->unsignedSmallInteger('snmp_port')->default(161);
            $table->string('snmp_version', 10)->default('v2c');
            $table->text('community')->nullable();
            $table->string('security_level', 30)->nullable();
            $table->string('security_name')->nullable();
            $table->string('auth_protocol', 10)->nullable();
            $table->text('auth_password')->nullable();
            $table->string('privacy_protocol', 10)->nullable();
            $table->text('privacy_password')->nullable();
            $table->unsignedSmallInteger('timeout')->default(5);
            $table->string('status', 20)->default('active');
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->unique(['tenant_id', 'name']);
            $table->index(['tenant_id', 'status']);
        });

        Schema::create('nas_webfig_devices', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->string('device_type')->nullable();
            $table->text('url');
            $table->string('username')->nullable();
            $table->text('password')->nullable();
            $table->string('status', 20)->default('active');
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->unique(['tenant_id', 'name']);
            $table->index(['tenant_id', 'status']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('nas_webfig_devices');
        Schema::dropIfExists('nas_olts');
        Schema::dropIfExists('nas_package_profiles');
        Schema::dropIfExists('nas_mikrotiks');
    }
};
