using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ablera.Serdica.Authority.Models;
public static class RouteEntityExtensions
{
    public static SerdicaRoute? FilterAndMapRoute(this RouteEntity route, IEnumerable<string> userRoles)
    {
        // Skip nodes where user is not allowed.
        if (!route.AllowedRoles.Intersect(userRoles).Any())
        {
            return null;
        }

        var children = route.Children
            .Select(child => FilterAndMapRoute(child, userRoles))
            .Where(childDto => childDto != null)
            .Cast<SerdicaRoute>()
            .ToList();

        return new SerdicaRoute(
            Id: route.Id,
            ParentId: route.ParentId,
            ViewConfigId: route.ViewConfigId,
            Type: route.Type,
            Title: route.Title,
            Disabled: route.Disabled,
            IsMenuItem: route.IsMenuItem,
            IsDashboardItem: route.IsDashboardItem,
            Path: route.Path,
            SortOrder: route.SortOrder,
            Icon: route.Icon,
            SvgIcon: route.SvgIcon,
            Breadcrumbs: route.Breadcrumbs,
            Translate: route.Translate,
            ExternalUrl: route.ExternalUrl,
            Url: route.Url,
            Function: route.Function,
            OpenInNewTab: route.OpenInNewTab,
            ExactMatch: route.ExactMatch,
            ProductCode: route.ProductCode,
            ProcessBusinessKey: route.ProcessBusinessKey,
            AllowedRoles: route.AllowedRoles,
            Children: children
        );
    }
}

public class RouteEntity
{
    public Guid Id { get; set; }
    public Guid? ParentId { get; set; }
    public Guid? ViewConfigId { get; set; }
    public string? Type { get; set; }
    public string? Title { get; set; }
    public string? Disabled { get; set; }
    public string? IsMenuItem { get; set; }
    public string? IsDashboardItem { get; set; }
    public required string Path { get; set; }
    public int? SortOrder { get; set; }
    public string? Icon { get; set; }
    public string? SvgIcon { get; set; }
    public string? Breadcrumbs { get; set; }
    public string? Translate { get; set; }
    public string? ExternalUrl { get; set; }
    public string? Url { get; set; }
    public string? Function { get; set; }
    public string? OpenInNewTab { get; set; }
    public string? ExactMatch { get; set; }
    public string? ProductCode { get; set; }
    public string? ProcessBusinessKey { get; set; }
    public required string[] AllowedRoles { get; set; }

    // Children collection for building the tree.
    public List<RouteEntity> Children { get; set; } = new List<RouteEntity>();
}

public record SerdicaRoute
   (
       Guid Id,
       Guid? ParentId,
       Guid? ViewConfigId,
       string? Type,
       string? Title,
       string? Disabled,
       string? IsMenuItem,
       string? IsDashboardItem,
       string? Path,
       int? SortOrder,
       string? Icon,
       string? SvgIcon,
       string? Breadcrumbs,
       string? Translate,
       string? ExternalUrl,
       string? Url,
       string? Function,
       string? OpenInNewTab,
       string? ExactMatch,
       string? ProductCode,
       string? ProcessBusinessKey,
       string[]? AllowedRoles,
       IReadOnlyList<SerdicaRoute> Children
   );