<?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('items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('category_id')->nullable()->constrained()->onDelete('set null');
            $table->foreignId('manufacturer_id')->nullable()->constrained()->onDelete('set null');
            $table->string('title');
            $table->text('description');
            $table->string('model')->nullable();
            $table->string('serial_number')->nullable();
            $table->year('year')->nullable();
            $table->string('condition');
            $table->decimal('price', 10, 2)->nullable();
            $table->enum('status', ['draft', 'active', 'sold', 'trade_pending', 'sale_pending', 'archived'])->default('draft');
            $table->enum('listing_type', ['sale', 'trade', 'inventory'])->default('sale');
            $table->string('location_city')->nullable();
            $table->string('location_state')->nullable();
            $table->string('location_country')->default('US');
            $table->boolean('shipping_available')->default(false);
            $table->decimal('shipping_cost', 8, 2)->nullable();
            $table->boolean('local_pickup')->default(true);
            $table->integer('views')->default(0);
            $table->timestamp('featured_until')->nullable();
            $table->timestamps();
            
            $table->index(['status', 'listing_type']);
            $table->index(['category_id', 'status']);
            $table->index('created_at');
        });
    }

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