<?php

/**
 * 文件功能：创建银行流水记录表 bank_logs
 *
 * 记录每次存款、取款的明细，便于用户查看历史。
 */

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

return new class extends Migration
{
    /**
     * 执行迁移：创建 bank_logs 流水表
     */
    public function up(): void
    {
        Schema::create('bank_logs', function (Blueprint $table): void {
            $table->id();
            $table->unsignedBigInteger('user_id')->comment('操作用户ID');
            $table->enum('type', ['deposit', 'withdraw'])->comment('操作类型：存款/取款');
            $table->integer('amount')->comment('操作金额（正数）');
            $table->integer('balance_after')->comment('操作后 bank_jjb 余额');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->index(['user_id', 'created_at']);
        });
    }

    /**
     * 回滚迁移
     */
    public function down(): void
    {
        Schema::dropIfExists('bank_logs');
    }
};
