<?php

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

return new class extends Migration {
    public function up(): void
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->id(); $table->string('name', 64)->unique(); $table->string('label'); $table->timestamps();
        });
        Schema::create('permissions', function (Blueprint $table) {
            $table->id(); $table->string('name', 96)->unique(); $table->string('label'); $table->timestamps();
        });
        Schema::create('role_user', function (Blueprint $table) {
            $table->foreignId('role_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->timestamps(); $table->primary(['role_id', 'user_id']);
        });
        Schema::create('permission_role', function (Blueprint $table) {
            $table->foreignId('permission_id')->constrained()->cascadeOnDelete();
            $table->foreignId('role_id')->constrained()->cascadeOnDelete();
            $table->timestamps(); $table->primary(['permission_id', 'role_id']);
        });

        $now = now();
        DB::table('roles')->insert([
            ['name' => 'super_admin', 'label' => 'Süper Admin', 'created_at' => $now, 'updated_at' => $now],
            ['name' => 'admin', 'label' => 'Admin', 'created_at' => $now, 'updated_at' => $now],
            ['name' => 'support', 'label' => 'Destek', 'created_at' => $now, 'updated_at' => $now],
            ['name' => 'customer', 'label' => 'Müşteri', 'created_at' => $now, 'updated_at' => $now],
        ]);
        DB::table('permissions')->insert([
            ['name' => 'admin.access', 'label' => 'Admin paneline erişim', 'created_at' => $now, 'updated_at' => $now],
            ['name' => 'users.view', 'label' => 'Kullanıcıları görüntüleme', 'created_at' => $now, 'updated_at' => $now],
            ['name' => 'users.manage', 'label' => 'Kullanıcıları yönetme', 'created_at' => $now, 'updated_at' => $now],
            ['name' => 'roles.assign', 'label' => 'Rol atama', 'created_at' => $now, 'updated_at' => $now],
            ['name' => 'system.manage', 'label' => 'Sistem yönetimi', 'created_at' => $now, 'updated_at' => $now],
        ]);
        $roles = DB::table('roles')->pluck('id', 'name');
        $permissions = DB::table('permissions')->pluck('id', 'name');
        $grants = [
            'super_admin' => $permissions->keys()->all(),
            'admin' => ['admin.access', 'users.view', 'users.manage', 'roles.assign'],
            'support' => ['admin.access', 'users.view'],
        ];
        foreach ($grants as $role => $names) foreach ($names as $name) {
            DB::table('permission_role')->insert([
                'role_id' => $roles[$role], 'permission_id' => $permissions[$name],
                'created_at' => $now, 'updated_at' => $now,
            ]);
        }
    }
    public function down(): void
    {
        Schema::dropIfExists('permission_role'); Schema::dropIfExists('role_user');
        Schema::dropIfExists('permissions'); Schema::dropIfExists('roles');
    }
};
