package services

import (
	dto "astrology-api/dto"
	repositories "astrology-api/repositories"
)

type CallHistoryService interface {

	GetCallHistory(
		userID uint,
		req *dto.GetCallHistoryRequest,
	) (*dto.CallHistoryListResponse, error)
}

type callHistoryService struct {
	repository repositories.CallHistoryRepository
}

func NewCallHistoryService(
	repository repositories.CallHistoryRepository,
) CallHistoryService {

	return &callHistoryService{
		repository: repository,
	}
}

func (s *callHistoryService) GetCallHistory(
	userID uint,
	req *dto.GetCallHistoryRequest,
) (*dto.CallHistoryListResponse, error) {

	// Default Page
	if req.Page <= 0 {
		req.Page = 1
	}

	// Default Limit
	if req.Limit <= 0 {
		req.Limit = 20
	}

	records, totalRecords, err := s.repository.GetCallHistory(
		userID,
		req.Page,
		req.Limit,
		req.Search,
		req.Status,
	)

	if err != nil {
		return nil, err
	}

	// Convert Astrologer
	for i := range records {

		records[i].Astrologer = dto.AstrologerBrief{
			ID:           records[i].AstrologerID,
			Name:         records[i].AstrologerName,
			ProfileImage: records[i].AstrologerProfileImage,
			Experience:   records[i].AstrologerExperience,
		}
	}

	response := &dto.CallHistoryListResponse{
		RecordList:  records,
		TotalRecord: totalRecords,
		CurrentPage: req.Page,
		Limit:       req.Limit,
		Status:      200,
	}

	return response, nil
}