package com.erp.ai.controller;

import com.erp.ai.dto.GroupMemberRequest;
import com.erp.ai.dto.GroupRequest;
import com.erp.ai.model.AiAgentGroup;
import com.erp.ai.model.AiAgentGroupMember;
import com.erp.ai.service.AiAgentGroupService;
import com.erp.dto.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/ai-agent-groups")
@RequiredArgsConstructor
public class AiAgentGroupController {

    private final AiAgentGroupService groupService;

    @GetMapping
    public ResponseEntity<ApiResponse<List<AiAgentGroup>>> list(
            @RequestAttribute(value = "company_code", required = false) String companyCode) {
        return ResponseEntity.ok(ApiResponse.success(groupService.list(companyCode)));
    }

    @GetMapping("/connectors")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> connectors() {
        return ResponseEntity.ok(ApiResponse.success(groupService.getAvailableConnectors()));
    }

    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getById(@PathVariable long id) {
        Map<String, Object> data = groupService.getById(id);
        if (data == null) {
            return ResponseEntity.status(404).body(ApiResponse.error("그룹을 찾을 수 없습니다."));
        }
        return ResponseEntity.ok(ApiResponse.success(data));
    }

    @PostMapping
    public ResponseEntity<ApiResponse<AiAgentGroup>> create(
            @RequestAttribute(value = "user_id", required = false) String userId,
            @RequestBody GroupRequest req) {
        return ResponseEntity.status(201).body(
                ApiResponse.success(groupService.create(req, userId != null ? userId : "system"),
                        "멀티 에이전트 그룹이 생성되었습니다."));
    }

    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<AiAgentGroup>> update(
            @PathVariable long id,
            @RequestBody GroupRequest req) {
        AiAgentGroup updated = groupService.update(id, req);
        if (updated == null) {
            return ResponseEntity.status(404).body(ApiResponse.error("그룹을 찾을 수 없습니다."));
        }
        return ResponseEntity.ok(ApiResponse.success(updated, "그룹이 수정되었습니다."));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> delete(@PathVariable long id) {
        groupService.delete(id);
        return ResponseEntity.ok(ApiResponse.success(null, "그룹이 삭제되었습니다."));
    }

    // ===== 멤버 관리 =====

    @PostMapping("/{id}/members")
    public ResponseEntity<ApiResponse<AiAgentGroupMember>> addMember(
            @PathVariable long id,
            @RequestBody GroupMemberRequest req) {
        return ResponseEntity.status(201).body(
                ApiResponse.success(groupService.addMember(id, req), "멤버가 추가되었습니다."));
    }

    @PutMapping("/members/{memberId}")
    public ResponseEntity<ApiResponse<AiAgentGroupMember>> updateMember(
            @PathVariable long memberId,
            @RequestBody GroupMemberRequest req) {
        return ResponseEntity.ok(
                ApiResponse.success(groupService.updateMember(memberId, req), "멤버가 수정되었습니다."));
    }

    @DeleteMapping("/members/{memberId}")
    public ResponseEntity<ApiResponse<Void>> removeMember(@PathVariable long memberId) {
        groupService.removeMember(memberId);
        return ResponseEntity.ok(ApiResponse.success(null, "멤버가 제거되었습니다."));
    }
}
