package service

import (
	"context"

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

type LogViewerService struct{ uc *biz.LogViewerUsecase }

func NewLogViewerService(uc *biz.LogViewerUsecase) *LogViewerService {
	return &LogViewerService{uc: uc}
}

func (s *LogViewerService) LogDates(ctx context.Context, month string) (*dto.LogDatesResponse, error) {
	items, err := s.uc.LogDates(ctx, month)
	if err != nil {
		return nil, err
	}
	out := make([]*dto.LogDateResponse, 0, len(items))
	for _, v := range items {
		out = append(out, &dto.LogDateResponse{Date: v.Date, FileCount: v.FileCount})
	}
	return &dto.LogDatesResponse{Month: month, Dates: out}, nil
}
func (s *LogViewerService) LogFiles(ctx context.Context, date string) (*dto.LogFilesResponse, error) {
	items, err := s.uc.LogFiles(ctx, date)
	if err != nil {
		return nil, err
	}
	out := make([]*dto.LogFileResponse, 0, len(items))
	for _, v := range items {
		out = append(out, &dto.LogFileResponse{Path: v.Path, Name: v.Name, Size: v.Size, ModifiedAt: v.ModifiedAt})
	}
	return &dto.LogFilesResponse{Date: date, Files: out}, nil
}
func (s *LogViewerService) LogContent(ctx context.Context, date, path string, cursor *int64) (*dto.LogContentResponse, error) {
	v, err := s.uc.LogContent(ctx, date, path, cursor)
	if err != nil {
		return nil, err
	}
	return &dto.LogContentResponse{Date: v.Date, Path: v.Path, Content: v.Content, LineCount: v.LineCount, NextCursor: v.NextCursor, HasMore: v.HasMore, LimitedByBytes: v.LimitedByBytes, Size: v.Size, ModifiedAt: v.ModifiedAt}, nil
}
