package com.ffii.fhsmsc.modules.User_group.web;

import com.ffii.fhsmsc.modules.User_group.service.UserGroupCombineService;
import com.ffii.fhsmsc.modules.User_group.req.UserGroupMemberBatchReq;
import com.ffii.fhsmsc.modules.User_group.req.MemberUpdateReq;
import com.ffii.fhsmsc.modules.User_group.enity.UserGroupMembers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.ffii.core.response.RecordsRes;
import com.ffii.fhsmsc.modules.User_group.req.*;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/usergroup")
public class UserGroupCombineController {

    @Autowired
    private UserGroupCombineService userGroupCombineService;
    
    @GetMapping("/members/{orgId}")
    public ResponseEntity<List<UserGroupMembers>> getGroupMembers(
            @PathVariable Long orgId,
            @RequestParam(required = false) String relationshipType) {
        List<UserGroupMembers> members = userGroupCombineService.getGroupMembersWithAuthorities(orgId, relationshipType);
        return ResponseEntity.ok(members);
    }
    
    @PostMapping("/members/batch")
    public ResponseEntity<Void> addMembers(@RequestBody UserGroupMemberBatchReq req) {
        userGroupCombineService.addMemberWithAuthorities(req);
        return ResponseEntity.ok().build();
    }
    
    @PutMapping("/members/{userId}")
    public ResponseEntity<Void> updateMember(
            @PathVariable Long userId,
            @RequestBody MemberUpdateReq req) {
        userGroupCombineService.updateMember(userId, req);
        return ResponseEntity.ok().build();
    }
    
    @DeleteMapping("/members/{orgId}/{userId}")
    public ResponseEntity<Void> removeMember(
            @PathVariable Long orgId,
            @PathVariable Long userId,
            @RequestParam String relationshipType) {
        userGroupCombineService.removeMember(orgId, userId, relationshipType);
        return ResponseEntity.ok().build();
    }
    
    @GetMapping("/members/{userId}/authorities")
    public RecordsRes<Map<String, Object>> getMemberAuthorities(@PathVariable int userId) {
        return new RecordsRes<>(userGroupCombineService.getMemberAuthorities(userId));
    }
    
    @GetMapping("/authorities")
    public RecordsRes<Map<String, Object>> getAllAuthorities() {
        return new RecordsRes<>(userGroupCombineService.getAllAuthorities());
    }
} 