<?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
    {
        Schema::create('marketplaces', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('agency_id');
            $table->string('name', 100);
            $table->string('slug');
            $table->unsignedBigInteger('default_language_id')->nullable();
            $table->unsignedBigInteger('default_currency_id')->nullable();
            $table->boolean('is_local_only')->default(false);
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->foreign('agency_id')->references('id')->on('agencies')->onDelete('cascade');
            $table->foreign('default_language_id')->references('id')->on('languages')->onDelete('set null');
            $table->foreign('default_currency_id')->references('id')->on('currencies')->onDelete('set null');
        });

        Schema::create('marketplace_languages', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('marketplace_id');
            $table->unsignedBigInteger('language_id');
            $table->timestamps();

            $table->foreign('marketplace_id')->references('id')->on('marketplaces')->onDelete('cascade');
            $table->foreign('language_id')->references('id')->on('languages')->onDelete('cascade');
        });

        Schema::create('marketplace_countries', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('marketplace_id');
            $table->unsignedBigInteger('country_id');
            $table->timestamps();

            $table->foreign('marketplace_id')->references('id')->on('marketplaces')->onDelete('cascade');
            $table->foreign('country_id')->references('id')->on('countries')->onDelete('cascade');
        });

        Schema::create('product_marketplaces', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('product_id');
            $table->unsignedBigInteger('marketplace_id');
            $table->timestamps();

            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->foreign('marketplace_id')->references('id')->on('marketplaces')->onDelete('cascade');
        });

        Schema::table('supplier_price_lists', function (Blueprint $table) {
            $table->foreign('marketplace_id')->references('id')->on('marketplaces')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        if (Schema::hasTable('supplier_price_lists')) {
            Schema::table('supplier_price_lists', function (Blueprint $table) {
                $table->dropForeign(['marketplace_id']);
            });
        }

        Schema::dropIfExists('marketplace_countries');
        Schema::dropIfExists('product_marketplaces');
        Schema::dropIfExists('marketplace_languages');
        Schema::dropIfExists('marketplaces');
    }
};
