package user

import (
	"Yimaru-Backend/internal/domain"
	"Yimaru-Backend/internal/ports"
	"context"
	"strconv"
)

func (s *Service) GetUserByEmailPhone(
	ctx context.Context,
	email string,
	phone string,
) (domain.User, error) {
	return s.userStore.GetUserByEmailPhone(ctx, email, phone)
}

func (s *Service) IsUserPending(ctx context.Context, userID int64) (bool, error) {
	return s.userStore.IsUserPending(ctx, userID)
}

func (s *Service) GetProfileCompletionStatus(ctx context.Context, userId int64) (ports.ProfileCompletionStatus, error) {
	return s.userStore.GetProfileCompletionStatus(ctx, userId)
}

func (s *Service) SearchUserByNameOrPhone(ctx context.Context, searchString string, role *int64) ([]domain.User, error) {
	// Search user
	var roleStr *string
	if role != nil {
		tmp := strconv.FormatInt(*role, 10)
		roleStr = &tmp
	}
	return s.userStore.SearchUserByNameOrPhone(ctx, searchString, roleStr)

}

func (s *Service) UpdateUser(ctx context.Context, req domain.UpdateUserReq) error {
	// Update user in the store
	return s.userStore.UpdateUser(ctx, req)
}

func (s *Service) GetUserByID(ctx context.Context, id int64) (domain.User, error) {
	return s.userStore.GetUserByID(ctx, id)
}

// GetAllUsers retrieves users based on the provided filter
// func (s *Service) GetAllUsers(ctx context.Context, filter domain.UserFilter) ([]domain.User, int64, error) {
// 	var role *string
// 	if filter.Role != "" {
// 		role = &filter.Role
// 	}

// 	var query *string
// 	if filter.Query.Valid {
// 		q := filter.Query
// 		query = &q
// 	}

// 	var createdBefore *time.Time
// 	if filter.CreatedBefore.Valid {
// 		b := filter.CreatedBefore
// 		createdBefore = &b
// 	}

// 	var createdAfter *time.Time
// 	if filter.CreatedAfter.Valid {
// 		a := filter.CreatedAfter
// 		createdAfter = &a
// 	}

// 	var limit int32 = 10
// 	var offset int32 = 0
// 	if filter.PageSize.Valid {
// 		limit = int32(filter.PageSize)
// 	}
// 	if filter.Page.Valid && filter.PageSize.Valid {
// 		offset = int32(filter.Page * filter.PageSize)
// 	}

// 	return s.userStore.GetAllUsers(ctx, role, query, createdBefore, createdAfter, limit, offset)

// Device management methods
func (s *Service) RegisterDevice(ctx context.Context, userID int64, deviceToken, platform string) error {
	return s.userStore.RegisterDevice(ctx, userID, deviceToken, platform)
}

func (s *Service) GetUserDeviceTokens(ctx context.Context, userID int64) ([]string, error) {
	return s.userStore.GetUserDeviceTokens(ctx, userID)
}

func (s *Service) DeactivateDevice(ctx context.Context, userID int64, deviceToken string) error {
	return s.userStore.DeactivateDevice(ctx, userID, deviceToken)
}

func (s *Service) DeactivateAllUserDevices(ctx context.Context, userID int64) error {
	return s.userStore.DeactivateAllUserDevices(ctx, userID)
}
