<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class RolePermissionSeeder extends Seeder
{
    public function run(): void
    {
        DB::table('roles')->insert([
            ['name' => 'admin', 'display_name' => 'ادمین', 'description' => 'دسترسی کامل به تمام بخش‌های سیستم', 'guard_name' => 'web', 'created_at' => now(), 'updated_at' => now()],
            ['name' => 'project_manager', 'display_name' => 'مدیر پروژه', 'description' => 'مدیریت پروژه‌ها، وظایف و تیم‌ها', 'guard_name' => 'web', 'created_at' => now(), 'updated_at' => now()],
            ['name' => 'product_manager', 'display_name' => 'مدیر محصول', 'description' => 'مدیریت بک‌لاگ، اولویت‌ها و گزارش محصول', 'guard_name' => 'web', 'created_at' => now(), 'updated_at' => now()],
            ['name' => 'scrum_master', 'display_name' => 'اسکرام مستر', 'description' => 'مدیریت اسپرینت‌ها، جلسات و پیگیری تیم', 'guard_name' => 'web', 'created_at' => now(), 'updated_at' => now()],
            ['name' => 'specialist', 'display_name' => 'کارشناس', 'description' => 'دسترسی پایه برای انجام وظایف محوله', 'guard_name' => 'web', 'created_at' => now(), 'updated_at' => now()],
            ['name' => 'observer', 'display_name' => 'ناظر', 'description' => 'دسترسی فقط مشاهده گزارشات و داشبورد', 'guard_name' => 'web', 'created_at' => now(), 'updated_at' => now()],
            ['name' => 'ceo', 'display_name' => 'مدیر عامل', 'description' => 'دسترسی مشاهده سطح بالا برای مدیران ارشد', 'guard_name' => 'web', 'created_at' => now(), 'updated_at' => now()],
        ]);

        $modules = [
            'projects' => ['view', 'create', 'edit', 'delete', 'assign', 'report'],
            'tasks' => ['view', 'create', 'edit', 'delete', 'assign', 'comment', 'upload'],
            'sprints' => ['view', 'create', 'edit', 'delete'],
            'backlog' => ['view', 'create', 'edit', 'delete', 'approve', 'reject'],
            'meetings' => ['view', 'create', 'edit', 'delete', 'comment'],
            'files' => ['view', 'upload', 'delete'],
            'reports' => ['view', 'create', 'export'],
            'team' => ['view', 'create', 'edit', 'delete'],
            'roles' => ['view', 'create', 'edit', 'delete'],
            'settings' => ['view', 'edit'],
            'notifications' => ['view'],
            'comments' => ['view', 'create', 'edit', 'delete'],
        ];

        $permissions = [];
        foreach ($modules as $module => $actions) {
            foreach ($actions as $action) {
                $name = "{$module}.{$action}";
                $displayMap = [
                    'view' => 'مشاهده',
                    'create' => 'ایجاد',
                    'edit' => 'ویرایش',
                    'delete' => 'حذف',
                    'assign' => 'تخصیص',
                    'comment' => 'نظردهی',
                    'upload' => 'بارگذاری',
                    'approve' => 'تأیید',
                    'reject' => 'رد',
                    'export' => 'خروجی',
                    'report' => 'گزارش',
                ];
                $displayName = $displayMap[$action] ?? $action;
                $moduleDisplay = [
                    'projects' => 'پروژه‌ها',
                    'tasks' => 'وظایف',
                    'sprints' => 'اسپرینت‌ها',
                    'backlog' => 'بک‌لاگ',
                    'meetings' => 'جلسات',
                    'files' => 'فایل‌ها',
                    'reports' => 'گزارشات',
                    'team' => 'تیم',
                    'roles' => 'نقش‌ها',
                    'settings' => 'تنظیمات',
                    'notifications' => 'اعلان‌ها',
                    'comments' => 'نظرات',
                ];
                $permissions[] = [
                    'name' => $name,
                    'display_name' => $displayName.' '.$moduleDisplay[$module],
                    'guard_name' => 'web',
                    'module' => $module,
                    'created_at' => now(),
                    'updated_at' => now(),
                ];
            }
        }
        DB::table('permissions')->insert($permissions);

        $allPermissions = DB::table('permissions')->pluck('id', 'name')->toArray();
        $roles = DB::table('roles')->pluck('id', 'name')->toArray();

        $rolePermissions = [];

        // admin - full access to all
        foreach ($allPermissions as $permId) {
            $rolePermissions[] = ['role_id' => $roles['admin'], 'permission_id' => $permId, 'created_at' => now(), 'updated_at' => now()];
        }

        // project_manager
        $pmModules = ['projects', 'tasks', 'sprints', 'backlog', 'meetings', 'files', 'reports', 'team', 'comments'];
        $pmActions = ['view', 'create', 'edit', 'delete', 'assign', 'comment', 'upload', 'approve', 'reject', 'report', 'export'];
        foreach ($allPermissions as $permName => $permId) {
            [$module, $action] = explode('.', $permName);
            if (in_array($module, $pmModules) && in_array($action, $pmActions)) {
                $rolePermissions[] = ['role_id' => $roles['project_manager'], 'permission_id' => $permId, 'created_at' => now(), 'updated_at' => now()];
            }
        }

        // specialist
        $tmModules = ['tasks', 'meetings', 'files', 'comments', 'notifications'];
        $tmActions = ['view', 'create', 'edit', 'comment', 'upload'];
        foreach ($allPermissions as $permName => $permId) {
            [$module, $action] = explode('.', $permName);
            if (in_array($module, $tmModules) && in_array($action, $tmActions)) {
                $rolePermissions[] = ['role_id' => $roles['specialist'], 'permission_id' => $permId, 'created_at' => now(), 'updated_at' => now()];
            }
        }
        // specialist also can view projects, sprints, backlog, team
        foreach ($allPermissions as $permName => $permId) {
            [$module, $action] = explode('.', $permName);
            if (in_array($module, ['projects', 'sprints', 'backlog', 'team']) && $action === 'view') {
                $rolePermissions[] = ['role_id' => $roles['specialist'], 'permission_id' => $permId, 'created_at' => now(), 'updated_at' => now()];
            }
        }

        $productModules = ['projects', 'tasks', 'backlog', 'reports', 'comments', 'notifications'];
        foreach ($allPermissions as $permName => $permId) {
            [$module, $action] = explode('.', $permName);
            if (in_array($module, $productModules) && in_array($action, ['view', 'create', 'edit', 'approve', 'reject', 'comment', 'report', 'export'])) {
                $rolePermissions[] = ['role_id' => $roles['product_manager'], 'permission_id' => $permId, 'created_at' => now(), 'updated_at' => now()];
            }
        }

        $scrumModules = ['projects', 'tasks', 'sprints', 'meetings', 'reports', 'team', 'comments', 'notifications'];
        foreach ($allPermissions as $permName => $permId) {
            [$module, $action] = explode('.', $permName);
            if (in_array($module, $scrumModules) && in_array($action, ['view', 'create', 'edit', 'assign', 'comment', 'report'])) {
                $rolePermissions[] = ['role_id' => $roles['scrum_master'], 'permission_id' => $permId, 'created_at' => now(), 'updated_at' => now()];
            }
        }

        // observer - view only on projects, tasks, sprints, backlog, meetings, reports, team
        $observerModules = ['projects', 'tasks', 'sprints', 'backlog', 'meetings', 'reports', 'team'];
        foreach ($allPermissions as $permName => $permId) {
            [$module, $action] = explode('.', $permName);
            if (in_array($module, $observerModules) && $action === 'view') {
                $rolePermissions[] = ['role_id' => $roles['observer'], 'permission_id' => $permId, 'created_at' => now(), 'updated_at' => now()];
            }
        }

        // ceo - view + report + export on executive modules
        $ceoModules = ['projects', 'tasks', 'reports', 'team', 'meetings', 'sprints', 'backlog'];
        foreach ($allPermissions as $permName => $permId) {
            [$module, $action] = explode('.', $permName);
            if (in_array($module, $ceoModules) && in_array($action, ['view', 'report', 'export'])) {
                $rolePermissions[] = ['role_id' => $roles['ceo'], 'permission_id' => $permId, 'created_at' => now(), 'updated_at' => now()];
            }
        }

        DB::table('permission_role')->insert($rolePermissions);
    }
}
