package service

import (
	"context"
	"net/http"

	"cmr-backend/internal/apperr"
	"cmr-backend/internal/store/postgres"
)

type MeService struct {
	store *postgres.Store
}

type MeResult struct {
	ID        string  `json:"id"`
	PublicID  string  `json:"publicId"`
	Status    string  `json:"status"`
	Nickname  *string `json:"nickname,omitempty"`
	AvatarURL *string `json:"avatarUrl,omitempty"`
}

func NewMeService(store *postgres.Store) *MeService {
	return &MeService{store: store}
}

func (s *MeService) GetMe(ctx context.Context, userID string) (*MeResult, error) {
	user, err := s.store.GetUserByID(ctx, s.store.Pool(), userID)
	if err != nil {
		return nil, err
	}
	if user == nil {
		return nil, apperr.New(http.StatusNotFound, "user_not_found", "user not found")
	}

	return &MeResult{
		ID:        user.ID,
		PublicID:  user.PublicID,
		Status:    user.Status,
		Nickname:  user.Nickname,
		AvatarURL: user.AvatarURL,
	}, nil
}
