<?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('invoice_types', function (Blueprint $table) {
            $table->string('slug')->primary();
            $table->string('name');
            $table->timestamps();
        });

        Schema::create('invoice_status', function (Blueprint $table) {
            $table->string('slug')->primary();
            $table->timestamps();
        });

        Schema::create('invoices', function (Blueprint $table) {
            $table->id();
            $table->string('tenant');
            $table->foreignId('cost_unit_id')->constrained('cost_units', 'id')->restrictOnDelete()->cascadeOnUpdate();
            $table->foreignId('user_id')->nullable()->constrained('users', 'id')->restrictOnDelete()->cascadeOnUpdate();
            $table->string('invoice_number');
            $table->string('status');
            $table->string('type');
            $table->string('type_other')->nullable();
            $table->boolean('donation')->default(false);
            $table->string('contact_name');
            $table->string('contact_email')->nullable();
            $table->string('contact_phone')->nullable();
            $table->string('contact_bank_owner')->nullable();
            $table->string('contact_bank_iban')->nullable();
            $table->float('amount', 2);
            $table->integer('distance')->nullable();
            $table->string('comment')->nullable();
            $table->longText('changes')->nullable();
            $table->string('travel_direction')->nullable();
            $table->string('travel_reason')->nullable();
            $table->boolean('passengers')->nullable();
            $table->boolean('transportation')->nullable();
            $table->string('document_filename')->nullable();
            $table->foreignId('approved_by')->nullable()->constrained('users', 'id')->restrictOnDelete()->cascadeOnUpdate();
            $table->dateTime('approved_at')->nullable();
            $table->boolean('upload_required')->default(false);
            $table->foreignId('denied_by')->nullable()->constrained('users', 'id')->restrictOnDelete()->cascadeOnUpdate();
            $table->dateTime('denied_at')->nullable();
            $table->string('denied_reason')->nullable();

            $table->foreign('tenant')->references('slug')->on('tenants')->restrictOnDelete()->cascadeOnUpdate();
            $table->foreign('type')->references('slug')->on('invoice_types')->restrictOnDelete()->cascadeOnUpdate();
            $table->foreign('status')->references('slug')->on('invoice_status')->restrictOnDelete()->cascadeOnUpdate();

            $table->timestamps();
        });

    }

    public function down(): void
    {
        Schema::dropIfExists('invoices');
        Schema::dropIfExists('invoice_status');
        Schema::dropIfExists('invoice_types');
    }
};
