/*
 * This file is part of LibEuFin.
 * Copyright (C) 2023-2025, 2026 Taler Systems S.A.

 * LibEuFin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3, or
 * (at your option) any later version.

 * LibEuFin is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
 * Public License for more details.

 * You should have received a copy of the GNU Affero General Public
 * License along with LibEuFin; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>
 */

/**
 * This file collects all the CLI subcommands and runs
 * them.  The actual implementation of each subcommand is
 * kept in their respective files.
 */
package tech.libeufin.nexus

import io.ktor.server.application.*
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import kotlinx.serialization.Serializable
import kotlinx.serialization.Contextual
import tech.libeufin.common.api.OpenApiInfo
import tech.libeufin.common.api.talerApi
import tech.libeufin.common.setupSecurityProperties
import tech.libeufin.common.VERSION
import tech.libeufin.nexus.api.revenueApi
import tech.libeufin.nexus.api.preparedTransferAPI
import tech.libeufin.nexus.api.wireGatewayApi
import tech.libeufin.nexus.api.observabilityApi
import tech.libeufin.nexus.cli.LibeufinNexus
import tech.libeufin.nexus.db.Database
import com.github.ajalt.clikt.core.main
import tech.libeufin.common.IBAN
import java.time.Instant

/** Triple identifying one IBAN bank account */
data class IbanAccountMetadata(
    val iban: IBAN,
    val bic: String?,
    val name: String
)

fun Application.nexusApi(db: Database, cfg: NexusConfig, serveSpec: Boolean = false) = talerApi(
    LoggerFactory.getLogger("libeufin-nexus-api"),
    OpenApiInfo(
        title = "LibEuFin Nexus API",
        version = VERSION,
        description = "Taler wire gateway, wire transfer gateway, revenue, and observability APIs for LibEuFin Nexus",
        securityConfig = { 
        securityScheme("bearerAuth") {
            type = io.github.smiley4.ktoropenapi.config.AuthType.HTTP
            scheme = io.github.smiley4.ktoropenapi.config.AuthScheme.BEARER
            bearerFormat = "token"
        }
        securityScheme("basicAuth") {
            type = io.github.smiley4.ktoropenapi.config.AuthType.HTTP
            scheme = io.github.smiley4.ktoropenapi.config.AuthScheme.BASIC
            description = "HTTP Basic authentication, supported only on compatibility-enabled endpoints"
        }
    }
    ),
    serveSpec = serveSpec
) {
    wireGatewayApi(db, cfg)
    preparedTransferAPI(db, cfg)
    revenueApi(db, cfg)
    observabilityApi(db, cfg)
}

fun main(args: Array<String>) {
    setupSecurityProperties()
    LibeufinNexus().main(args)
}

@Serializable
data class TaskStatus(
    @Contextual
    val last_successfull: Instant? = null,
    @Contextual
    val last_trial: Instant? = null
)