package org.octopus.internal.web.controllers

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.Valid
import lombok.AllArgsConstructor
import org.octopus.internal.common.models.Entry
import org.octopus.internal.services.EntryService
import org.octopus.internal.web.dtos.EntryDto
import org.octopus.internal.web.utils.responses.WebResponse
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/entries")
@AllArgsConstructor
@Tag(name = "Entries Management", description = "Operations related to entries.")
class EntryController(
    val entryService: EntryService
) {
    @Operation(
        summary = "Create a new planning entry",
        description = "Adds a new income, outcome, or investment entry to the system.",
        responses = [
            ApiResponse(responseCode = "200", description = "Entry created successfully. Returned with ID")
        ]
    )
    @PostMapping
    fun createEntry(
        @Valid @RequestBody entryDto: EntryDto
    ): WebResponse<Entry> {
        return WebResponse.ok(entryService.createEntry(entryDto))
    }

    @Operation(
        summary = "Get all entries in the DB",
        description = "Returns a list containing all entries.",
        responses = [
            ApiResponse(responseCode = "200", description = "A valid list.")
        ]
    )
    @GetMapping
    fun getAllEntries(): WebResponse<List<Entry>> {
        return WebResponse.ok(entryService.getAllEntries())
    }

    @Operation(
        summary = "Delete all entries in the DB",
        description = "Returns the result of the operation.",
        responses = [
            ApiResponse(responseCode = "200", description = "Whether the operation succeeded.")
        ]
    )
    @DeleteMapping
    fun deleteAllEntries(): WebResponse<Boolean> {
        return WebResponse.ok(entryService.deleteAll())
    }

    @Operation(
        summary = "Delete a specific entry in the DB",
        description = "Returns the result of the operation.",
        responses = [
            ApiResponse(responseCode = "200", description = "Whether the operation succeeded.")
        ]
    )
    @DeleteMapping("/{id}")
    fun deleteEntryById(@PathVariable("id") id: Long): WebResponse<Boolean> {
        return WebResponse.ok(entryService.deleteById(id))
    }

    @Operation(
        summary = "Updates a specific entry in the DB",
        description = "Returns the updated entry.",
        responses = [
            ApiResponse(responseCode = "200", description = "The updated entry.")
        ]
    )
    @PutMapping("/{id}")
    fun editEntryById(@PathVariable("id") id: Long,
                            @Valid @RequestBody entryDto: EntryDto): WebResponse<Entry> {
        return WebResponse.ok(entryService.editEntry(id, entryDto))
    }

//    @GetMapping("/{id}")
//    fun getClient(@PathVariable("id") id: String, @RequestParam(
//            "includeDeactivated",
//            required = false
//        ) includeDeactivated: Boolean = false): WebResponse<Client> {
//        return WebResponse.ok(clientService.getClientById(id.toLong()))
//    }

}