package service

import (
	"context"

	"kra/internal/biz"
	"kra/internal/service/dto"
)

type DictionaryService struct{ uc *biz.DictionaryUsecase }

func NewDictionaryService(uc *biz.DictionaryUsecase) *DictionaryService {
	return &DictionaryService{uc: uc}
}

func dictionaryDomain(value *dto.DictionaryRequest) *biz.Dictionary {
	return &biz.Dictionary{ID: value.ID, Name: value.Name, Type: value.Type, Status: value.Status, Desc: value.Description, ParentID: value.ParentID}
}
func (s *DictionaryService) CreateDictionaryRequest(ctx context.Context, req *dto.DictionaryRequest) (*dto.DictionaryResponse, error) {
	value := dictionaryDomain(req)
	if err := s.CreateDictionary(ctx, value); err != nil {
		return nil, err
	}
	return dictionaryDTO(value), nil
}
func (s *DictionaryService) UpdateDictionaryRequest(ctx context.Context, req *dto.DictionaryRequest) error {
	return s.UpdateDictionary(ctx, dictionaryDomain(req))
}
func dictionaryDTO(v *biz.Dictionary) *dto.DictionaryResponse {
	var children []*dto.DictionaryResponse
	if v.Children != nil {
		children = make([]*dto.DictionaryResponse, 0, len(v.Children))
		for _, x := range v.Children {
			children = append(children, dictionaryDTO(x))
		}
	}
	var details []*dto.DictionaryDetailResponse
	if v.Details != nil {
		details = make([]*dto.DictionaryDetailResponse, 0, len(v.Details))
		for _, x := range v.Details {
			details = append(details, detailDTO(x))
		}
	}
	return &dto.DictionaryResponse{ID: v.ID, CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, DeletedAt: nil, Name: v.Name, Type: v.Type, Status: v.Status, Desc: v.Desc, ParentID: v.ParentID, Children: children, SysDictionaryDetails: details}
}
func (s *DictionaryService) Dictionaries(ctx context.Context, page, size int, name, typ string, details bool) ([]*dto.DictionaryResponse, int64, error) {
	items, total, err := s.uc.ListDictionaries(ctx, page, size, name, typ, details)
	if err != nil {
		return nil, 0, err
	}
	out := make([]*dto.DictionaryResponse, 0, len(items))
	for _, v := range items {
		out = append(out, dictionaryDTO(v))
	}
	return out, total, nil
}
func (s *DictionaryService) Dictionary(ctx context.Context, id uint, typ string, status *bool, details bool) (*dto.DictionaryResponse, error) {
	v, err := s.uc.FindDictionary(ctx, id, typ, status, details)
	if err != nil {
		return nil, err
	}
	return dictionaryDTO(v), nil
}
func (s *DictionaryService) ExportDictionary(ctx context.Context, id uint) (*dto.DictionaryExportResponse, error) {
	v, err := s.uc.ExportDictionary(ctx, id)
	if err != nil {
		return nil, err
	}
	var details []*dto.DictionaryDetailExportResponse
	for _, detail := range v.Details {
		details = append(details, &dto.DictionaryDetailExportResponse{
			Label: detail.Label, Value: detail.Value, Extend: detail.Extend,
			Status: detail.Status, Sort: detail.Sort, Level: detail.Level, Path: detail.Path,
		})
	}
	return &dto.DictionaryExportResponse{
		Name: v.Name, Type: v.Type, Status: v.Status, Desc: v.Desc,
		SysDictionaryDetails: details,
	}, nil
}
func (s *DictionaryService) CreateDictionary(ctx context.Context, v *biz.Dictionary) error {
	return s.uc.CreateDictionary(ctx, v)
}
func (s *DictionaryService) UpdateDictionary(ctx context.Context, v *biz.Dictionary) error {
	return s.uc.UpdateDictionary(ctx, v)
}
func (s *DictionaryService) DeleteDictionary(ctx context.Context, id uint) error {
	return s.uc.DeleteDictionary(ctx, id)
}

