package service

import (
	"context"
	"kra/internal/biz/system"

	"kra/internal/service/dto"
)

type MenuService struct{ uc *system.MenuUsecase }

func NewMenuService(uc *system.MenuUsecase) *MenuService { return &MenuService{uc: uc} }

func (s *MenuService) List(ctx context.Context) ([]*dto.MenuResponse, error) {
	items, err := s.uc.List(ctx)
	if err != nil {
		return nil, err
	}
	return menuResponses(items), nil
}

func (s *MenuService) Tree(ctx context.Context) ([]*dto.MenuResponse, error) {
	items, err := s.uc.Tree(ctx)
	if err != nil {
		return nil, err
	}
	return menuResponses(items), nil
}

func (s *MenuService) Create(ctx context.Context, req *dto.MenuRequest) error {
	return s.uc.Create(ctx, menuDomain(req))
}
func (s *MenuService) Update(ctx context.Context, req *dto.MenuRequest) error {
	return s.uc.Update(ctx, menuDomain(req))
}
func (s *MenuService) Delete(ctx context.Context, id uint) error { return s.uc.Delete(ctx, id) }

func (s *MenuService) Find(ctx context.Context, id uint) (*dto.MenuResponse, error) {
	item, err := s.uc.Find(ctx, id)
	if err != nil {
		return nil, err
	}
	return menuResponse(item), nil
}

func (s *MenuService) SetAuthorityMenus(ctx context.Context, req *dto.SetAuthorityMenusRequest) error {
	ids := make([]uint, 0, len(req.Menus))
	for _, menu := range req.Menus {
		ids = append(ids, menu.ID)
	}
	return s.uc.SetAuthorityMenus(ctx, req.AuthorityID, ids)
}

func (s *MenuService) AuthorityMenus(ctx context.Context, authorityID uint) ([]*dto.AuthorityMenuResponse, error) {
	items, err := s.uc.AuthorityMenus(ctx, authorityID)
	if err != nil {
		return nil, err
	}
	var out []*dto.AuthorityMenuResponse
	for _, item := range items {
		item.MenuID = item.ID
		out = append(out, &dto.AuthorityMenuResponse{DynamicMenuResponse: dynamicMenuResponse(item), AuthorityID: authorityID})
	}
	return out, nil
}

func (s *MenuService) RoleIDs(ctx context.Context, menuID uint) ([]uint, error) {
	return s.uc.RoleIDs(ctx, menuID)
}
func (s *MenuService) DefaultRouterRoleIDs(ctx context.Context, menuID uint) ([]uint, error) {
	return s.uc.DefaultRouterRoleIDs(ctx, menuID)
}
func (s *MenuService) SetRoles(ctx context.Context, req *dto.SetMenuRolesRequest) error {
	return s.uc.SetRoles(ctx, req.MenuID, req.AuthorityIDs)
}

func menuDomain(v *dto.MenuRequest) *system.Menu {
	menu := &system.Menu{CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, ID: v.ID, ParentID: v.ParentID, Path: v.Path, Name: v.Name, Hidden: v.Hidden, Component: v.Component, Sort: v.Sort, ActiveName: v.Meta.ActiveName, KeepAlive: v.Meta.KeepAlive, DefaultMenu: v.Meta.DefaultMenu, Title: v.Meta.Title, Icon: v.Meta.Icon, CloseTab: v.Meta.CloseTab, TransitionType: v.Meta.TransitionType}
	menu.Buttons = make([]*system.MenuButton, 0, len(v.MenuButtons))
	for _, button := range v.MenuButtons {
		menu.Buttons = append(menu.Buttons, &system.MenuButton{CreatedAt: button.CreatedAt, UpdatedAt: button.UpdatedAt, ID: button.ID, Name: button.Name, Description: button.Description, MenuID: v.ID})
	}
	menu.Parameters = make([]*system.MenuParameter, 0, len(v.Parameters))
	for _, parameter := range v.Parameters {
		menu.Parameters = append(menu.Parameters, &system.MenuParameter{CreatedAt: parameter.CreatedAt, UpdatedAt: parameter.UpdatedAt, ID: parameter.ID, MenuID: v.ID, Type: parameter.Type, Key: parameter.Key, Value: parameter.Value})
	}
	return menu
}

