<?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('offers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('item_id')->constrained()->onDelete('cascade');
            $table->foreignId('buyer_id')->constrained('users')->onDelete('cascade');
            $table->foreignId('seller_id')->constrained('users')->onDelete('cascade');
            $table->decimal('offered_price', 10, 2);
            $table->boolean('is_committed')->default(true); // true = committed offer, false = no-commit/fun offer
            $table->enum('status', ['pending', 'accepted', 'counter_offered', 'rejected', 'withdrawn', 'expired'])->default('pending');
            $table->text('message')->nullable(); // Buyer's message with offer
            $table->text('seller_message')->nullable(); // Seller's message when counter-offering or rejecting
            $table->decimal('counter_price', 10, 2)->nullable(); // For counter offers
            $table->timestamp('expires_at')->nullable(); // Offer expiration
            $table->timestamp('accepted_at')->nullable();
            $table->timestamp('rejected_at')->nullable();
            $table->timestamps();

            // Indexes
            $table->index('item_id');
            $table->index('buyer_id');
            $table->index('seller_id');
            $table->index('status');
            $table->index('created_at');
        });
    }

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