package com.jacoby6000.smithplates.testkit

import software.amazon.smithy.model.Model

import java.io.InputStream
import java.nio.charset.StandardCharsets

object SqlItModelLoader {
  val SqlSchemaTraitsModelId: String  = "META-INF/smithy/smithplates.codegen.sql.smithy"
  val SqlServiceTraitsModelId: String = "META-INF/smithy/smithplates.codegen.sql.service.smithy"

  def assemble(additionalModels: (String, String)*): Model = {
    val assembler = internal.assemblerWithSqlTraits
    additionalModels.foreach { case (id, content) => assembler.addUnparsedModel(id, content) }
    assembler.assemble().unwrap()
  }

  /** Internal implementation surface — not part of the stable API; subject to change without notice. */
  object internal {
    def assemblerWithSqlTraits = {
      val assembler = Model.assembler().disableValidation()
      assembler.addUnparsedModel(SqlSchemaTraitsModelId, readClasspathResource(SqlSchemaTraitsModelId))
      assembler.addUnparsedModel(SqlServiceTraitsModelId, readClasspathResource(SqlServiceTraitsModelId))
      assembler
    }

    def readClasspathResource(path: String): String = {
      val stream = Option(SqlItModelLoader.getClass.getClassLoader.getResourceAsStream(path)).getOrElse {
        throw new IllegalStateException(
          s"SQL traits Smithy model not on classpath at '$path'. " +
            "Ensure smithplates-sql-ir and smithplates-sql-service-ir are on the classpath."
        )
      }
      try
        readStream(stream)
      finally
        stream.close()
    }

    def readStream(stream: InputStream): String =
      new String(stream.readAllBytes(), StandardCharsets.UTF_8)
  }
}