func menuResponse(menu *system.Menu) *dto.MenuResponse {
	var children []*dto.MenuResponse
	if menu.Children != nil {
		children = make([]*dto.MenuResponse, 0, len(menu.Children))
		for _, child := range menu.Children {
			children = append(children, menuResponse(child))
		}
	}
	var buttons []dto.MenuButtonResponse
	if menu.Buttons != nil {
		buttons = make([]dto.MenuButtonResponse, 0, len(menu.Buttons))
		for _, button := range menu.Buttons {
			buttons = append(buttons, dto.MenuButtonResponse{CreatedAt: button.CreatedAt, UpdatedAt: button.UpdatedAt, ID: button.ID, Name: button.Name, Description: button.Description, SysBaseMenuID: button.MenuID})
		}
	}
	var parameters []dto.MenuParameterResponse
	if menu.Parameters != nil {
		parameters = make([]dto.MenuParameterResponse, 0, len(menu.Parameters))
		for _, parameter := range menu.Parameters {
			parameters = append(parameters, dto.MenuParameterResponse{CreatedAt: parameter.CreatedAt, UpdatedAt: parameter.UpdatedAt, ID: parameter.ID, SysBaseMenuID: parameter.MenuID, Type: parameter.Type, Key: parameter.Key, Value: parameter.Value})
		}
	}
	return &dto.MenuResponse{CreatedAt: menu.CreatedAt, UpdatedAt: menu.UpdatedAt, ID: menu.ID, MenuID: menu.MenuID, ParentID: menu.ParentID, Path: menu.Path, Name: menu.Name, Hidden: menu.Hidden, Component: menu.Component, Sort: menu.Sort, Meta: dto.MenuMetaResponse{ActiveName: menu.ActiveName, KeepAlive: menu.KeepAlive, DefaultMenu: menu.DefaultMenu, Title: menu.Title, Icon: menu.Icon, CloseTab: menu.CloseTab, TransitionType: menu.TransitionType}, Children: children, Parameters: parameters, MenuButtons: buttons}
}

func dynamicMenuResponse(menu *system.Menu) *dto.DynamicMenuResponse {
	var children []*dto.DynamicMenuResponse
	if menu.Children != nil {
		children = make([]*dto.DynamicMenuResponse, 0, len(menu.Children))
		for _, child := range menu.Children {
			children = append(children, dynamicMenuResponse(child))
		}
	}
	var parameters []dto.MenuParameterResponse
	if menu.Parameters != nil {
		parameters = make([]dto.MenuParameterResponse, 0, len(menu.Parameters))
		for _, parameter := range menu.Parameters {
			parameters = append(parameters, dto.MenuParameterResponse{CreatedAt: parameter.CreatedAt, UpdatedAt: parameter.UpdatedAt, ID: parameter.ID, SysBaseMenuID: parameter.MenuID, Type: parameter.Type, Key: parameter.Key, Value: parameter.Value})
		}
	}
	var buttonAuthorities map[string]uint
	for _, button := range menu.Buttons {
		if button.AuthorityID == 0 {
			continue
		}
		if buttonAuthorities == nil {
			buttonAuthorities = make(map[string]uint)
		}
		buttonAuthorities[button.Name] = button.AuthorityID
	}
	return &dto.DynamicMenuResponse{CreatedAt: menu.CreatedAt, UpdatedAt: menu.UpdatedAt, ID: menu.ID, MenuID: menu.MenuID, ParentID: menu.ParentID, Path: menu.Path, Name: menu.Name, Hidden: menu.Hidden, Component: menu.Component, Sort: menu.Sort, Meta: dto.MenuMetaResponse{ActiveName: menu.ActiveName, KeepAlive: menu.KeepAlive, DefaultMenu: menu.DefaultMenu, Title: menu.Title, Icon: menu.Icon, CloseTab: menu.CloseTab, TransitionType: menu.TransitionType}, Children: children, Parameters: parameters, MenuButtons: nil, Buttons: buttonAuthorities}
}

func menuResponses(items []*system.Menu) []*dto.MenuResponse {
	if items == nil {
		return nil
	}
	out := make([]*dto.MenuResponse, 0, len(items))
	for _, item := range items {
		out = append(out, menuResponse(item))
	}
	return out
}

func dynamicMenuResponses(items []*system.Menu) []*dto.DynamicMenuResponse {
	out := make([]*dto.DynamicMenuResponse, 0, len(items))
	for _, item := range items {
		out = append(out, dynamicMenuResponse(item))
	}
	return out
}
