<?php

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

/**
 * WebDisk Accounts (WebDAV).
 *
 * cPanel "Web Disk" muadili — müşteri /home dizinini Windows Explorer
 * veya macOS Finder'da ağ sürücüsü olarak mount eder. Apache mod_dav kullanır.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('webdisk_accounts', function (Blueprint $table) {
            $table->id();

            $table->foreignId('account_id')
                ->constrained('accounts')
                ->cascadeOnDelete();

            $table->string('username', 100);
            $table->string('password_hash');
            $table->string('home_path', 500)
                ->comment('Webdisk root dizini: /home/<user>/ veya alt klasör');

            $table->enum('permission', ['read', 'read_write'])->default('read_write');

            // Phase 2: per-path restrictions
            $table->json('allowed_paths')->nullable();

            // Digest auth (önerilen — basic auth yerine)
            $table->boolean('use_digest_auth')->default(true);

            $table->bigInteger('quota_mb')->nullable()
                ->comment('Bu webdisk user için ayrı quota, null=hesap quota\'sını paylaş');

            $table->timestamp('last_access_at')->nullable();
            $table->bigInteger('total_bytes_transferred')->default(0);

            $table->boolean('is_active')->default(true);

            $table->timestamps();
            $table->softDeletes();

            $table->unique(['account_id', 'username']);
        });
    }

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