<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('quotes', function (Blueprint $table) {
            // Add new tracking fields
            $table->timestamp('buyer_viewed_at')->nullable()->after('status');
            $table->text('buyer_response')->nullable()->after('buyer_viewed_at');
            $table->timestamp('seller_revised_at')->nullable()->after('buyer_response');
            $table->decimal('revised_price', 12, 2)->nullable()->after('seller_revised_at');
            $table->text('revised_message')->nullable()->after('revised_price');
            $table->timestamp('expired_at')->nullable()->after('revised_message');
        });

        // Update status enum to include all states
        // SQLite doesn't support ALTER COLUMN for enums, so we handle it differently
        if (DB::getDriverName() !== 'sqlite') {
            DB::statement("ALTER TABLE quotes MODIFY COLUMN status ENUM('pending', 'viewed', 'under_review', 'negotiation', 'revised', 'accepted', 'rejected', 'expired') DEFAULT 'pending'");
        }
    }

    public function down(): void
    {
        Schema::table('quotes', function (Blueprint $table) {
            $table->dropColumn([
                'buyer_viewed_at',
                'buyer_response',
                'seller_revised_at',
                'revised_price',
                'revised_message',
                'expired_at',
            ]);
        });
    }
};
