# R/zzz.R  ------------------------------------------------------------------
# Package initialisation: register the CBF reader and writer with ROI.
# ---------------------------------------------------------------------------

#' @import ROI
#' @importFrom slam simple_triplet_matrix as.simple_triplet_matrix
#'   simple_triplet_zero_matrix
NULL


.onLoad <- function(libname, pkgname) {

    # ── Register CBF reader ──────────────────────────────────────────────────
    #
    # ROI_plugin_register_reader(type, solver, method) tells ROI that when a
    # user calls  ROI_read("problem.cbf", type = "cbf")  it should dispatch
    # to  read_cbf(file, ...)  defined in R/read.R.
    ROI::ROI_plugin_register_reader(
        type   = "cbf",
        solver = pkgname,
        method = read_cbf
    )

    # ── Build writer signature data.frame ────────────────────────────────────
    #
    # ROI_plugin_register_writer requires a "signature" data.frame that
    # declares which (objective × constraints × variable-types) combinations
    # this writer can handle.  ROI uses the signature to pick the right writer
    # for a given OP when multiple format writers are available.
    #
    # Column semantics:
    #   objective   – type of objective function
    #                   "L"  : linear (affine) objective
    #   constraints – structure of constraints
    #                   "X"  : no constraints (pure objective)
    #                   "L"  : linear (polyhedral) constraints only
    #                   "C"  : conic constraints only
    #                   "LC" : mixed linear + conic
    #   types       – variable type mixture
    #                   "C"  : all variables continuous
    #                   "CI" : mixed continuous + integer (MIP)
    #
    # Justification for this particular set of rows
    # -----------------------------------------------
    # CBF version 4 uses a unified cone representation for ALL constraint
    # types: linear equalities/inequalities are simply L=. / L+. / L-. cone
    # chunks, and non-linear conic constraints (SOC, PSD, EXP, POW) are
    # separate chunks.  The INT keyword handles mixed-integer variables.
    # Therefore CBF can faithfully represent each of the six combinations
    # below without any loss of information.
    #
    #   Row 1: Pure objective, no constraints, continuous variables.
    #   Row 2: Linear constraints only, continuous.      (LP)
    #   Row 3: Conic constraints only, continuous.       (SOCP / SDP / ExpCP)
    #   Row 4: Mixed linear+conic constraints, cont.     (Mixed LP+conic)
    #   Row 5: Conic constraints, mixed-integer.         (MISOCP / MISDP)
    #   Row 6: Mixed linear+conic constraints, MI.       (General MI-conic)
    sig <- data.frame(
        objective   = rep("L", 6L),
        constraints = c("X",  "L",  "C",  "LC",  "C",  "LC"),
        types       = c("C",  "C",  "C",  "C",   "CI", "CI"),
        stringsAsFactors = FALSE
    )

    # ── Register CBF writer ──────────────────────────────────────────────────
    ROI::ROI_plugin_register_writer(
        type      = "cbf",
        solver    = pkgname,
        signature = sig,
        method    = write_cbf
    )

    invisible(NULL)
}
