<?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('creances', function (Blueprint $table) {
            $table->id();
            $table->string('reference')->unique();
            $table->foreignId('client_id')->constrained('clients')->cascadeOnDelete();
            // Optionnel: rattacher à une vente précise
            $table->foreignId('vente_id')->nullable()->constrained('ventes')->nullOnDelete();

            $table->decimal('montant_initial', 12, 0);  // montant de la facture
            $table->decimal('montant_restant', 12, 0);  // ce qu’il reste à payer

            $table->string('statut', 20)->default('ouverte');
            // ex: ouverte, partielle, soldée, en_retard

            $table->date('date_creance')->nullable();    // souvent = date de la vente
            $table->date('date_echeance')->nullable();   // si tu gères des échéances
            $table->timestamps();
        });
    }

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