<?php

use App\Enums\OrderChannel;
use App\Enums\OrderStatus;
use App\Enums\PaymentType;
use App\Enums\PriceType;
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('orders', function (Blueprint $table) {
            $table->id();

            $table->foreignId('customer_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('marketing_id')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('cash_transaction_id')->nullable()->unique()->constrained()->restrictOnDelete();
            $table->foreignId('created_by_id')->constrained('users')->restrictOnDelete();

            $table->string('order_number', 30)->unique();
            $table->enum('channel', OrderChannel::values());
            $table->enum('price_type', PriceType::values());
            $table->enum('status', OrderStatus::values())->default(OrderStatus::PENDING->value);
            $table->enum('payment_type', PaymentType::values())->default(PaymentType::CASH->value);
            $table->boolean('is_affiliate')->default(false);
            $table->string('tiktok_order_id', 100)->nullable();
            $table->string('shopee_order_id', 100)->nullable();
            $table->unsignedBigInteger('subtotal');
            $table->unsignedBigInteger('discount')->default(0);
            $table->unsignedBigInteger('nego_price')->nullable();
            $table->json('marketplace_settings_snapshot')->nullable();
            $table->unsignedBigInteger('total_amount');
            $table->text('notes')->nullable();

            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();
            $table->softDeletes();
        });
    }

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