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

import java.util.Map;

import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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 com.ffii.core.response.IdRes;
import com.ffii.core.response.RecordsRes;
import com.ffii.fhsmsc.modules.User_group.req.DeleteMemberWithAuthoritiesReq;
import com.ffii.fhsmsc.modules.User_group.req.UserCombineReq;
import com.ffii.fhsmsc.modules.User_group.service.UserCombineService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;

@RestController
@RequestMapping("/userCombine")
public class UserCombineController {
    private final UserCombineService userCombineService;

    public UserCombineController(UserCombineService userCombineService) {
        this.userCombineService = userCombineService;
    }

    @Operation(summary = "Get user groups", security = @SecurityRequirement(name = "bearerAuth"))
    @GetMapping("/list")
    public RecordsRes<Map<String, Object>> getUserGroups(HttpServletRequest request) throws ServletRequestBindingException {
        return new RecordsRes<>(userCombineService.getUserGroups(
            Long.parseLong(request.getParameter("userId"))));
    }

    @Operation(summary = "Get group members", security = @SecurityRequirement(name = "bearerAuth"))
    @GetMapping("/members/{orgId}")
    public RecordsRes<Map<String, Object>> getGroupMembers(@PathVariable Long orgId) {
        return new RecordsRes<>(userCombineService.getGroupMembers(orgId));
    }

    @Operation(summary = "Get group detail", security = @SecurityRequirement(name = "bearerAuth"))
    @GetMapping("/detail/{orgId}")
    public Map<String, Object> getGroupDetail(@PathVariable Long orgId) {
        return userCombineService.getGroupDetail(orgId);
    }

    @Operation(summary = "Save user group", security = @SecurityRequirement(name = "bearerAuth"))
    @PostMapping("/save")
    public IdRes saveUserGroup(@RequestBody @Valid UserCombineReq req) {
        Map<String, Object> result = userCombineService.saveUserGroup(req);
        Number id = (Number) result.get("User_Grp_Org_ID");
        return new IdRes(id != null ? id.longValue() : null);
    }

    @Operation(summary = "Delete user group", security = @SecurityRequirement(name = "bearerAuth"))
    @DeleteMapping("/{orgId}")
    public void deleteUserGroup(@PathVariable Long orgId, HttpServletRequest request) {
        Long adminUserId = Long.parseLong(request.getParameter("adminUserId"));
        userCombineService.deleteUserGroup(orgId, adminUserId);
    }

    @Operation(summary = "Get user's organizations", security = @SecurityRequirement(name = "bearerAuth"))
    @GetMapping("/user-groups/{userId}")
    public RecordsRes<Map<String, Object>> getUserOrganizations(@PathVariable Long userId) {
        return new RecordsRes<>(userCombineService.getUserGroups(userId));
    }

    @Operation(summary = "Remove group member", security = @SecurityRequirement(name = "bearerAuth"))
    @DeleteMapping("/members/{orgId}/{userId}")
    public void removeGroupMember(@PathVariable Long orgId, @PathVariable Long userId) {
        userCombineService.removeGroupMember(orgId, userId);
    }

    @Operation(summary = "Delete member and related records", security = @SecurityRequirement(name = "bearerAuth"))
    @DeleteMapping("/delete-member")
    public void deleteMemberAndRelatedRecords(@RequestBody @Valid DeleteMemberWithAuthoritiesReq req) {
        userCombineService.deleteMemberAndRelatedRecords(req);
    }
}
