package mapper

import (
	"narasi-ahli-be/app/database/entity"
	ebooksResponse "narasi-ahli-be/app/module/ebooks/response"
)

func ToEbooksResponse(ebook *entity.Ebooks) *ebooksResponse.EbooksResponse {
	if ebook == nil {
		return nil
	}

	var authorName, authorEmail *string
	if ebook.Author != nil {
		authorName = &ebook.Author.Fullname
		authorEmail = &ebook.Author.Email
	}

	return &ebooksResponse.EbooksResponse{
		ID:            ebook.ID,
		Title:         ebook.Title,
		Slug:          ebook.Slug,
		Description:   ebook.Description,
		Price:         ebook.Price,
		PdfFilePath:   ebook.PdfFilePath,
		PdfFileName:   ebook.PdfFileName,
		PdfFileSize:   ebook.PdfFileSize,
		ThumbnailPath: ebook.ThumbnailPath,
		ThumbnailName: ebook.ThumbnailName,
		AuthorId:      ebook.AuthorId,
		AuthorName:    authorName,
		AuthorEmail:   authorEmail,
		Category:      ebook.Category,
		Tags:          ebook.Tags,
		PageCount:     ebook.PageCount,
		Language:      ebook.Language,
		Isbn:          ebook.Isbn,
		Publisher:     ebook.Publisher,
		PublishedYear: ebook.PublishedYear,
		DownloadCount: ebook.DownloadCount,
		PurchaseCount: ebook.PurchaseCount,
		WishlistCount: ebook.WishlistCount,
		Rating:        ebook.Rating,
		ReviewCount:   ebook.ReviewCount,
		StatusId:      ebook.StatusId,
		IsActive:      ebook.IsActive,
		IsPublished:   ebook.IsPublished,
		PublishedAt:   ebook.PublishedAt,
		CreatedById:   ebook.CreatedById,
		CreatedAt:     ebook.CreatedAt,
		UpdatedAt:     ebook.UpdatedAt,
	}
}

func ToEbooksResponseList(ebooks []*entity.Ebooks) []*ebooksResponse.EbooksResponse {
	if ebooks == nil {
		return nil
	}

	var responses []*ebooksResponse.EbooksResponse
	for _, ebook := range ebooks {
		responses = append(responses, ToEbooksResponse(ebook))
	}

	return responses
}

func ToEbookWishlistResponse(wishlist *entity.EbookWishlists) *ebooksResponse.EbookWishlistResponse {
	if wishlist == nil {
		return nil
	}

	return &ebooksResponse.EbookWishlistResponse{
		ID:        wishlist.ID,
		UserId:    wishlist.UserId,
		EbookId:   wishlist.EbookId,
		Ebook:     ToEbooksResponse(wishlist.Ebook),
		CreatedAt: wishlist.CreatedAt,
		UpdatedAt: wishlist.UpdatedAt,
	}
}

func ToEbookWishlistResponseList(wishlists []*entity.EbookWishlists) []*ebooksResponse.EbookWishlistResponse {
	if wishlists == nil {
		return nil
	}

	var responses []*ebooksResponse.EbookWishlistResponse
	for _, wishlist := range wishlists {
		responses = append(responses, ToEbookWishlistResponse(wishlist))
	}

	return responses
}

func ToEbookPurchaseResponse(purchase *entity.EbookPurchases) *ebooksResponse.EbookPurchaseResponse {
	if purchase == nil {
		return nil
	}

	var buyerName, buyerEmail *string
	if purchase.Buyer != nil {
		buyerName = &purchase.Buyer.Fullname
		buyerEmail = &purchase.Buyer.Email
	}

	return &ebooksResponse.EbookPurchaseResponse{
		ID:             purchase.ID,
		BuyerId:        purchase.BuyerId,
		BuyerName:      buyerName,
		BuyerEmail:     buyerEmail,
		EbookId:        purchase.EbookId,
		Ebook:          ToEbooksResponse(purchase.Ebook),
		PurchasePrice:  purchase.PurchasePrice,
		PaymentMethod:  purchase.PaymentMethod,
		PaymentStatus:  purchase.PaymentStatus,
		TransactionId:  purchase.TransactionId,
		PaymentProof:   purchase.PaymentProof,
		PaymentDate:    purchase.PaymentDate,
		DownloadCount:  purchase.DownloadCount,
		LastDownloadAt: purchase.LastDownloadAt,
		StatusId:       purchase.StatusId,
		IsActive:       purchase.IsActive,
		CreatedAt:      purchase.CreatedAt,
		UpdatedAt:      purchase.UpdatedAt,
	}
}

func ToEbookPurchaseResponseList(purchases []*entity.EbookPurchases) []*ebooksResponse.EbookPurchaseResponse {
	if purchases == nil {
		return nil
	}

	var responses []*ebooksResponse.EbookPurchaseResponse
	for _, purchase := range purchases {
		responses = append(responses, ToEbookPurchaseResponse(purchase))
	}

	return responses
}
