package com.erp.controller;

import com.erp.dto.ApiResponse;
import com.erp.service.BusinessRuleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Slf4j
public class BusinessRuleController {

    private final BusinessRuleService businessRuleService;

    /**
     * GET /api/dashboards/{dashboardId}/rules — 해당 대시보드의 룰 목록
     */
    @GetMapping("/api/dashboards/{dashboardId}/rules")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getBusinessRuleList(
            @PathVariable String dashboardId,
            @RequestAttribute("company_code") String companyCode,
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int limit) {
        Map<String, Object> params = new HashMap<>();
        params.put("dashboard_id", dashboardId);
        params.put("company_code", companyCode);
        params.put("page", page);
        params.put("limit", limit);
        return ResponseEntity.ok(ApiResponse.success(businessRuleService.getBusinessRuleList(params)));
    }

    /**
     * GET /api/rules/{ruleId} — 룰 상세 (노드 + 연결)
     */
    @GetMapping("/api/rules/{ruleId}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getBusinessRuleInfo(
            @PathVariable String ruleId) {
        Map<String, Object> params = new HashMap<>();
        params.put("rule_id", ruleId);
        Map<String, Object> result = businessRuleService.getBusinessRuleInfo(params);
        if (result == null) {
            return ResponseEntity.ok(ApiResponse.error("Rule not found"));
        }
        return ResponseEntity.ok(ApiResponse.success(result));
    }

    /**
     * POST /api/dashboards/{dashboardId}/rules — 룰 생성
     */
    @PostMapping("/api/dashboards/{dashboardId}/rules")
    public ResponseEntity<ApiResponse<Map<String, Object>>> insertBusinessRule(
            @PathVariable String dashboardId,
            @RequestBody Map<String, Object> body,
            @RequestAttribute("company_code") String companyCode,
            @RequestAttribute("user_id") String userId) {
        body.put("dashboard_id", dashboardId);
        body.put("company_code", companyCode);
        body.put("user_id", userId);
        return ResponseEntity.ok(ApiResponse.success(businessRuleService.insertBusinessRule(body)));
    }

    /**
     * PUT /api/rules/{ruleId} — 룰 수정
     */
    @PutMapping("/api/rules/{ruleId}")
    public ResponseEntity<ApiResponse<Void>> updateBusinessRule(
            @PathVariable String ruleId,
            @RequestBody Map<String, Object> body,
            @RequestAttribute("user_id") String userId) {
        body.put("rule_id", ruleId);
        body.put("user_id", userId);
        businessRuleService.updateBusinessRule(body);
        return ResponseEntity.ok(ApiResponse.success(null));
    }

    /**
     * DELETE /api/rules/{ruleId} — 룰 삭제
     */
    @DeleteMapping("/api/rules/{ruleId}")
    public ResponseEntity<ApiResponse<Void>> deleteBusinessRule(
            @PathVariable String ruleId,
            @RequestAttribute("user_id") String userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("rule_id", ruleId);
        params.put("user_id", userId);
        businessRuleService.deleteBusinessRule(params);
        return ResponseEntity.ok(ApiResponse.success(null));
    }

    /**
     * PUT /api/rules/{ruleId}/toggle — 활성/비활성 토글
     */
    @PutMapping("/api/rules/{ruleId}/toggle")
    public ResponseEntity<ApiResponse<Void>> toggleBusinessRule(
            @PathVariable String ruleId,
            @RequestAttribute("user_id") String userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("rule_id", ruleId);
        params.put("user_id", userId);
        businessRuleService.toggleBusinessRule(params);
        return ResponseEntity.ok(ApiResponse.success(null));
    }
}
