<?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('b2b_trade_customers', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('member_id')->unique();
            $table->unsignedBigInteger('tier_id')->nullable();
            $table->string('company_name', 255);
            $table->string('phone', 50);
            $table->text('message')->nullable();
            $table->enum('status', ['pending', 'approved', 'rejected'])->default('pending');
            $table->timestamp('approved_at')->nullable();
            $table->unsignedInteger('approved_by')->nullable();
            $table->timestamp('rejected_at')->nullable();
            $table->timestamps();

            $table->foreign('member_id')->references('id')->on('members')->onDelete('cascade');
            $table->foreign('tier_id')->references('id')->on('b2b_trade_tiers')->onDelete('set null');
            $table->index('status');
            $table->index('tier_id');
        });
    }

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