<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

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('dberp_data_import_batches', function (Blueprint $table): void {
            $table->id();
            $table->string('batch_no', 64)->comment('导入批次号');
            $table->string('import_type', 64)->comment('导入类型');
            $table->string('status', 32)->comment('批次状态');
            $table->string('source_filename', 255)->nullable()->comment('原始文件名');
            $table->string('stored_path', 512)->nullable()->comment('私有存储路径');
            $table->foreignId('operator_user_id')
                ->nullable()
                ->comment('操作人')
                ->constrained('users')
                ->nullOnDelete();
            $table->timestamp('started_at')->nullable()->comment('开始时间');
            $table->timestamp('finished_at')->nullable()->comment('结束时间');
            $table->unsignedInteger('total_rows')->default(0)->comment('总行数');
            $table->unsignedInteger('success_rows')->default(0)->comment('成功行数');
            $table->unsignedInteger('failed_rows')->default(0)->comment('失败行数');
            $table->json('error_summary')->nullable()->comment('错误摘要');
            $table->json('options')->nullable()->comment('导入选项');
            $table->string('correlation_id', 36)->nullable()->comment('审计关联 ID');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->unique('batch_no', 'uq_dberp_data_import_batch_no');
            $table->index('import_type', 'idx_dberp_data_import_batch_type');
            $table->index('status', 'idx_dberp_data_import_batch_status');
            $table->index(['correlation_id', 'created_at'], 'idx_dberp_data_import_batch_correlation');
            $table->index('created_at', 'idx_dberp_data_import_batch_created_at');
        });

        Schema::create('dberp_data_import_batch_rows', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('batch_id')
                ->comment('所属批次')
                ->constrained('dberp_data_import_batches')
                ->cascadeOnDelete();
            $table->unsignedInteger('row_number')->comment('源文件行号');
            $table->string('status', 32)->comment('行状态');
            $table->json('raw_snapshot')->nullable()->comment('原始行快照（脱敏）');
            $table->json('error_messages')->nullable()->comment('行级错误信息');
            $table->string('created_record_type', 191)->nullable()->comment('创建记录类型');
            $table->unsignedBigInteger('created_record_id')->nullable()->comment('创建记录 ID');
            $table->string('external_key', 191)->nullable()->comment('外部幂等键');
            $table->timestamps();

            $table->index(['batch_id', 'row_number'], 'idx_dberp_data_import_batch_row_batch_row');
            $table->index('status', 'idx_dberp_data_import_batch_row_status');
            $table->index('external_key', 'idx_dberp_data_import_batch_row_external_key');
        });
    }

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