<?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('credit_histories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('credit_id')->constrained('credits')->onDelete('cascade');
            $table->foreignId('lender_id')->constrained('lenders')->onDelete('cascade');
            $table->foreignId('user_id')->nullable()->constrained('users')->onDelete('cascade');
            $table->string('code', 15)->nullable();
            $table->string('type', 10); // MZ/HZ
            $table->decimal('amount', 15, 2);
            $table->date('date');
            $table->date('due_date');
            $table->string('attachment')->nullable();
            $table->boolean('is_valid')->default(0)->nullable();
            $table->datetime('validation_date')->nullable();
            $table->decimal('total_repayments', 15, 2)->default(0);
            $table->integer('transaction'); // 0 => Delete, 1 => Create, 2 => Update, 3 => Valid
            $table->timestamps();
        });
    }

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