package dev.fyloz.trial.colorrecipesexplorer.rest

import dev.fyloz.trial.colorrecipesexplorer.model.*
import dev.fyloz.trial.colorrecipesexplorer.service.MaterialService
import org.springframework.context.annotation.Profile
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import javax.validation.Valid

private const val MATERIAL_CONTROLLER_PATH = "api/material"

@RestController
@RequestMapping(MATERIAL_CONTROLLER_PATH)
@Profile("rest")
class MaterialController(materialService: MaterialService) :
    AbstractModelRestApiController<Material, MaterialSaveDto, MaterialUpdateDto, MaterialService>(
        materialService,
        MATERIAL_CONTROLLER_PATH
    ) {
    @GetMapping("{id}/simdut/exists")
    @ResponseStatus(HttpStatus.OK)
    fun hasSimdut(@PathVariable id: Long): ResponseEntity<Boolean> =
        ResponseEntity.ok(service.hasSimdut(id))

    @GetMapping("{id}/simdut", produces = [MediaType.APPLICATION_PDF_VALUE])
    @ResponseStatus(HttpStatus.OK)
    fun getSimdut(@PathVariable id: Long): ResponseEntity<ByteArray> {
        val simdutFile = service.getSimdut(id)
        return if (simdutFile.isEmpty()) {
            ResponseEntity.notFound().build()
        } else {
            val headers = HttpHeaders().apply { contentType = MediaType.APPLICATION_PDF }
            ResponseEntity(simdutFile, headers, HttpStatus.OK)
        }
    }

    @GetMapping("mix/create/{recipeId}")
    @ResponseStatus(HttpStatus.OK)
    fun getAllForMixCreation(@PathVariable recipeId: Long): ResponseEntity<Collection<Material>> =
        ResponseEntity.ok(service.getAllForMixCreation(recipeId))

    @GetMapping("mix/update/{mixId}")
    @ResponseStatus(HttpStatus.OK)
    fun getAllForMixUpdate(@PathVariable mixId: Long): ResponseEntity<Collection<Material>> =
        ResponseEntity.ok(service.getAllForMixUpdate(mixId))

    @PostMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
    fun saveTest(@Valid entity: MaterialSaveDto, simdutFile: MultipartFile?): ResponseEntity<Material> =
        super.save(
            materialSaveDto(
                name = entity.name,
                inventoryQuantity = entity.inventoryQuantity,
                materialType = entity.materialType,
                simdutFile = simdutFile
            )
        )

    @PostMapping("oldsave")
    override fun save(entity: MaterialSaveDto): ResponseEntity<Material> =
        ResponseEntity.notFound().build()

    @PutMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
    fun update(@Valid entity: MaterialUpdateDto, simdutFile: MultipartFile?): ResponseEntity<Void> =
        super.update(
            materialUpdateDto(
                id = entity.id,
                name = entity.name,
                inventoryQuantity = entity.inventoryQuantity,
                materialType = entity.materialType,
                simdutFile = simdutFile
            )
        )

    @PutMapping("oldupdate")
    override fun update(entity: MaterialUpdateDto): ResponseEntity<Void> =
        ResponseEntity.notFound().build()
}
