<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Insert new report permissions
        $reportPermissions = [
            [
                'permission_key' => 'reports.view',
                'name' => 'View Reports',
                'description' => 'Can access reports dashboard',
                'group' => 'reports',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'permission_key' => 'reports.user_activity',
                'name' => 'User Activity Report',
                'description' => 'Can view user activity reports',
                'group' => 'reports',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'permission_key' => 'reports.sms_summary',
                'name' => 'SMS Summary Report',
                'description' => 'Can view SMS summary reports',
                'group' => 'reports',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'permission_key' => 'reports.call_summary',
                'name' => 'Call Summary Report',
                'description' => 'Can view call summary reports',
                'group' => 'reports',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'permission_key' => 'reports.promotion_summary',
                'name' => 'Promotion Summary Report',
                'description' => 'Can view promotion summary reports',
                'group' => 'reports',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'permission_key' => 'reports.device_usage',
                'name' => 'Device Usage Report',
                'description' => 'Can view device usage reports',
                'group' => 'reports',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'permission_key' => 'reports.daily_summary',
                'name' => 'Daily Summary Report',
                'description' => 'Can view daily summary reports',
                'group' => 'reports',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'permission_key' => 'reports.export',
                'name' => 'Export Reports',
                'description' => 'Can export reports to Excel/CSV',
                'group' => 'reports',
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ];

        // Insert permissions (skip if already exists)
        foreach ($reportPermissions as $permission) {
            $exists = DB::table('permissions')
                ->where('permission_key', $permission['permission_key'])
                ->exists();

            if (!$exists) {
                DB::table('permissions')->insert($permission);
            }
        }

        // Assign all report permissions to superadmin role
        $superAdminPermissions = array_column($reportPermissions, 'permission_key');
        foreach ($superAdminPermissions as $permissionKey) {
            $exists = DB::table('role_permissions')
                ->where('role', 'superadmin')
                ->where('permission_key', $permissionKey)
                ->exists();

            if (!$exists) {
                DB::table('role_permissions')->insert([
                    'role' => 'superadmin',
                    'permission_key' => $permissionKey,
                    'is_allowed' => true,
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }
        }

        // Assign limited report permissions to admin role
        $adminPermissions = [
            'reports.view',
            'reports.user_activity',
            'reports.sms_summary',
            'reports.call_summary',
        ];

        foreach ($adminPermissions as $permissionKey) {
            $exists = DB::table('role_permissions')
                ->where('role', 'admin')
                ->where('permission_key', $permissionKey)
                ->exists();

            if (!$exists) {
                DB::table('role_permissions')->insert([
                    'role' => 'admin',
                    'permission_key' => $permissionKey,
                    'is_allowed' => true,
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Get report permission keys
        $reportPermissionKeys = [
            'reports.view',
            'reports.user_activity',
            'reports.sms_summary',
            'reports.call_summary',
            'reports.promotion_summary',
            'reports.device_usage',
            'reports.daily_summary',
            'reports.export',
        ];

        // Remove role_permissions entries
        DB::table('role_permissions')
            ->whereIn('permission_key', $reportPermissionKeys)
            ->delete();

        // Remove admin_permissions entries
        DB::table('admin_permissions')
            ->whereIn('permission_key', $reportPermissionKeys)
            ->delete();

        // Remove permissions
        DB::table('permissions')
            ->where('group', 'reports')
            ->delete();
    }
};
