<?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
    {
        // Conversations table
        Schema::create('conversations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('vendor_id')->nullable()->constrained()->onDelete('cascade');
            $table->string('subject')->nullable();
            $table->enum('type', ['support', 'vendor_inquiry', 'order', 'general'])->default('general');
            $table->enum('status', ['open', 'closed', 'pending'])->default('open');
            $table->foreignId('order_id')->nullable()->constrained()->onDelete('set null');
            $table->timestamp('last_message_at')->nullable();
            $table->timestamps();
            
            $table->index(['user_id', 'status']);
            $table->index(['vendor_id', 'status']);
        });

        // Messages table
        Schema::create('messages', function (Blueprint $table) {
            $table->id();
            $table->foreignId('conversation_id')->constrained()->onDelete('cascade');
            $table->morphs('sender'); // Can be User or Vendor
            $table->text('content');
            $table->json('attachments')->nullable();
            $table->boolean('is_read')->default(false);
            $table->timestamp('read_at')->nullable();
            $table->timestamps();
            
            $table->index(['conversation_id', 'created_at']);
        });

        // Add currency fields to orders
        Schema::table('orders', function (Blueprint $table) {
            $table->string('currency_code', 3)->default('USD')->after('total');
            $table->decimal('exchange_rate', 10, 6)->default(1.000000)->after('currency_code');
        });

        // Create currency settings table
        Schema::create('currencies', function (Blueprint $table) {
            $table->id();
            $table->string('code', 3)->unique();
            $table->string('name');
            $table->string('symbol', 10);
            $table->enum('symbol_position', ['left', 'right'])->default('left');
            $table->decimal('exchange_rate', 10, 6)->default(1.000000);
            $table->boolean('is_default')->default(false);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // Add vendor status history tracking
        Schema::create('vendor_status_histories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('vendor_id')->constrained()->onDelete('cascade');
            $table->string('old_status')->nullable();
            $table->string('new_status');
            $table->text('reason')->nullable();
            $table->foreignId('changed_by')->constrained('users')->onDelete('cascade');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('vendor_status_histories');
        Schema::dropIfExists('currencies');
        Schema::table('orders', function (Blueprint $table) {
            $table->dropColumn(['currency_code', 'exchange_rate']);
        });
        Schema::dropIfExists('messages');
        Schema::dropIfExists('conversations');
    }
};
