#' Build JAGS Model String (Continuous M & Continuous Y)
#'
#' Constructs the JAGS model specification for a Bayesian mediation model
#' where both mediators and outcome are continuous, incorporating spike-and-slab
#' variable selection.
#'
#' @description
#' This internal function dynamically generates a JAGS model string based on
#' the number of predictors (\eqn{P}), mediators (\eqn{K}), and the prior
#' distributions defined in the \code{parms} object. It handles the
#' multivariate \eqn{a}-paths and \eqn{b}-paths using \code{inprod} (inner
#' product) for efficiency in JAGS.
#'
#' @param P Integer. The number of predictors/covariates.
#' @param K Integer. The number of mediators.
#' @param parms A \code{data.frame} generated by \code{\link{make_parms_main}}
#' containing columns: \code{priors}, \code{distribution}, \code{arguments},
#' and \code{template}.
#'
#' @return A character string containing the full JAGS model specification.
#'
#' @details
#' The model structure follows a standard mediation framework:
#' \itemize{
#'   \item \strong{Mediator Models:} Each mediator \eqn{M_k} is modeled as a
#'   linear combination of predictors \eqn{X}.
#'   \item \strong{Outcome Model:} \eqn{Y} is modeled as a linear combination
#'   of predictors \eqn{X} (direct effects) and mediators \eqn{M} (indirect effects).
#'   \item \strong{Variable Selection:} Uses Bernoulli-distributed indicators
#'   (\code{a.pip} and \code{b.pip}) to implement spike-and-slab priors on
#'   the mediation pathways.
#' }
#'
#' Joint inclusion for a specific mediator pathway is calculated internally
#' as \code{ind.joint <- a.pip * b.pip}.
#'
#' @seealso \code{\link{prepare_ebmed_data}}, \code{\link{define_init_values}},
#' \code{\link{buzzEBMcontMcontY}}
#'
#' @keywords internal
#'

build_ebmed_model_mcont_ycont <- function(P, K, parms) {

  #Set up prior strings
  prior_strings <- mapply(function(p, d, a, t) {
    sprintf(t, p, d, a)
  }, parms$priors, parms$distribution, parms$arguments, parms$template)
  names(prior_strings) <- parms$priors

  # Create a and b effect blocks
  a_effect_string <- ""
  b_effect_string <- ""

  for (k in 1:K) {
    a_effect_string <- paste0(
      a_effect_string,
      "m", k, "[i] ~ dnorm(mu.m", k, "[i], m.prec[", k, "])\n",
      "mu.m", k, "[i] <- inprod(X[i, ], a[", k, ",])\n\n"
    )
    b_effect_string <- paste0(
      b_effect_string,
      "+ inprod(m",k,"[i],b[",k,"])"
    )
  }

  # Create actual model string

  modelstring <- paste0("
model {

  ## a effects (X -> mediators)
  for (i in 1:N) {",
                        a_effect_string,"}

  for (j in 1:", K, ") {
    a.pip[j] ~ dbern(a.pip.hyperprior)
    for (p in 1:", P, ") {
      a[j, p] <- a.pip[j] * a.coef[j, p]
      a.coef[j, p] ~ dnorm(0, a.coef.hyperprec)
    }
    ", prior_strings["m.prec"],"
  }

  ## b effects (mediators -> y)
  for (i in 1:N) {
    y[i] ~ dnorm(mu.y[i], y.prec)
    mu.y[i] <- inprod(X[i, ], direct.coef[])",
                        b_effect_string, "
  }

  for (j in 1:", K, ") {
    b.pip[j] ~ dbern(b.pip.hyperprior)
    b[j] <- b.pip[j] * b.coef[j]
    b.coef[j] ~ dnorm(0, b.coef.hyperprec)
  }

  ## Direct effects for predictors
  for (p in 1:", P, ") {
  ",prior_strings["direct.coef"],"
  }

  ## Hyperparameters
  ", prior_strings["y.prec"], "
  ", prior_strings["a.pip.hyperprior"], "
  ", prior_strings["b.pip.hyperprior"], "
  a.coef.hyperprec ~ dgamma(1, 0.001)
  b.coef.hyperprec ~ dgamma(1, 0.001)

  ## Joint inclusion indicators
  ind.joint <- a.pip * b.pip
}
")

  return(modelstring)
}
