package com.erp.controller;
import com.erp.dto.ApiResponse;
import com.erp.service.ProcessWorkStandardService;
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/process-work-standard")
@RequiredArgsConstructor
@Slf4j
public class ProcessWorkStandardController {
    private final ProcessWorkStandardService processWorkStandardService;

    @GetMapping("/items")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getProcessWorkStandardItemList(
            @RequestAttribute("company_code") String companyCode,
            @RequestParam Map<String, Object> params) {
        params.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.getProcessWorkStandardItemList(params)));
    }

    @GetMapping("/items/{itemCode}/routings")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getProcessWorkStandardRoutingList(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable String itemCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("item_code", itemCode);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.getProcessWorkStandardRoutingList(params)));
    }

    @PutMapping("/versions/{versionId}/set-default")
    public ResponseEntity<ApiResponse<Map<String, Object>>> setProcessWorkStandardDefaultVersion(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long versionId) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("version_id", versionId);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.setProcessWorkStandardDefaultVersion(params)));
    }

    @PutMapping("/versions/{versionId}/unset-default")
    public ResponseEntity<ApiResponse<Map<String, Object>>> unsetProcessWorkStandardDefaultVersion(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long versionId) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("version_id", versionId);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.unsetProcessWorkStandardDefaultVersion(params)));
    }

    @GetMapping("/routing-detail/{routingDetailId}/work-items")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getProcessWorkStandardWorkItemList(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long routingDetailId) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("routing_detail_id", routingDetailId);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.getProcessWorkStandardWorkItemList(params)));
    }

    @PostMapping("/work-items")
    public ResponseEntity<ApiResponse<Map<String, Object>>> insertProcessWorkStandardWorkItem(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.insertProcessWorkStandardWorkItem(body)));
    }

    @PutMapping("/work-items/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> updateProcessWorkStandardWorkItem(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        body.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.updateProcessWorkStandardWorkItem(body)));
    }

    @DeleteMapping("/work-items/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> deleteProcessWorkStandardWorkItem(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.deleteProcessWorkStandardWorkItem(params)));
    }

    @GetMapping("/work-items/{workItemId}/details")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getProcessWorkStandardWorkItemDetailList(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long workItemId) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("work_item_id", workItemId);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.getProcessWorkStandardWorkItemDetailList(params)));
    }

    @PostMapping("/work-item-details")
    public ResponseEntity<ApiResponse<Map<String, Object>>> insertProcessWorkStandardWorkItemDetail(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.insertProcessWorkStandardWorkItemDetail(body)));
    }

    @PutMapping("/work-item-details/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> updateProcessWorkStandardWorkItemDetail(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        body.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.updateProcessWorkStandardWorkItemDetail(body)));
    }

    @DeleteMapping("/work-item-details/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> deleteProcessWorkStandardWorkItemDetail(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.deleteProcessWorkStandardWorkItemDetail(params)));
    }

    @PutMapping("/save-all")
    public ResponseEntity<ApiResponse<Map<String, Object>>> saveAllProcessWorkStandard(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.saveAllProcessWorkStandard(body)));
    }

    @GetMapping("/registered-items/{screenCode}")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getProcessWorkStandardRegisteredItemList(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable String screenCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("screen_code", screenCode);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.getProcessWorkStandardRegisteredItemList(params)));
    }

    @PostMapping("/registered-items")
    public ResponseEntity<ApiResponse<Map<String, Object>>> insertProcessWorkStandardRegisteredItem(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.insertProcessWorkStandardRegisteredItem(body)));
    }

    @PostMapping("/registered-items/batch")
    public ResponseEntity<ApiResponse<Map<String, Object>>> insertProcessWorkStandardRegisteredItemBatch(
            @RequestAttribute("company_code") String companyCode,
            @RequestBody List<Map<String, Object>> body) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("items", body);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.insertProcessWorkStandardRegisteredItemBatch(params)));
    }

    @DeleteMapping("/registered-items/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> deleteProcessWorkStandardRegisteredItem(
            @RequestAttribute("company_code") String companyCode,
            @PathVariable Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("company_code", companyCode);
        params.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(processWorkStandardService.deleteProcessWorkStandardRegisteredItem(params)));
    }
}
