package com.erp.controller;
import com.erp.dto.ApiResponse;
import com.erp.service.YardLayoutService;
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/yard-layout")
@RequiredArgsConstructor
@Slf4j
public class YardLayoutController {
    private final YardLayoutService yardLayoutService;

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

    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getYardLayoutInfo(
            @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(yardLayoutService.getYardLayoutInfo(params)));
    }

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

    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> updateYardLayout(
            @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(yardLayoutService.updateYardLayout(body)));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> deleteYardLayout(
            @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(yardLayoutService.deleteYardLayout(params)));
    }

    @PostMapping("/{id}/duplicate")
    public ResponseEntity<ApiResponse<Map<String, Object>>> duplicateYardLayout(
            @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(yardLayoutService.duplicateYardLayout(params)));
    }

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

    @PostMapping("/{id}/placements")
    public ResponseEntity<ApiResponse<Map<String, Object>>> insertYardPlacement(
            @PathVariable Long id,
            @RequestBody Map<String, Object> body) {
        body.put("yard_layout_id", id);
        return ResponseEntity.ok(ApiResponse.success(yardLayoutService.insertYardPlacement(body)));
    }

    @PutMapping("/{id}/placements/batch")
    public ResponseEntity<ApiResponse<Map<String, Object>>> batchUpdateYardPlacements(
            @PathVariable Long id,
            @RequestBody List<Map<String, Object>> placements) {
        return ResponseEntity.ok(ApiResponse.success(yardLayoutService.batchUpdateYardPlacements(placements)));
    }

    @PutMapping("/placements/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> updateYardPlacement(
            @PathVariable Long id,
            @RequestBody Map<String, Object> body) {
        body.put("id", id);
        return ResponseEntity.ok(ApiResponse.success(yardLayoutService.updateYardPlacement(body)));
    }

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