package org.raddatz.familienarchiv.user;

import java.util.List;
import java.util.UUID;

import org.raddatz.familienarchiv.user.GroupDTO;
import org.raddatz.familienarchiv.user.UserGroup;
import org.raddatz.familienarchiv.security.Permission;
import org.raddatz.familienarchiv.security.RequirePermission;
import org.raddatz.familienarchiv.user.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/api/groups")
@RequirePermission(Permission.ADMIN_PERMISSION)
@RequiredArgsConstructor
public class GroupController {

    private final UserService userService;

    @PostMapping("")
    public ResponseEntity<UserGroup> createGroup(@RequestBody GroupDTO dto) {
        return ResponseEntity.ok(userService.createGroup(dto));
    }

    @PatchMapping("/{id}")
    public ResponseEntity<UserGroup> updateGroup(@PathVariable UUID id, @RequestBody GroupDTO dto) {
        return ResponseEntity.ok(userService.updateGroup(id, dto));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteGroup(@PathVariable UUID id) {
        userService.deleteGroup(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("")
    public ResponseEntity<List<UserGroup>> getAllGroups() {
        return ResponseEntity.ok(userService.getAllGroups());
    }
}
