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

import com.platform2easy.genesis.domain.model.cerc.ContratoValorCpr;
import com.platform2easy.genesis.domain.model.cerc.EmissaoCpr;
import com.platform2easy.genesis.domain.service.cerc.ContratoValorCprService;
import com.platform2easy.genesis.domain.service.cerc.EmissaoCprService;
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/emission")
@AllArgsConstructor
public class EmissaoCprController {

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


    private final EmissaoCprService service;

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

    @GetMapping()
    public String init(EmissaoCpr cprEmission, Model model) {
        return openForm(cprEmission, "Cadastrar Emissão de CPR", model);
    }

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

    @PostMapping()
    public String save(@ModelAttribute EmissaoCpr cprEmission) {
        service.save(cprEmission);
        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, "Emissões de CPR's");
        model.addAttribute("cprEmissions", service.listAll());
        return LIST;
    }


}
