package service

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

	"kra/internal/service/dto"
)

type MediaService struct {
	uc       *system.MediaUsecase
	settings system.RuntimeSettings
}

func NewMediaService(uc *system.MediaUsecase, settings system.RuntimeSettings) *MediaService {
	return &MediaService{uc: uc, settings: settings}
}
func (s *MediaService) MediaConfig() system.MediaSettings {
	return s.settings.MediaSettings()
}
func mediaDTO(v *system.MediaFile) *dto.MediaResponse {
	return &dto.MediaResponse{ID: v.ID, CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, DeletedAt: nil, Name: v.Name, ClassID: v.CategoryID, URL: v.URL, Tag: v.Tag, Key: v.Key, Size: v.Size, Mime: v.Mime, MD5: v.MD5, UserID: v.UserID}
}
func categoryDTO(v *system.AttachmentCategory) *dto.MediaCategoryResponse {
	var children []*dto.MediaCategoryResponse
	if v.Children != nil {
		children = make([]*dto.MediaCategoryResponse, 0, len(v.Children))
		for _, x := range v.Children {
			children = append(children, categoryDTO(x))
		}
	}
	return &dto.MediaCategoryResponse{ID: v.ID, CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, DeletedAt: nil, Name: v.Name, PID: v.ParentID, Children: children}
}
func (s *MediaService) Upload(ctx context.Context, userID uint, name, mime string, categoryID int, reader io.Reader, save bool) (*dto.MediaResponse, error) {
	v, err := s.uc.Upload(ctx, userID, name, mime, categoryID, reader, save)
	if err != nil {
		return nil, err
	}
	return mediaDTO(v), nil
}
func (s *MediaService) Media(ctx context.Context, id uint) (*dto.MediaResponse, error) {
	v, err := s.uc.FindMedia(ctx, id)
	if err != nil {
		return nil, err
	}
	return mediaDTO(v), nil
}
func (s *MediaService) MediaList(ctx context.Context, filter system.MediaFilter) ([]*dto.MediaResponse, int64, error) {
	items, total, err := s.uc.ListMedia(ctx, filter)
	if err != nil {
		return nil, 0, err
	}
	out := make([]*dto.MediaResponse, 0, len(items))
	for _, v := range items {
		out = append(out, mediaDTO(v))
	}
	return out, total, nil
}
func (s *MediaService) MediaListRequest(ctx context.Context, request *dto.MediaListRequest) ([]*dto.MediaResponse, int64, error) {
	return s.MediaList(ctx, system.MediaFilter{
		Page: request.Page, PageSize: request.PageSize, Keyword: request.Keyword,
		CategoryID: request.ClassID, Tag: request.Tag, UserID: request.UserID,
		StartCreatedAt: request.StartCreatedAt, EndCreatedAt: request.EndCreatedAt,
		OrderKey: request.OrderKey, Desc: request.Desc,
	})
}
func (s *MediaService) Delete(ctx context.Context, id uint) error { return s.uc.Delete(ctx, id) }
func (s *MediaService) Rename(ctx context.Context, id uint, name string) error {
	return s.uc.UpdateMediaName(ctx, id, name)
}
func (s *MediaService) ImportURLs(ctx context.Context, items []*system.MediaFile) error {
	return s.uc.CreateMediaBatch(ctx, items)
}
func (s *MediaService) ImportURLRequests(ctx context.Context, values []dto.ImportMediaRequest) error {
	items := make([]*system.MediaFile, 0, len(values))
	for _, value := range values {
		items = append(items, &system.MediaFile{Name: value.Name, CategoryID: value.ClassID, URL: value.URL, Tag: value.Tag, Key: value.Key, Size: value.Size, Mime: value.Mime, MD5: value.MD5, UserID: value.UserID})
	}
	return s.ImportURLs(ctx, items)
}
func (s *MediaService) Storage(ctx context.Context, prefix, cursor string, limit int) ([]*dto.StorageObjectResponse, string, bool, error) {
	items, next, more, err := s.uc.ListStorage(ctx, prefix, cursor, limit)
	if err != nil {
		return nil, "", false, err
	}
	out := make([]*dto.StorageObjectResponse, 0, len(items))
	for _, v := range items {
		out = append(out, &dto.StorageObjectResponse{Key: v.Path, Size: v.Size, LastModified: v.LastModified, ContentType: v.ContentType})
	}
	return out, next, more, nil
}
func (s *MediaService) Categories(ctx context.Context) ([]*dto.MediaCategoryResponse, error) {
	items, err := s.uc.ListCategories(ctx)
	if err != nil {
		return nil, err
	}
	out := make([]*dto.MediaCategoryResponse, 0, len(items))
	for _, v := range items {
		out = append(out, categoryDTO(v))
	}
	return out, nil
}
func (s *MediaService) SaveCategory(ctx context.Context, v *system.AttachmentCategory) error {
	return s.uc.SaveCategory(ctx, v)
}
func (s *MediaService) SaveCategoryRequest(ctx context.Context, value *dto.CategoryRequest) error {
	return s.SaveCategory(ctx, &system.AttachmentCategory{ID: value.ID, Name: value.Name, ParentID: value.PID})
}
func (s *MediaService) DeleteCategory(ctx context.Context, id uint) error {
	return s.uc.DeleteCategory(ctx, id)
}
