package com.ffii.lioner.modules.lioner.pdf.web;

import java.util.List;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.ffii.core.exception.NotFoundException;
import com.ffii.core.response.IdRes;
import com.ffii.core.response.RecordsRes;
import com.ffii.core.utils.Params;
import com.ffii.lioner.modules.lioner.pdf.entity.FormSigPage;
import com.ffii.lioner.modules.lioner.pdf.req.UpdateFormSigPageReq;
import com.ffii.lioner.modules.lioner.pdf.service.FormSigPageService;

import jakarta.validation.Valid;

/**
 * CRUD for form_sig_page (signature page config per formCode/startDate/sigType).
 * Access is aligned with {@code /template} (authenticated users; UI restricts via VIEW TEMPLATE).
 */
@RestController
@RequestMapping("/pdf/form-sig-page")
public class FormSigPageController {

    private final FormSigPageService formSigPageService;

    public FormSigPageController(FormSigPageService formSigPageService) {
        this.formSigPageService = formSigPageService;
    }

    @GetMapping
    public RecordsRes<FormSigPage> list(
            @RequestParam(required = false) String formCode,
            @RequestParam(required = false) String sigType) {
        List<FormSigPage> records = formSigPageService.list(formCode, sigType);
        return new RecordsRes<>(records);
    }

    @GetMapping("/{id}")
    public Map<String, Object> get(@PathVariable Long id) {
        return Map.of(
                Params.DATA,
                formSigPageService.find(id).orElseThrow(NotFoundException::new));
    }

    @PostMapping("/save")
    public IdRes save(@RequestBody @Valid UpdateFormSigPageReq req) {
        FormSigPage saved = formSigPageService.save(req);
        return new IdRes(saved.getId());
    }

    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@PathVariable Long id) {
        formSigPageService.markDelete(id);
    }
}
