package adsrepository

import (
	"fmt"
	adsdomain "legalgo-BE-go/internal/domain/ads"
)

func (a *accessor) GetAll() ([]adsdomain.AdsResponse, error) {
	var ads []adsdomain.AdsResponse

	if err := a.db.Table("ads").
		Select("ads.*, COUNT(content_logs.content_id) as clicked").
		Joins("LEFT JOIN content_logs ON content_logs.content_id = ads.id").
		Group("ads.id").
		Scan(&ads).Error; err != nil {
		return ads, fmt.Errorf("failed to get all ads: %v", err)
	}

	return ads, nil
}
