<?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('listing_transfers', function (Blueprint $table) {
            $table->id();

            // nullOnDelete (SET NULL) için kolonların nullable olması şart;
            // değilse MariaDB FK'yı reddediyor (errno 150).
            $table->foreignId('listing_id')
            ->nullable()
            ->constrained('listings')
            ->nullOnDelete();

            $table->foreignId('sender_company_id')
            ->nullable()
            ->constrained('companies')
            ->nullOnDelete();

            $table->foreignId('receiver_company_id')
            ->nullable()
            ->constrained('companies')
            ->nullOnDelete();

            $table->enum('status',[
                'pending',
                'approved',
                'rejected'
            ])->default('pending');

            $table->string('transfer_type')->nullable();
            $table->decimal('amount',10,1)->nullable();
            $table->text('description')->nullable();

            $table->text('reject_reason')->nullable();

            $table->timestamps();

            $table->index([
                'receiver_company_id',
                'listing_id'
            ]);

            $table->index([
                'sender_company_id',
                'listing_id'
            ]);
        });
    }

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