<?php

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

return new class extends Migration
{
    public function up(): void
    {
        // Regions & Schools (for intern/donor driving)
        Schema::create('regions', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique(); // New England, Florida, etc.
            $table->string('code', 10)->nullable();
            $table->timestamps();
        });

        Schema::create('schools', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('short_name')->nullable();
            $table->foreignId('region_id')->nullable()->constrained()->nullOnDelete();
            $table->string('logo_url')->nullable();
            $table->timestamps();
        });

        Schema::create('auctions', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('slug')->unique();
            $table->text('description')->nullable();
            $table->timestamp('starts_at')->nullable();
            $table->timestamp('ends_at')->nullable();
            $table->string('status')->default('draft'); // draft, published, live, closed
            $table->boolean('is_public')->default(true);
            $table->json('settings')->nullable(); // increments table, anti-snipe minutes, etc.
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('lots', function (Blueprint $table) {
            $table->id();
            $table->foreignId('auction_id')->constrained()->cascadeOnDelete();
            $table->string('title');
            $table->string('slug');
            $table->text('description')->nullable();
            $table->decimal('starting_bid', 12, 2)->default(0);
            $table->decimal('current_bid', 12, 2)->default(0);
            $table->decimal('buy_now_price', 12, 2)->nullable();
            $table->decimal('reserve_price', 12, 2)->nullable();
            $table->decimal('bid_increment', 8, 2)->default(10.00);
            $table->timestamp('starts_at')->nullable();
            $table->timestamp('ends_at')->nullable();
            $table->string('status')->default('draft');
            $table->json('ai_analysis')->nullable(); // full AI payload
            $table->unsignedBigInteger('category_id')->nullable();
            $table->string('condition')->nullable();
            $table->string('serial_number')->nullable();
            $table->boolean('is_featured')->default(false);
            $table->unsignedInteger('view_count')->default(0);
            $table->unsignedInteger('watch_count')->default(0);
            $table->timestamps();
            $table->softDeletes();

            $table->index(['auction_id', 'status']);
            $table->index('ends_at');
        });

        Schema::create('bids', function (Blueprint $table) {
            $table->id();
            $table->foreignId('lot_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->decimal('amount', 12, 2);
            $table->boolean('is_proxy')->default(false);
            $table->decimal('max_proxy', 12, 2)->nullable();
            $table->string('status')->default('active'); // active, outbid, winning, cancelled
            $table->json('meta')->nullable();
            $table->timestamps();

            $table->index(['lot_id', 'amount']);
        });

        Schema::create('donors', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('first_name');
            $table->string('last_name');
            $table->string('email')->index();
            $table->string('phone')->nullable();
            $table->foreignId('region_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('school_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('assigned_intern_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('status')->default('new'); // new, contacted, pledged, donated, lapsed
            $table->text('notes')->nullable();
            $table->json('custom_fields')->nullable();
            $table->timestamp('last_contacted_at')->nullable();
            $table->decimal('total_donated', 12, 2)->default(0);
            $table->string('source')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['region_id', 'school_id', 'status']);
        });

        Schema::create('donations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('campaign_id')->nullable(); // if multi-campaign
            $table->foreignId('donor_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->decimal('amount', 12, 2);
            $table->string('type')->default('donation'); // donation, auction_win, pledge
            $table->string('status')->default('completed');
            $table->string('payment_method')->nullable();
            $table->string('transaction_id')->nullable();
            $table->json('meta')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('donations');
        Schema::dropIfExists('donors');
        Schema::dropIfExists('bids');
        Schema::dropIfExists('lots');
        Schema::dropIfExists('auctions');
        Schema::dropIfExists('schools');
        Schema::dropIfExists('regions');
    }
};
