<?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('promoter_user', function (Blueprint $table) {
            $table->id();
            $table->foreignId('promoter_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->unique(['promoter_id', 'user_id']);
        });

        Schema::create('authorised_partner_promoter', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('authorised_partner_id');
            $table->unsignedBigInteger('promoter_id');
            $table->foreign('authorised_partner_id', 'ap_promoter_ap_fk')->references('id')->on('authorised_partners')->cascadeOnDelete();
            $table->foreign('promoter_id', 'ap_promoter_p_fk')->references('id')->on('promoters')->cascadeOnDelete();
            $table->unique(['authorised_partner_id', 'promoter_id'], 'ap_promoter_unique');
        });

        Schema::create('authorised_partner_user', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('authorised_partner_id');
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreign('authorised_partner_id', 'ap_user_ap_fk')->references('id')->on('authorised_partners')->cascadeOnDelete();
            $table->unique(['authorised_partner_id', 'user_id'], 'ap_user_unique');
        });

        Schema::create('courseware_partner_authorised_partner', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('courseware_partner_id');
            $table->unsignedBigInteger('authorised_partner_id');
            $table->foreign('courseware_partner_id', 'cp_ap_cp_fk')->references('id')->on('courseware_partners')->cascadeOnDelete();
            $table->foreign('authorised_partner_id', 'cp_ap_ap_fk')->references('id')->on('authorised_partners')->cascadeOnDelete();
            $table->unique(['courseware_partner_id', 'authorised_partner_id'], 'cp_ap_unique');
        });

        Schema::create('courseware_partner_user', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('courseware_partner_id');
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreign('courseware_partner_id', 'cp_user_cp_fk')->references('id')->on('courseware_partners')->cascadeOnDelete();
            $table->unique(['courseware_partner_id', 'user_id'], 'cp_user_unique');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('courseware_partner_user');
        Schema::dropIfExists('courseware_partner_authorised_partner');
        Schema::dropIfExists('authorised_partner_user');
        Schema::dropIfExists('authorised_partner_promoter');
        Schema::dropIfExists('promoter_user');
    }
};
