package com.platform2easy.genesis.web.controller.cerc;

import com.platform2easy.genesis.domain.model.cerc.ContratoValorCpr;
import com.platform2easy.genesis.domain.service.cerc.ContratoValorCprService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("cpr/contract")
@AllArgsConstructor
public class ContratoValorCprController {

    private static final String FORM = "cpr/contract/form";
    private static final String LIST = "cpr/contract/list";
    private static final String REDIRECT_ALL = "redirect:/cpr/contract/all";
    private static final String TITLE_PAGE = "titlePage";


    private final ContratoValorCprService service;

    private String openForm(ContratoValorCpr cprContract, String title, Model model) {
        model.addAttribute(TITLE_PAGE, title);
        model.addAttribute("cprContract", cprContract);
        return FORM;
    }

    @GetMapping()
    public String init(ContratoValorCpr cprContract, Model model) {
        return openForm(cprContract, "Cadastrar Contrato de CPR", model);
    }

    @GetMapping("edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        return openForm(service.getById(id), "Editar Contrato de CPR", model);
    }

    @PostMapping()
    public String save(@ModelAttribute ContratoValorCpr cprContract) {
        service.save(cprContract);
        return REDIRECT_ALL;
    }

    @GetMapping("remove/{id}")
    public String remove(@PathVariable Long id, Model model) {
        service.delete(id);
        return REDIRECT_ALL;
    }

    @GetMapping("all")
    public String listAll(Model model) {
        model.addAttribute(TITLE_PAGE, "Contratos de CPR's");
        model.addAttribute("cprContracts", service.listAll());
        return LIST;
    }


}
