package com.erp.controller;

import com.erp.dto.ApiResponse;
import com.erp.service.DashboardService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api/dashboards")
@RequiredArgsConstructor
public class DashboardController {

    private final DashboardService dashboardService;

    // ═══ 대시보드 CRUD (MENU_INFO 단일 본체) ═══

    @GetMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> getDashboardList(
            @RequestAttribute("company_code") String companyCode,
            @RequestAttribute("user_id") String userId,
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "50") int limit) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("user_id", userId);
        params.put("keyword", keyword);
        params.put("page", page);
        params.put("limit", limit);
        return ResponseEntity.ok(ApiResponse.success(dashboardService.getDashboardList(params)));
    }

    @GetMapping("/{objid}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getDashboardInfo(
            @PathVariable String objid) {
        Map<String, Object> params = new HashMap<>();
        params.put("objid", objid);
        Map<String, Object> result = dashboardService.getDashboardInfo(params);
        if (result == null) {
            return ResponseEntity.ok(ApiResponse.error("대시보드를 찾을 수 없습니다"));
        }
        return ResponseEntity.ok(ApiResponse.success(result));
    }

    @PostMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> insertDashboard(
            @RequestAttribute("company_code") String companyCode,
            @RequestAttribute("user_id") String userId,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        body.put("user_id", userId);
        return ResponseEntity.ok(ApiResponse.success(dashboardService.insertDashboard(body)));
    }

    @PutMapping("/{objid}")
    public ResponseEntity<ApiResponse<Void>> updateDashboard(
            @PathVariable String objid,
            @RequestAttribute("user_id") String userId,
            @RequestBody Map<String, Object> body) {
        body.put("objid", objid);
        body.put("user_id", userId);
        dashboardService.updateDashboard(body);
        return ResponseEntity.ok(ApiResponse.success(null, "수정 완료"));
    }

    @DeleteMapping("/{objid}")
    public ResponseEntity<ApiResponse<Void>> deleteDashboard(
            @PathVariable String objid,
            @RequestAttribute("user_id") String userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("objid", objid);
        params.put("user_id", userId);
        dashboardService.deleteDashboard(params);
        return ResponseEntity.ok(ApiResponse.success(null, "삭제 완료"));
    }

    // ═══ 카드 CRUD ═══

    @GetMapping("/{objid}/cards")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getDashboardCards(
            @PathVariable String objid) {
        Map<String, Object> params = new HashMap<>();
        params.put("menu_objid", objid);
        return ResponseEntity.ok(ApiResponse.success(dashboardService.getDashboardCardList(params)));
    }

    @PostMapping("/{objid}/cards")
    public ResponseEntity<ApiResponse<Map<String, Object>>> insertDashboardCard(
            @PathVariable String objid,
            @RequestBody Map<String, Object> body) {
        body.put("menu_objid", objid);
        return ResponseEntity.ok(ApiResponse.success(dashboardService.insertDashboardCard(body)));
    }

    @PutMapping("/{objid}/cards/{cardId}")
    public ResponseEntity<ApiResponse<Void>> updateDashboardCard(
            @PathVariable String objid,
            @PathVariable String cardId,
            @RequestBody Map<String, Object> body) {
        body.put("menu_objid", objid);
        body.put("card_id", cardId);
        dashboardService.updateDashboardCard(body);
        return ResponseEntity.ok(ApiResponse.success(null));
    }

    @DeleteMapping("/{objid}/cards/{cardId}")
    public ResponseEntity<ApiResponse<Void>> deleteDashboardCard(
            @PathVariable String objid,
            @PathVariable String cardId) {
        Map<String, Object> params = new HashMap<>();
        params.put("card_id", cardId);
        dashboardService.deleteDashboardCard(params);
        return ResponseEntity.ok(ApiResponse.success(null));
    }

    @PutMapping("/{objid}/cards/batch")
    public ResponseEntity<ApiResponse<Void>> updateCardPositions(
            @PathVariable String objid,
            @RequestBody Map<String, Object> body) {
        dashboardService.updateCardPositions(body);
        return ResponseEntity.ok(ApiResponse.success(null, "일괄 업데이트 완료"));
    }
}
