<?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
    {
        // Mutasi credit In/Out (RPD §12.2, BR-13). Setiap perubahan saldo wajib dicatat.
        Schema::create('credit_histories', function (Blueprint $table) {
            $table->id();
            $table->string('invoice', 30)->nullable();          // nomor invoice/referensi
            $table->timestamp('tanggal')->useCurrent();         // waktu mutasi
            $table->text('keterangan')->nullable();             // keterangan (HTML)
            $table->integer('credit');                          // jumlah mutasi (positif)
            $table->enum('jenis', ['In', 'Out'])->default('In'); // arah mutasi
            $table->foreignId('user_id')->constrained()->cascadeOnDelete(); // iduser
            $table->timestamps();

            $table->index(['user_id', 'tanggal']);
        });
    }

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