package userrepository

import (
	"errors"
	authdomain "legalgo-BE-go/internal/domain/auth"

	"gorm.io/gorm"
)

func (ur *UserRepository) GetUserProfile(email string) (*authdomain.UserProfile, error) {
	var users []authdomain.UserProfile

	if email == "" {
		return nil, errors.New("email is empty")
	}

	if err := ur.DB.Table("users u").
		Where("email = ?", email).
		Select("u.email, u.id, s.status as subscribe_status, sp.code as subscribe_plan_code, sp.name as subscribe_plan_name").
		Joins("join subscribes s on s.id = u.subscribe_id").
		Joins("join subscribe_plans sp on s.subscribe_plan_id = sp.id").
		Scan(&users).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, errors.New("user not found")
		}
		return nil, err
	}

	return &users[0], nil
}
