package com.erp.controller;
import com.erp.dto.ApiResponse;
import com.erp.service.WorkHistoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@RequestMapping("/api/work-history")
@RequiredArgsConstructor
@Slf4j
public class WorkHistoryController {
    private final WorkHistoryService workHistoryService;

    @GetMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> getWorkHistoryList(
            @RequestParam Map<String, Object> params) {
        return ResponseEntity.ok(ApiResponse.success(workHistoryService.getWorkHistoryList(params)));
    }

    @GetMapping("/list")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getWorkHistoryListAlias(
            @RequestParam Map<String, Object> params) {
        return ResponseEntity.ok(ApiResponse.success(workHistoryService.getWorkHistoryList(params)));
    }

    @GetMapping("/stats")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getWorkHistoryStats() {
        return ResponseEntity.ok(ApiResponse.success(workHistoryService.getWorkHistoryStats()));
    }

    @GetMapping("/trend")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getWorkHistoryMonthlyTrend(
            @RequestParam Map<String, Object> params) {
        if (!params.containsKey("months")) params.put("months", 6);
        return ResponseEntity.ok(ApiResponse.success(workHistoryService.getWorkHistoryMonthlyTrend(params)));
    }

    @GetMapping("/routes")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getWorkHistoryTopRoutes(
            @RequestParam Map<String, Object> params) {
        if (!params.containsKey("limit")) params.put("limit", 5);
        return ResponseEntity.ok(ApiResponse.success(workHistoryService.getWorkHistoryTopRoutes(params)));
    }

    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getWorkHistoryInfo(
            @PathVariable Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(workHistoryService.getWorkHistoryInfo(params)));
    }

    @PostMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> insertWorkHistory(
            @RequestBody Map<String, Object> body) {
        return ResponseEntity.status(201).body(ApiResponse.success(workHistoryService.insertWorkHistory(body), "작업 이력이 생성되었습니다."));
    }

    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> updateWorkHistory(
            @PathVariable Long id,
            @RequestBody Map<String, Object> body) {
        body.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(workHistoryService.updateWorkHistory(body), "작업 이력이 수정되었습니다."));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> deleteWorkHistory(
            @PathVariable Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        workHistoryService.deleteWorkHistory(params);
        return ResponseEntity.ok(ApiResponse.success(null, "작업 이력이 삭제되었습니다."));
    }
}
