package com.erp.controller;

import com.erp.dto.ApiResponse;
import com.erp.service.MailAccountFileService;
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/mail-account-file")
@RequiredArgsConstructor
@Slf4j
public class MailAccountFileController {
    private final MailAccountFileService mailAccountFileService;

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

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

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

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

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