<?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
    {
        Schema::create('bids', function (Blueprint $table) {
            $table->id();
            $table->string('bid_code')->unique(); // MS-BID-2024-0089
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('auction_vehicle_id')->constrained('auction_vehicles')->cascadeOnDelete();

            // Bid Financials
            $table->decimal('max_bid_jpy', 12, 2);
            $table->decimal('exchange_rate', 8, 4)->default(0.8200); // JPY to BDT rate at bid time
            $table->decimal('estimated_bd_cost_bdt', 14, 2);
            $table->string('currency', 5)->default('JPY');

            // Calculation Breakdown Snapshot (JSON)
            $table->json('cost_breakdown_snapshot')->nullable();

            // Status Workflow
            // pending -> approved / rejected -> submitted (sent to Japan API) -> bidding -> won / lost -> cancelled / expired
            $table->enum('status', [
                'pending',
                'submitted',
                'approved',
                'rejected',
                'bidding',
                'won',
                'lost',
                'cancelled',
                'expired'
            ])->default('pending');

            // Verification & Security
            $table->boolean('terms_accepted')->default(true);
            $table->timestamp('terms_accepted_at')->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();

            // Admin Actions
            $table->foreignId('reviewed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('reviewed_at')->nullable();
            $table->text('admin_notes')->nullable();

            // Japan Auction API Integration Details
            $table->string('japan_api_provider')->nullable(); // mock, generic_api
            $table->string('japan_api_bid_id')->nullable();
            $table->string('japan_api_status')->nullable();
            $table->timestamp('japan_api_submitted_at')->nullable();
            $table->json('japan_api_response')->nullable();

            // Outcome
            $table->decimal('won_hammer_price_jpy', 12, 2)->nullable();
            $table->decimal('final_bd_cost_bdt', 14, 2)->nullable();

            $table->timestamps();

            // Indexes
            $table->index(['user_id', 'status']);
            $table->index('auction_vehicle_id');
            $table->index('status');
        });

        Schema::create('bid_history', function (Blueprint $table) {
            $table->id();
            $table->foreignId('bid_id')->constrained('bids')->cascadeOnDelete();
            $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('action'); // created, approved, rejected, sent_to_japan, won, lost, cancelled
            $table->string('from_status')->nullable();
            $table->string('to_status');
            $table->text('notes')->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('bid_history');
        Schema::dropIfExists('bids');
    }
};
