<?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
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('items', function (Blueprint $table) {
            // Add open_to_trades column
            $table->boolean('open_to_trades')->default(false)->after('listing_type');
        });

        // Update existing 'trade' listings to be 'sale' with open_to_trades = true
        DB::table('items')
            ->where('listing_type', 'trade')
            ->update([
                'listing_type' => 'sale',
                'open_to_trades' => true
            ]);

        // For SQLite, we can't modify enum directly, but Laravel will handle validation
        // The original migration created the enum properly, we just need to update data
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Convert sale items with open_to_trades back to trade
        DB::table('items')
            ->where('listing_type', 'sale')
            ->where('open_to_trades', true)
            ->update(['listing_type' => 'trade']);

        Schema::table('items', function (Blueprint $table) {
            $table->dropColumn('open_to_trades');
        });
    }
};