<?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
    {
        if (Schema::hasTable('order_items')) return;
        
        Schema::create('order_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained()->onDelete('cascade');
            $table->foreignId('product_id')->constrained()->onDelete('cascade');
            $table->foreignId('seller_id')->nullable()->constrained()->onDelete('set null');
            $table->string('product_name')->nullable();
            $table->integer('quantity');
            $table->decimal('price', 15, 2)->nullable();
            $table->decimal('unit_price', 15, 2);
            $table->decimal('subtotal', 15, 2)->nullable();
            $table->decimal('total_price', 15, 2);
            
            // Seller commission tracking
            $table->decimal('seller_commission', 15, 2)->default(0);
            $table->decimal('seller_amount', 15, 2)->default(0);
            $table->boolean('seller_paid')->default(false);
            $table->timestamp('seller_paid_at')->nullable();
            
            $table->timestamps();
            
            // Indexes
            $table->index('seller_id');
        });
    }

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

