// PolarisCoreEmrDevice.fsh - V1.1 - 2025-04-09
// Defines the Polaris EMR Device profile based on the FHIR Device resource.
// Captures manufacturer, product name (as model), type, version, and owner.
// Enforces Polaris identifier syntax for the owner reference.
// Uses a non-instance-specific identifier for the Device itself.
// Includes embedded dependencies.

// --- Embedded Aliases ---
Alias: $PolarisCoreEmrDevice = https://fhir.apps.health/StructureDefinition/polaris-core-emrdevice


// emrdevice self identifier rule
// emrdevice identifier must have at least one identifier whose system matches the Polaris EMR identifier pattern
Invariant:     emrdevice-has-polaris-id
Description:   "Must have at least one identifier whose system matches the Polaris EMR pattern."
Severity:      #error
Expression:    "identifier.exists(system.matches('^https://fhir.apps.health/NamingSystem/[A-Za-z0-9._-]+-device-identifier$'))"


// --- Profile Definition ---

Profile:        PolarisCoreEmrDevice
Parent:         Device
Id:             polaris-core-emrdevice
Title:          "Polaris EMR Device Core"
Description:    "Represents an EMR software application/product, capturing key details like manufacturer, product name/model, type, version, and the owning organization using standard Polaris references. Uses a non-instance-specific identifier for the Device itself."
* ^url = $PolarisCoreEmrDevice
* ^status = #draft
* ^publisher = "Polaris Project"
* obeys emrdevice-has-polaris-id

// --- Element Constraints ---

* . ^short      = "Polaris EMR Device Core"
* . ^definition = """
  Represents an EMR software application/product, capturing key details
  like manufacturer, product name/model, type, version, and the owning
  organization using standard Polaris references. Uses a non‑instance‑
  specific identifier for the Device itself.
  """

* meta.source 0..1 MS
* meta.source ^short = "Identifies EMR instance & pipeline version the resource came from"
* meta.source ^definition = "A URI that identifies the EMR pipeline and version from which this resource originated.  This tells you which EMR instance (recommend using the instance identifier), and the version of the pipeline code/transformations."

* text 0..1 MS // Narrative element with human-readable summary
* text ^short = "Narrative"
* text ^definition = "A human-readable narrative summary of the PolarisCoreEmrDevice resource for human interpretation."

* id 0..1 MS

// Identifier: Must include a non-instance-specific Polaris identifier for the device.
* identifier 1..* MS

* identifier ^short = "Business identifier(s) for emrdevice"
* identifier ^definition = """
Unique identifiers for the emrdevice.

At least one must use the Polaris naming convention pattern.

When a source/local 'raw code' identifier exists (e.g. from source EMR),
it SHOULD be included as an additional identifier.

If the raw code's system is known and appropriate, use that.
If no recognized system exists, implementers MAY:
  • invent a URI for the system, OR
  • use the fallback URI template: https://fhir.apps.health/NamingSystem/<instance-id>-emrdevice-raw-code
"""

// Identifier slicing setup
* identifier ^slicing.discriminator.type = #pattern
* identifier ^slicing.discriminator.path = "system"
* identifier ^slicing.rules = #open
* identifier ^slicing.description = "Slice identifiers by system"

// Define slices
* identifier contains 
    polarisId 1..1 MS and
    rawCode 0..* MS

// Polaris ID slice (required)
* identifier[polarisId] ^short = "Polaris EmrDevice ID"
* identifier[polarisId] ^definition = "The primary Polaris identifier for this emrdevice, following the standard Polaris naming pattern"
* identifier[polarisId].system 1..1
* identifier[polarisId].system ^short = "Polaris emrdevice identifier system: https://fhir.apps.health/NamingSystem/<instance-id>-emrdevice-identifier"
* identifier[polarisId].system ^definition = "Must match the pattern: https://fhir.apps.health/NamingSystem/<instance-id>-emrdevice-identifier"
* identifier[polarisId].system ^comment = "Example: https://fhir.apps.health/NamingSystem/clinic-12345-emrdevice-identifier"
* identifier[polarisId].value 1..1
* identifier[polarisId].value ^short = "The actual Polaris emrdevice ID value: unique in source system"
* identifier[polarisId].use = #official

// Raw code identifier slice (optional, multiple allowed)
* identifier[rawCode] ^short = "Source EMR emrdevice identifier"
* identifier[rawCode] ^definition = "The original raw identifier(s) from the source EMR system"
* identifier[rawCode].system 1..1
* identifier[rawCode].system ^short = "Source system URI or https://fhir.apps.health/NamingSystem/<instance-id>-emrdevice-raw-code"
* identifier[rawCode].system ^definition = """
URI identifying the code system for the raw code.
If the system is known, use the appropriate URI.
If unknown, use a constructed URI or the fallback:
`https://fhir.apps.health/NamingSystem/<instance-id>-emrdevice-raw-code`
"""
* identifier[rawCode].value 1..1
* identifier[rawCode].use = #secondary
// Manufacturer: Name of the vendor. Mandatory. MS.
* manufacturer 1..1 MS
* manufacturer ^short = "EMR vendor name"
* manufacturer ^definition = "The name of the company or organization that produces the EMR software."
* manufacturer ^comment = "Examples include Epic, Cerner, Allscripts, or custom vendor names."

// Device Name: Slice to ensure model name is captured. Mandatory. MS.
* deviceName 1..1 MS
* deviceName ^short = "EMR product name"
* deviceName ^definition = "The name of the EMR software product or application."
* deviceName ^comment = "Should contain the commercial product name or model name of the EMR."
* deviceName.name 1..1 MS
* deviceName.name ^short = "The actual product name"
* deviceName.name ^definition = "The specific name of the EMR product."
* deviceName.type 1..1 MS // user-friendly-name, registered-name, patient-reported-name, model-name
* deviceName.type ^short = "Type of device name"
* deviceName.type ^definition = "The type of name being provided (typically 'model-name' for EMR products)."

// Type: Fixed to indicate software. Mandatory. MS.
* type 1..1 MS
* type ^short = "Device type - always 'software'"
* type ^definition = "Categorizes this device as software rather than hardware."
* type ^comment = "Fixed value to distinguish EMR software from physical medical devices."
* type.text = "software" (exactly)

// Version: Capture the software version string. Mandatory. MS.
* version 1..1 MS
* version ^short = "EMR software version information"
* version ^definition = "Version number or identifier of the EMR software."
* version ^comment = "May include major.minor.patch version numbers or build identifiers."
* version.value 1..1 MS
* version.value ^short = "Version string"
* version.value ^definition = "The actual version identifier of the EMR software."

// Owner (Organization): Reference via Identifier. Mandatory. MS.
* owner 1..1 MS
* owner ^short = "Organization that owns/operates the EMR"
* owner ^definition = "The organization that owns or operates this EMR instance. Must reference using Polaris organization identifier."
* owner ^comment = "Typically the healthcare organization or clinic using the EMR."
* owner only Reference($PolarisCoreOrganization)
