package org.raddatz.familienarchiv.user;

import lombok.RequiredArgsConstructor;
import org.raddatz.familienarchiv.document.transcription.MentionDTO;
import org.raddatz.familienarchiv.user.AppUser;
import org.raddatz.familienarchiv.security.Permission;
import org.raddatz.familienarchiv.security.RequirePermission;
import org.raddatz.familienarchiv.user.UserSearchService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequirePermission({Permission.READ_ALL, Permission.WRITE_ALL, Permission.ANNOTATE_ALL})
public class UserSearchController {

    private final UserSearchService userSearchService;

    @GetMapping("/api/users/search")
    public List<MentionDTO> search(@RequestParam(defaultValue = "") String q) {
        return userSearchService.search(q).stream()
                .map(this::toMentionDTO)
                .toList();
    }

    private MentionDTO toMentionDTO(AppUser user) {
        return new MentionDTO(user.getId(), user.getFirstName(), user.getLastName());
    }
}
