package com.ffii.fpsms.modules.master.web

import com.ffii.fpsms.modules.master.entity.Printer
import com.ffii.fpsms.modules.master.entity.projections.PrinterCombo
import com.ffii.fpsms.modules.master.service.PrinterService
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.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import jakarta.validation.Valid

@RequestMapping("printers")
@RestController
class PrinterController(
    private val printerService: PrinterService
) {
    @GetMapping
    fun getPrinters(): List<Printer> {
        return printerService.getPrinters();
    }
    
    @GetMapping("/{id}")
    fun getPrinter(@PathVariable id: Long): Printer {
        return printerService.findById(id) 
            ?: throw org.springframework.web.server.ResponseStatusException(
                org.springframework.http.HttpStatus.NOT_FOUND, 
                "Printer with id $id not found"
            )
    }
    
    @GetMapping("/combo")
    fun findCombo(): List<PrinterCombo> {
        return printerService.findCombo();
    }
    
    @GetMapping("/descriptions")
    fun getDescriptions(): List<String> {
        return printerService.getDistinctDescriptions();
    }
    
    @PostMapping
    fun createPrinter(@Valid @RequestBody request: PrinterCreateRequest): Printer {
        return printerService.createPrinter(request)
    }

    @PutMapping("/{id}")
    fun updatePrinter(@PathVariable id: Long, @Valid @RequestBody request: PrinterUpdateRequest): Printer {
        return printerService.updatePrinter(id, request);
    }
    
    @DeleteMapping("/{id}")
    fun deletePrinter(@PathVariable id: Long): List<Printer> {
        return printerService.markDeleted(id);
    }
}

data class PrinterCreateRequest(
    val name: String? = null,
    val code: String? = null,
    val type: String? = null,
    val description: String? = null,
    val ip: String? = null,
    val port: Int? = null,
    val dpi: Int? = null,
    val brand: String? = null
)

data class PrinterUpdateRequest(
    val name: String? = null,
    val code: String? = null,
    val type: String? = null,
    val description: String? = null,
    val ip: String? = null,
    val port: Int? = null,
    val dpi: Int? = null,
    val brand: String? = null
)