package com.owrawww.controller;

import com.owrawww.domain.Inquiry;
import com.owrawww.service.InquiryService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/bbs/partnership")
@RequiredArgsConstructor
public class PartnershipController {

    private final InquiryService inquiryService;

    @GetMapping
    public String partnershipForm(@ModelAttribute Inquiry inquiry) {
        return "sub04_02";
    }

    @PostMapping
    @ResponseBody
    public ResponseEntity<Map<String, Object>> partnershipSubmit(@Valid @ModelAttribute Inquiry inquiry,
                                                                 BindingResult bindingResult) {
        Map<String, Object> result = new HashMap<>();
        if (bindingResult.hasErrors()) {
            result.put("success", false);
            result.put("message", "입력값을 확인해주세요.");
            return ResponseEntity.badRequest().body(result);
        }
        boolean success = inquiryService.submit(inquiry);
        result.put("success", success);
        if (!success) {
            result.put("message", "처리 중 오류가 발생했습니다. 다시 시도해주세요.");
        }
        return ResponseEntity.ok(result);
    }
}
