<?php

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

/**
 * Satışa bağlı faturalar (satış quick-view "Faturalar" sekmesi).
 * Fatura belgesi (PDF/görsel) opsiyonel olarak dosya şeklinde eklenebilir.
 */
return new class extends Migration {
    public function up(): void
    {
        Schema::create('sale_invoices', function (Blueprint $t) {
            $t->id();
            $t->unsignedBigInteger('agency_id');
            $t->unsignedBigInteger('sale_id');
            $t->unsignedBigInteger('created_by')->nullable();
            $t->string('invoice_number');                                        // Fatura no (seri + sıra)
            $t->enum('invoice_type', ['sale', 'refund'])->default('sale');       // Satış / iade faturası
            $t->date('issue_date');                                              // Kesim tarihi
            $t->decimal('amount', 12, 2)->default(0);
            $t->string('currency', 3)->default('TRY');
            $t->enum('status', ['draft', 'issued', 'paid', 'cancelled'])->default('issued');
            $t->text('notes')->nullable();
            $t->string('file_path')->nullable();                                 // Yüklü fatura belgesi
            $t->string('file_name')->nullable();
            $t->unsignedBigInteger('file_size')->nullable();
            $t->timestamps();

            $t->foreign('agency_id')->references('id')->on('agencies')->onDelete('cascade');
            $t->foreign('sale_id')->references('id')->on('sales')->onDelete('cascade');
            $t->foreign('created_by')->references('id')->on('users')->onDelete('set null');
            $t->index(['agency_id', 'sale_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('sale_invoices');
    }
};
