package com.erp.controller;

import com.erp.dto.ApiResponse;
import com.erp.service.BarcodeLabelService;
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/barcode-label")
@RequiredArgsConstructor
@Slf4j
public class BarcodeLabelController {

    private final BarcodeLabelService barcodeLabelService;

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

    @GetMapping("/templates")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getTemplateList() {
        return ResponseEntity.ok(ApiResponse.success(barcodeLabelService.getBarcodeLabelTemplateList()));
    }

    @GetMapping("/templates/{templateId}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getTemplateInfo(
            @PathVariable String templateId) {
        Map<String, Object> result = barcodeLabelService.getBarcodeLabelTemplateInfo(templateId);
        if (result == null) return ResponseEntity.status(404).body(ApiResponse.error("템플릿을 찾을 수 없습니다."));
        return ResponseEntity.ok(ApiResponse.success(result));
    }

    @GetMapping("/{labelId}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getBarcodeLabelInfo(
            @PathVariable String labelId) {
        Map<String, Object> result = barcodeLabelService.getBarcodeLabelInfo(labelId);
        if (result == null) return ResponseEntity.status(404).body(ApiResponse.error("라벨을 찾을 수 없습니다."));
        return ResponseEntity.ok(ApiResponse.success(result));
    }

    @GetMapping("/{labelId}/layout")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getBarcodeLabelLayout(
            @PathVariable String labelId) {
        Map<String, Object> result = barcodeLabelService.getBarcodeLabelLayout(labelId);
        if (result == null) return ResponseEntity.status(404).body(ApiResponse.error("레이아웃을 찾을 수 없습니다."));
        return ResponseEntity.ok(ApiResponse.success(result));
    }

    @PostMapping
    public ResponseEntity<ApiResponse<Map<String, Object>>> insertBarcodeLabel(
            @RequestAttribute("company_code") String companyCode,
            @RequestAttribute(value = "user_id", required = false) String userId,
            @RequestBody Map<String, Object> body) {
        body.put("company_code", companyCode);
        body.put("created_by", userId);
        Map<String, Object> result = barcodeLabelService.insertBarcodeLabel(body);
        return ResponseEntity.status(201).body(ApiResponse.success(result, "라벨이 생성되었습니다."));
    }

    @PostMapping("/{labelId}/copy")
    public ResponseEntity<ApiResponse<Map<String, Object>>> copyBarcodeLabel(
            @PathVariable String labelId,
            @RequestAttribute(value = "user_id", required = false) String userId) {
        String newLabelId = barcodeLabelService.copyBarcodeLabel(labelId, userId);
        if (newLabelId == null) return ResponseEntity.status(404).body(ApiResponse.error("복사할 라벨을 찾을 수 없습니다."));
        Map<String, Object> result = new HashMap<>();
        result.put("label_id", newLabelId);
        return ResponseEntity.status(201).body(ApiResponse.success(result, "라벨이 복사되었습니다."));
    }

    @PutMapping("/{labelId}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> updateBarcodeLabel(
            @PathVariable String labelId,
            @RequestBody Map<String, Object> body) {
        boolean updated = barcodeLabelService.updateBarcodeLabel(labelId, body);
        if (!updated) return ResponseEntity.status(404).body(ApiResponse.error("라벨을 찾을 수 없습니다."));
        return ResponseEntity.ok(ApiResponse.success(null, "라벨이 수정되었습니다."));
    }

    @PutMapping("/{labelId}/layout")
    public ResponseEntity<ApiResponse<Map<String, Object>>> saveBarcodeLabelLayout(
            @PathVariable String labelId,
            @RequestAttribute(value = "user_id", required = false) String userId,
            @RequestBody Map<String, Object> body) {
        barcodeLabelService.saveBarcodeLabelLayout(labelId, body, userId);
        return ResponseEntity.ok(ApiResponse.success(null, "레이아웃이 저장되었습니다."));
    }

    @DeleteMapping("/{labelId}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> deleteBarcodeLabel(
            @PathVariable String labelId) {
        boolean deleted = barcodeLabelService.deleteBarcodeLabel(labelId);
        if (!deleted) return ResponseEntity.status(404).body(ApiResponse.error("라벨을 찾을 수 없습니다."));
        return ResponseEntity.ok(ApiResponse.success(null, "라벨이 삭제되었습니다."));
    }
}