func detailDomain(value *dto.DictionaryDetailRequest) *biz.DictionaryDetail {
	return &biz.DictionaryDetail{ID: value.ID, Label: value.Label, Value: value.Value, Extend: value.Extend, Status: value.Status, Sort: value.Sort, DictionaryID: value.DictionaryID, ParentID: value.ParentID, Level: value.Level, Path: value.Path}
}
func (s *DictionaryService) CreateDictionaryDetailRequest(ctx context.Context, req *dto.DictionaryDetailRequest) error {
	return s.CreateDictionaryDetail(ctx, detailDomain(req))
}
func (s *DictionaryService) UpdateDictionaryDetailRequest(ctx context.Context, req *dto.DictionaryDetailRequest) error {
	return s.UpdateDictionaryDetail(ctx, detailDomain(req))
}
func detailDTO(v *biz.DictionaryDetail) *dto.DictionaryDetailResponse {
	var children []*dto.DictionaryDetailResponse
	if v.Children != nil {
		children = make([]*dto.DictionaryDetailResponse, 0, len(v.Children))
		for _, x := range v.Children {
			children = append(children, detailDTO(x))
		}
	}
	disabled := v.Status != nil && !*v.Status
	return &dto.DictionaryDetailResponse{ID: v.ID, CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, DeletedAt: nil, Label: v.Label, Value: v.Value, Extend: v.Extend, Status: v.Status, Sort: v.Sort, DictionaryID: v.DictionaryID, ParentID: v.ParentID, Level: v.Level, Path: v.Path, Disabled: disabled, Children: children}
}
func (s *DictionaryService) DictionaryDetails(ctx context.Context, page, size int, filter biz.DictionaryDetailFilter) ([]*dto.DictionaryDetailResponse, int64, error) {
	items, total, err := s.uc.ListDictionaryDetails(ctx, page, size, filter)
	if err != nil {
		return nil, 0, err
	}
	out := make([]*dto.DictionaryDetailResponse, 0, len(items))
	for _, v := range items {
		out = append(out, detailDTO(v))
	}
	return out, total, nil
}
func (s *DictionaryService) DictionaryDetailsRequest(ctx context.Context, page, size int, request dto.DictionaryDetailListRequest) ([]*dto.DictionaryDetailResponse, int64, error) {
	return s.DictionaryDetails(ctx, page, size, biz.DictionaryDetailFilter{
		DictionaryID: request.DictionaryID,
		Label:        request.Label,
		Value:        request.Value,
		Status:       request.Status,
		ParentID:     request.ParentID,
		Level:        request.Level,
	})
}
func (s *DictionaryService) DictionaryDetail(ctx context.Context, id uint) (*dto.DictionaryDetailResponse, error) {
	v, err := s.uc.FindDictionaryDetail(ctx, id)
	if err != nil {
		return nil, err
	}
	return detailDTO(v), nil
}
func (s *DictionaryService) DictionaryPath(ctx context.Context, id uint) ([]*dto.DictionaryDetailResponse, error) {
	path := make([]*dto.DictionaryDetailResponse, 0)
	for id != 0 {
		value, err := s.uc.FindDictionaryDetail(ctx, id)
		if err != nil {
			return nil, err
		}
		path = append([]*dto.DictionaryDetailResponse{detailDTO(value)}, path...)
		if value.ParentID == nil {
			break
		}
		id = *value.ParentID
	}
	return path, nil
}
func (s *DictionaryService) DictionaryTree(ctx context.Context, id uint, typ string) ([]*dto.DictionaryDetailResponse, error) {
	items, err := s.uc.DictionaryDetailTree(ctx, id, typ)
	if err != nil {
		return nil, err
	}
	out := make([]*dto.DictionaryDetailResponse, 0, len(items))
	for _, v := range items {
		out = append(out, detailDTO(v))
	}
	return out, nil
}
func (s *DictionaryService) CreateDictionaryDetail(ctx context.Context, v *biz.DictionaryDetail) error {
	return s.uc.CreateDictionaryDetail(ctx, v)
}
func (s *DictionaryService) UpdateDictionaryDetail(ctx context.Context, v *biz.DictionaryDetail) error {
	return s.uc.UpdateDictionaryDetail(ctx, v)
}
func (s *DictionaryService) DeleteDictionaryDetail(ctx context.Context, id uint) error {
	return s.uc.DeleteDictionaryDetail(ctx, id)
}
