//    Copyright 2019 Google Inc.
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        https://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

syntax = "proto3";

package google.fhir.r4.core;

import "google/protobuf/any.proto";
import "google/fhir/core/proto/annotations.proto";
import "google/fhir/r4/proto/core/codes.proto";
import "google/fhir/r4/proto/core/datatypes.proto";

option java_multiple_files = true;
option java_package = "com.google.fhir.r4.core";
option (.google.fhir.proto.fhir_version) = R4;
option go_package = "github.com/google/fhir/go/google/fhir/r4/proto/core/resources/consent_go_proto";

// Auto-generated from StructureDefinition for Consent.
// A healthcare consumer's  choices to permit or deny recipients or roles to
// perform actions for specific purposes and periods of time. See
// http://hl7.org/fhir/StructureDefinition/Consent
message Consent {
  option (.google.fhir.proto.structure_definition_kind) = KIND_RESOURCE;
  option (.google.fhir.proto.fhir_structure_definition_url) =
      "http://hl7.org/fhir/StructureDefinition/Consent";
  option (.google.fhir.proto.fhir_path_message_constraint) =
      "policy.exists() or policyRule.exists()";
  option (.google.fhir.proto.fhir_path_message_constraint) =
      "patient.exists() or scope.coding.where(system='something' and code='patient-privacy').exists().not()";
  option (.google.fhir.proto.fhir_path_message_constraint) =
      "patient.exists() or scope.coding.where(system='something' and code='research').exists().not()";
  option (.google.fhir.proto.fhir_path_message_constraint) =
      "patient.exists() or scope.coding.where(system='something' and code='adr').exists().not()";
  option (.google.fhir.proto.fhir_path_message_constraint) =
      "patient.exists() or scope.coding.where(system='something' and code='treatment').exists().not()";

  // Logical id of this artifact
  Id id = 1;

  // Metadata about the resource
  Meta meta = 2;

  // A set of rules under which this content was created
  Uri implicit_rules = 3;

  // Language of the resource content
  Code language = 4;

  // Text summary of the resource, for human interpretation
  Narrative text = 5;

  // Contained, inline Resources
  repeated protobuf.Any contained = 6;

  // Field 7 reserved for strongly-typed ContainedResource for id:
  // Consent.contained
  reserved 7;

  // Additional content defined by implementations
  repeated Extension extension = 8;

  // Extensions that cannot be ignored
  repeated Extension modifier_extension = 9;

  // Identifier for this record (external references)
  repeated Identifier identifier = 10;

  // draft | proposed | active | rejected | inactive | entered-in-error
  message StatusCode {
    option (.google.fhir.proto.structure_definition_kind) = KIND_PRIMITIVE_TYPE;
    option (.google.fhir.proto.fhir_valueset_url) =
        "http://hl7.org/fhir/ValueSet/consent-state-codes";
    option (.google.fhir.proto.fhir_profile_base) =
        "http://hl7.org/fhir/StructureDefinition/code";

    ConsentStateCode.Value value = 1;

    String id = 2;

    repeated Extension extension = 3;
  }
  StatusCode status = 11
      [(.google.fhir.proto.validation_requirement) = REQUIRED_BY_FHIR];

  // Which of the four areas this resource covers (extensible)
  CodeableConcept scope = 12
      [(.google.fhir.proto.validation_requirement) = REQUIRED_BY_FHIR];

  // Classification of the consent statement - for indexing/retrieval
  repeated CodeableConcept category = 13
      [(.google.fhir.proto.validation_requirement) = REQUIRED_BY_FHIR];

  // Who the consent applies to
  Reference patient = 14
      [(.google.fhir.proto.valid_reference_type) = "Patient"];

  // When this Consent was created or indexed
  DateTime date_time = 15;

  // Who is agreeing to the policy and rules
  repeated Reference performer = 16 [
    (.google.fhir.proto.valid_reference_type) = "Organization",
    (.google.fhir.proto.valid_reference_type) = "Patient",
    (.google.fhir.proto.valid_reference_type) = "Practitioner",
    (.google.fhir.proto.valid_reference_type) = "RelatedPerson",
    (.google.fhir.proto.valid_reference_type) = "PractitionerRole"
  ];

  // Custodian of the consent
  repeated Reference organization = 17
      [(.google.fhir.proto.valid_reference_type) = "Organization"];

  // Source from which this consent is taken
  message SourceX {
    option (.google.fhir.proto.is_choice_type) = true;

    oneof choice {
      Attachment attachment = 1;
      Reference reference = 2 [
        (.google.fhir.proto.valid_reference_type) = "Consent",
        (.google.fhir.proto.valid_reference_type) = "DocumentReference",
        (.google.fhir.proto.valid_reference_type) = "Contract",
        (.google.fhir.proto.valid_reference_type) = "QuestionnaireResponse"
      ];
    }
  }
  SourceX source = 18;

  // Policies covered by this consent
  message Policy {
    // Unique id for inter-element referencing
    String id = 1;

    // Additional content defined by implementations
    repeated Extension extension = 2;

    // Extensions that cannot be ignored even if unrecognized
    repeated Extension modifier_extension = 3;

    // Enforcement source for policy
    Uri authority = 4;

    // Specific policy covered by this consent
    Uri uri = 5;
  }
  repeated Policy policy = 19;

  // Regulation that this consents to
  CodeableConcept policy_rule = 20;

  // Consent Verified by patient or family
  message Verification {
    // Unique id for inter-element referencing
    String id = 1;

    // Additional content defined by implementations
    repeated Extension extension = 2;

    // Extensions that cannot be ignored even if unrecognized
    repeated Extension modifier_extension = 3;

    // Has been verified
    Boolean verified = 4
        [(.google.fhir.proto.validation_requirement) = REQUIRED_BY_FHIR];

    // Person who verified
    Reference verified_with = 5 [
      (.google.fhir.proto.valid_reference_type) = "Patient",
      (.google.fhir.proto.valid_reference_type) = "RelatedPerson"
    ];

    // When consent verified
    DateTime verification_date = 6;
  }
  repeated Verification verification = 21;

  // Constraints to the base Consent.policyRule
  message Provision {
    // Unique id for inter-element referencing
    String id = 1;

    // Additional content defined by implementations
    repeated Extension extension = 2;

    // Extensions that cannot be ignored even if unrecognized
    repeated Extension modifier_extension = 3;

    // deny | permit
    message TypeCode {
      option (.google.fhir.proto.structure_definition_kind) =
          KIND_PRIMITIVE_TYPE;
      option (.google.fhir.proto.fhir_valueset_url) =
          "http://hl7.org/fhir/ValueSet/consent-provision-type";
      option (.google.fhir.proto.fhir_profile_base) =
          "http://hl7.org/fhir/StructureDefinition/code";

      ConsentProvisionTypeCode.Value value = 1;

      String id = 2;

      repeated Extension extension = 3;
    }
    TypeCode type = 4;

    // Timeframe for this rule
    Period period = 5;

    // Who|what controlled by this rule (or group, by role)
    message ProvisionActor {
      // Unique id for inter-element referencing
      String id = 1;

      // Additional content defined by implementations
      repeated Extension extension = 2;

      // Extensions that cannot be ignored even if unrecognized
      repeated Extension modifier_extension = 3;

      // How the actor is involved
      CodeableConcept role = 4
          [(.google.fhir.proto.validation_requirement) = REQUIRED_BY_FHIR];

      // Resource for the actor (or group, by role)
      Reference reference = 5 [
        (.google.fhir.proto.validation_requirement) = REQUIRED_BY_FHIR,
        (.google.fhir.proto.valid_reference_type) = "Device",
        (.google.fhir.proto.valid_reference_type) = "Group",
        (.google.fhir.proto.valid_reference_type) = "CareTeam",
        (.google.fhir.proto.valid_reference_type) = "Organization",
        (.google.fhir.proto.valid_reference_type) = "Patient",
        (.google.fhir.proto.valid_reference_type) = "Practitioner",
        (.google.fhir.proto.valid_reference_type) = "RelatedPerson",
        (.google.fhir.proto.valid_reference_type) = "PractitionerRole"
      ];
    }
    repeated ProvisionActor actor = 6;

    // Actions controlled by this rule
    repeated CodeableConcept action = 7;

    // Security Labels that define affected resources
    repeated Coding security_label = 8;

    // Context of activities covered by this rule
    repeated Coding purpose = 9;

    // e.g. Resource Type, Profile, CDA, etc.
    repeated Coding class_value = 10 [json_name = "class"];

    // e.g. LOINC or SNOMED CT code, etc. in the content
    repeated CodeableConcept code = 11;

    // Timeframe for data controlled by this rule
    Period data_period = 12;

    // Data controlled by this rule
    message ProvisionData {
      // Unique id for inter-element referencing
      String id = 1;

      // Additional content defined by implementations
      repeated Extension extension = 2;

      // Extensions that cannot be ignored even if unrecognized
      repeated Extension modifier_extension = 3;

      // instance | related | dependents | authoredby
      message MeaningCode {
        option (.google.fhir.proto.structure_definition_kind) =
            KIND_PRIMITIVE_TYPE;
        option (.google.fhir.proto.fhir_valueset_url) =
            "http://hl7.org/fhir/ValueSet/consent-data-meaning";
        option (.google.fhir.proto.fhir_profile_base) =
            "http://hl7.org/fhir/StructureDefinition/code";

        ConsentDataMeaningCode.Value value = 1;

        String id = 2;

        repeated Extension extension = 3;
      }
      MeaningCode meaning = 4
          [(.google.fhir.proto.validation_requirement) = REQUIRED_BY_FHIR];

      // The actual data reference
      Reference reference = 5 [
        (.google.fhir.proto.validation_requirement) = REQUIRED_BY_FHIR,
        (.google.fhir.proto.valid_reference_type) = "Resource"
      ];
    }
    repeated ProvisionData data = 13;

    // Nested Exception Rules
    repeated Provision provision = 14;
  }
  Provision provision = 22;
}
