<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('data_exports', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete()->comment('테넌트 ID');
            $table->string('export_type', 50)->comment('내보내기 유형: all, users, products, orders, clients');
            $table->string('status', 20)->default('pending')->comment('상태: pending, processing, completed, failed');
            $table->string('file_path')->nullable()->comment('생성된 파일 경로');
            $table->string('file_name')->nullable()->comment('다운로드 파일명');
            $table->unsignedBigInteger('file_size')->nullable()->comment('파일 크기 (bytes)');
            $table->json('options')->nullable()->comment('내보내기 옵션');
            $table->timestamp('started_at')->nullable()->comment('처리 시작 시간');
            $table->timestamp('completed_at')->nullable()->comment('처리 완료 시간');
            $table->text('error_message')->nullable()->comment('에러 메시지');
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete()->comment('생성자');
            $table->timestamps();

            $table->index(['tenant_id', 'status']);
            $table->index(['tenant_id', 'created_at']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('data_exports');
    }
};
