<?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_permissions');

    } // down()


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

                $Table->index(['model_id', 'model_type'], 'model_has_permissions_model_id_model_type_index');
                $Table->index('tenant_id',                'model_has_permissions_team_foreign_key_index');

                $Table->primary(['tenant_id', 'permission_id', 'model_id', 'model_type'], 'model_has_permissions_permission_model_type_primary');

            });

        }
    } // up()

};
