FrenchTaxController.kt

// EV Supervision Lite — built by Kevin Vavelin, Codetics
// https://codetics.fr
// Copyright (c) 2026 Kevin Vavelin, Codetics
// SPDX-License-Identifier: LicenseRef-Codetics-Attribution-1.0

package fr.codetics.ev_server.controllers.fiscal

import fr.codetics.ev_server.core.AppPrincipal
import fr.codetics.ev_server.services.fiscal.FrenchTaxRules
import fr.codetics.ev_server.services.fiscal.FrenchTaxService
import fr.codetics.ev_server.services.fleet.Period
import java.math.BigDecimal
import java.time.LocalDate
import org.springframework.format.annotation.DateTimeFormat
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

/** French tax and payroll declarations linked to charging, for the whole workspace. */
@RestController
@RequestMapping("/api/v1/fiscal/fr")
@PreAuthorize("hasAnyRole('ADMIN','OPERATOR','VIEWER')")
class FrenchTaxController(private val taxes: FrenchTaxService) {
    @GetMapping
    fun report(
        @AuthenticationPrincipal user: AppPrincipal,
        @RequestParam(required = false) year: Int?,
        @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) from: LocalDate?,
        @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) to: LocalDate?,
        @RequestParam(required = false) referencePriceKwh: BigDecimal?,
    ) = taxes.report(user, period(year, from, to), referencePriceKwh)

    @GetMapping("/rules/{year}")
    fun rules(@PathVariable year: Int) = FrenchTaxRules.of(year.coerceIn(2025, 2100))

    @GetMapping("/export/{kind}")
    fun export(
        @AuthenticationPrincipal user: AppPrincipal,
        @PathVariable kind: String,
        @RequestParam(required = false) year: Int?,
        @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) from: LocalDate?,
        @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) to: LocalDate?,
        @RequestParam(required = false) referencePriceKwh: BigDecimal?,
    ) =
        when (kind) {
            "payroll" -> taxes.payrollExport(user, period(year, from, to), referencePriceKwh)
            "vat" -> taxes.vatExport(user, period(year, from, to))
            "renewable-energy" -> taxes.renewableEnergyExport(user, period(year, from, to))
            else -> throw fr.codetics.ev_server.core.NotFound(fr.codetics.ev_server.core.msg("fiscal.unknown_export"))
        }

    private fun period(year: Int?, from: LocalDate?, to: LocalDate?): Period =
        when {
            from != null || to != null -> {
                val default = Period.yearToDate()
                Period(from ?: default.from, to ?: default.to)
            }
            year != null -> Period.year(year.coerceIn(2000, 2100))
            else -> Period.yearToDate()
        }
}