package cokr.xit.ens.modules.common.ctgy.mblpage.presentation;

import cokr.xit.ens.core.exception.code.EnsErrCd;
import cokr.xit.ens.modules.common.code.PostSeCd;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.entity.Bill;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.code.BillSeCd;
import cokr.xit.ens.modules.common.ctgy.mblpage.domain.SendDetailMblPage;
import cokr.xit.ens.modules.common.ctgy.mblpage.domain.ids.SendDetailMblPageIds;
import cokr.xit.ens.modules.common.ctgy.mblpage.domain.repository.SendDetailMblPageRepository;
import cokr.xit.ens.modules.common.domain.SendMast;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.Optional;

@Tag(name = "SendDetailMblPageTestController")
@Slf4j
@RequiredArgsConstructor
@Controller
public class SendDetailMblPageController extends MblPageControllerSupport {

    private final SendDetailMblPageRepository sendDetailMblPageRepository;


    @Operation(summary = "모바일페이지 출력")
    @GetMapping(value = "/cmft/mbl/page/{postSe}/{sendDetailId}")
    public String mblPagePrnt(@PathVariable String postSe, @PathVariable Long sendDetailId, ModelMap model, @RequestParam(required = false, defaultValue = "") String billUid, @RequestParam(required = false, defaultValue = "") String billSe) {

        Optional<SendDetailMblPage> sendDetailMblPage = sendDetailMblPageRepository.findById(SendDetailMblPageIds.builder()
                .postSe(postSe)
                .sendDetailId(sendDetailId)
                .build());
        model.put("details", sendDetailMblPage.isPresent() ? sendDetailMblPage.get().getDetails() : "{}");
        Bill bill = Bill.builder()
                .billUid(billUid)
                .billSeCd(Arrays.stream(BillSeCd.values())
                        .noneMatch(billSeCd -> billSeCd.getCode().equals(billSe))
                        ? BillSeCd.all
                        : BillSeCd.valueOf(billSe))
                .build();
        SendMast sendMast = SendMast.builder()
                .postSe(Arrays.stream(PostSeCd.values())
                        .noneMatch(postSeCd -> postSeCd.getCode().equals(postSe))
                        ? PostSeCd.kkoMydoc
                        : PostSeCd.valueOf(postSe))
                .build();
        model.put("payBtnInf", getJsonPayBtnInf(bill, sendMast));
        model.put("errCode", EnsErrCd.OK);
        model.put("errMsg", EnsErrCd.OK.getCodeNm());


        return "modules/mblpage/notiprnt";
    }


}
