<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('report_jobs', function (Blueprint $table) {
            $table->id();
            $table->string('job_no', 64)->unique();
            $table->foreignId('admin_user_id')->nullable()->constrained('admin_users')->nullOnDelete();
            $table->string('report_type', 64);
            $table->string('export_format', 16)->default('csv');
            $table->json('filter_json')->nullable();
            $table->string('status', 32);
            $table->string('output_path', 512)->nullable();
            $table->text('error_message')->nullable();
            $table->timestamp('finished_at')->nullable();
            $table->timestamps();
        });

        Schema::create('audit_logs', function (Blueprint $table) {
            $table->id();
            $table->string('operator_type', 16);
            $table->unsignedBigInteger('operator_id')->default(0);
            $table->string('module_code', 32)->nullable();
            $table->string('action_code', 32)->nullable();
            $table->string('target_type', 32)->nullable();
            $table->string('target_id', 64)->nullable();
            $table->json('before_json')->nullable();
            $table->json('after_json')->nullable();
            $table->string('ip', 64)->nullable();
            $table->string('user_agent', 255)->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['operator_type', 'operator_id', 'created_at'], 'idx_audit_logs_operator_time');
            $table->index(['module_code', 'action_code'], 'idx_audit_logs_module_action');
        });

        Schema::create('system_jobs', function (Blueprint $table) {
            $table->id();
            $table->string('job_key', 128)->unique();
            $table->string('name', 128);
            $table->string('schedule_cron', 64)->nullable();
            $table->boolean('is_enabled')->default(true);
            $table->timestamp('last_started_at')->nullable();
            $table->timestamp('last_finished_at')->nullable();
            $table->string('last_status', 32)->nullable();
            $table->timestamps();
        });

        Schema::create('reconcile_jobs', function (Blueprint $table) {
            $table->id();
            $table->string('job_no', 64)->unique();
            $table->string('reconcile_type', 32);
            $table->string('status', 32);
            $table->timestamp('period_start')->nullable();
            $table->timestamp('period_end')->nullable();
            $table->json('summary_json')->nullable();
            $table->timestamp('finished_at')->nullable();
            $table->timestamps();
        });

        Schema::create('reconcile_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('reconcile_job_id')->constrained('reconcile_jobs')->cascadeOnDelete();
            $table->string('side_a_ref', 128)->nullable();
            $table->string('side_b_ref', 128)->nullable();
            $table->bigInteger('difference_amount')->default(0);
            $table->string('status', 32);
            $table->timestamp('resolved_at')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('reconcile_items');
        Schema::dropIfExists('reconcile_jobs');
        Schema::dropIfExists('system_jobs');
        Schema::dropIfExists('audit_logs');
        Schema::dropIfExists('report_jobs');
    }
};
