<?php
/**
 * Migration Class
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  Migrations
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 * @copyright   Copyright (C) 2025, 2026 bplan-solutions GmbH & Co. KG <https://www.bplan-solutions.de/>
 * /Δ\
 */


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;


/**
 * Migration Class
 *
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
return new class extends Migration
{

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('model_has_roles');

    } // down()


    /**
     * Run the migrations.
     */
    public function up(): void
    {
        if (Schema::hasTable('model_has_roles') === false) {
            Schema::create('model_has_roles', function (Blueprint $Table) {
                $Table->unsignedBigInteger('model_id');
                $Table->string('model_type');
                $Table->foreignId('role_id')->constrained(table: 'roles', indexName: 'model_has_roles_FK_role_id')->onDelete('cascade');
                $Table->unsignedBigInteger('tenant_id');

                $Table->index(['model_id', 'model_type'], 'model_has_roles_model_id_model_type_index');
                $Table->index('tenant_id',                'model_has_roles_team_foreign_key_index');

                $Table->primary(['tenant_id', 'role_id', 'model_id', 'model_type'], 'model_has_roles_role_model_type_primary');

            });

        }
    } // up()

};
