package com.ffii.fpsms.modules.common.mail.web

import com.ffii.fpsms.modules.common.mail.entity.MailTemplate
import com.ffii.fpsms.modules.common.mail.web.models.MailTemplateRequest
import com.ffii.fpsms.modules.common.mail.service.MailTemplateService
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
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.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RequestMapping("/mailTemplates")
@RestController
class MailTemplateController(
    private val mailTemplateService: MailTemplateService,
) {
    @GetMapping
    fun allMailTemplates(): List<MailTemplate> {
        return mailTemplateService.allMailTemplates();
    }

    @PostMapping("/save")
    fun saveMailTemplate(request: MailTemplateRequest): MailTemplate {
        return mailTemplateService.saveMailTemplate(request);
    }

    @GetMapping("/getMailTemplatePdfForStockInLine/{stockInLineId}")
    fun getMailTemplatePdfForStockInLine(@PathVariable stockInLineId: Long): ResponseEntity<ByteArray> {
        val response = mailTemplateService.getMailTemplatePdfForStockInLine(stockInLineId)
        val headers = HttpHeaders().apply {
            contentType = MediaType.APPLICATION_PDF
//            add("Content-Disposition", "attachment; filename=${response.fileName}")
            add("filename", "${response.fileName}.pdf")
        }

        return ResponseEntity.ok()
            .headers(headers)
            .body(response.file)
    }
}