using bitforum.Constants;
using System.IO;
using System.Security;

namespace bitforum.Models
{
    public static class Permissions
    {
        public static List<string> GeneratePermissionsForMenu(Menu menu, string? parentName = null)
        {
            var permissions = new List<string>();

            // 상위 메뉴 이름과 현재 메뉴 이름을 조합
            var fullName = string.IsNullOrWhiteSpace(parentName) ? $"{menu.Name}.{menu.Id}" : $"{parentName} - {menu.Name}.{menu.Id}";

            // 하위 메뉴가 있는 경우 재귀적으로 권한 생성
            if (menu.Children != null && menu.Children.Any())
            {
                if (menu.Path is not null)
                {
                    permissions.Add($"Permissions.{fullName}.Create");
                    permissions.Add($"Permissions.{fullName}.View");
                    permissions.Add($"Permissions.{fullName}.Edit");
                    permissions.Add($"Permissions.{fullName}.Delete");
                }

                foreach (var child in menu.Children)
                {
                    permissions.AddRange(GeneratePermissionsForMenu(child, menu.Name));
                }
            }
            else
            {
                permissions = new List<string>()
                {
                    $"Permissions.{fullName}.Create",
                    $"Permissions.{fullName}.View",
                    $"Permissions.{fullName}.Edit",
                    $"Permissions.{fullName}.Delete",
                };
            }

            return permissions;
        }

        public static List<string> GeneratePermissions()
        {
            var allPermissions = new List<string>();
            var menus = MenuData.GetMenus();

            foreach (var menu in menus)
            {
                allPermissions.AddRange(GeneratePermissionsForMenu(menu));
            }

            return allPermissions;
        }
    }
}
