# Schema: https://signstack.ai/schemas/blueprint/v1beta2

# ===== Employee Onboarding Blueprint =====
apiVersion: signstack/v1beta2
kind: Blueprintt # Intentionally misspelled to filter out from CLI operations

metadata:
  key: employee_onboarding
  version: 1.0.0
  name: Employee Onboarding Workflow
  description: Complete employee onboarding with offer letter, NDA, and benefits enrollment
  labels:
    department: hr
    process: onboarding

spec:
  inputs:
    - key: employee
      name: Employee Information
      schema: employee@1.0.0

    - key: company
      name: Company Details
      schema: company@1.0.0

    - key: position
      name: Position Details
      schema: position@1.0.0

  functions:
    format_currency: format_currency@1.0.0
    calculate_probation_end: calculate_probation_end@1.0.0

  participants:
    - key: new_hire
      name:
        expr: "$.employee.firstName & ' ' & $.employee.lastName"
      email:
        expr: '$.employee.email'

    - key: hr_manager
      name:
        expr: '$.company.hrManager.name'
      email:
        expr: '$.company.hrManager.email'

    - key: hiring_manager
      name:
        expr: '$.position.hiringManager.name'
      email:
        expr: '$.position.hiringManager.email'

  envelopes:
    - key: offer_envelope
      name: Offer Letter Package
      subject: 'Your Offer Letter from Acme Corp'
      documents:
        # No inputMap needed - keys match (employee, position, company)
        - key: offer_letter
          name: Offer Letter
          template: offer_letter@1.0.0

        # No inputMap needed - position auto-mapped
        - key: position_description
          name: Position Description
          template: position_description@1.0.0

    - key: compliance_envelope
      name: Compliance Documents
      subject: 'Compliance Documents for Signing'
      documents:
        # inputMap required - template expects 'contact', blueprint has 'employee'
        - key: nda
          name: Non-Disclosure Agreement
          template: simple_nda@1.0.0
          inputMap:
            contact: '$.employee'

        # No inputMap needed - employee and company auto-mapped
        - key: ip_agreement
          name: IP Assignment Agreement
          template: ip_agreement@1.0.0

    - key: benefits_envelope
      name: Benefits Enrollment
      includeIf: '$.position.includesBenefits'
      documents:
        # No inputMap needed - employee auto-mapped
        - key: benefits_form
          name: Benefits Selection Form
          template: benefits_enrollment@1.0.0

        # iterator: one document per supplement in the array
        - key: supplement
          name: Supplement Document
          template: supplement_template@1.0.0
          iterator: '$.position.supplements'
          includeIf: '__iterator__.current.required'
          fileId:
            expr: '__iterator__.current.fileId'

  orchestration:
    key: root
    type: group
    execution: sequential
    children:
      # Phase 1: Offer acceptance
      - key: offer_phase
        type: group
        name: Offer Acceptance Phase
        execution: sequential
        children:
          - key: hr_sends_offer
            type: participant
            participant: hr_manager
            action:
              type: sign
              envelope: offer_envelope
              assignments:
                - document: offer_letter
                  role: hr_representative
                - document: position_description
                  role: hr_representative

          - key: manager_approves_offer
            type: participant
            participant: hiring_manager
            includeIf: '$.position.level > 3'
            action:
              type: sign
              envelope: offer_envelope
              assignments:
                - document: offer_letter
                  role: manager

          - key: new_hire_accepts_offer
            type: participant
            participant: new_hire
            action:
              type: sign
              envelope: offer_envelope
              assignments:
                - document: offer_letter
                  role: employee
                - document: position_description
                  role: employee
            # onComplete: validation and data updates when step completes
            onComplete:
              validate: '$exists($envelope.signedAt)'
              updates:
                - key: employee
                  transform: |
                    {
                      "acceptedAt": $now(),
                      "signedOfferId": $envelope.id,
                      "status": "offer_accepted"
                    }

      # Phase 2: Compliance documents (parallel)
      - key: compliance_phase
        type: group
        name: Compliance Phase
        execution: parallel
        # Group-level onComplete: runs when all children complete
        onComplete:
          updates:
            - key: employee
              transform: |
                {
                  "complianceCompletedAt": $now(),
                  "ndaSigned": true,
                  "ipAgreementSigned": true
                }
        children:
          - key: new_hire_signs_nda
            type: participant
            participant: new_hire
            action:
              type: sign
              envelope: compliance_envelope
              assignments:
                - document: nda
                  role: signer
                - document: ip_agreement
                  role: signer

      # Phase 3: Benefits (conditional)
      - key: benefits_phase
        type: group
        name: Benefits Enrollment Phase
        execution: sequential
        includeIf: '$.position.includesBenefits'
        children:
          - key: new_hire_selects_benefits
            type: participant
            participant: new_hire
            action:
              type: sign
              envelope: benefits_envelope
              assignments:
                - document: benefits_form
                  role: employee

---
# ===== Simple NDA Blueprint =====
# This example shows a single participant step as the root orchestration
apiVersion: signstack/v1beta2
kind: Blueprintt # Intentionally misspelled to filter out from CLI operations

metadata:
  key: simple_nda_workflow
  version: 1.0.0
  name: Simple NDA Workflow
  description: Single document NDA signing workflow

spec:
  inputs:
    # Blueprint input key is 'signer_info', but template expects 'contact'
    - key: signer_info
      schema: contact@1.0.0

  participants:
    - key: signer
      name:
        expr: '$.signer_info.name'
      email:
        expr: '$.signer_info.email'

  envelopes:
    - key: nda_envelope
      documents:
        - key: nda
          template: simple_nda@1.0.0
          # inputMap required because keys differ: template expects 'contact'
          inputMap:
            contact: '$.signer_info'

  # Simple workflow: single participant step as root
  orchestration:
    key: signer_signs_nda
    type: participant
    participant: signer
    action:
      type: sign
      envelope: nda_envelope
      assignments:
        - document: nda
          role: signer

---
# ===== Multi-Party Contract Blueprint =====
apiVersion: signstack/v1beta2
kind: Blueprintt # Intentionally misspelled to filter out from CLI operations

metadata:
  key: multi_party_contract
  version: 1.0.0
  name: Multi-Party Contract
  description: Contract requiring signatures from multiple parties in parallel

spec:
  inputs:
    - key: party_a
      schema: company@1.0.0

    - key: party_b
      schema: company@1.0.0

    - key: contract_details
      schema: contract_data@1.0.0

  participants:
    - key: party_a_signer
      name:
        expr: '$.party_a.representative.name'
      email:
        expr: '$.party_a.representative.email'

    - key: party_b_signer
      name:
        expr: '$.party_b.representative.name'
      email:
        expr: '$.party_b.representative.email'

    - key: witness
      name:
        expr: '$.contract_details.witness.name'
      email:
        expr: '$.contract_details.witness.email'

  envelopes:
    - key: contract_envelope
      documents:
        - key: main_contract
          template: business_contract@1.0.0
          # Partial inputMap: party_a and party_b auto-mapped, only 'details' needs mapping
          inputMap:
            details: '$.contract_details'

        - key: exhibit_a
          template: contract_exhibit@1.0.0
          # Custom transformation example: combine data from multiple inputs
          inputMap:
            details: '$.contract_details'
            combined_parties: |
              {
                "primary": $.party_a.name,
                "secondary": $.party_b.name,
                "effectiveDate": $.contract_details.effectiveDate
              }

  # Custom fields are included in webhook payloads as additional metadata
  customFields:
    - key: contract_type
      name: Contract Type
      value: business_agreement
    - key: department
      name: Department
      value: legal
    - key: total_parties
      name: Total Parties
      value:
        expr: '$count($.party_a) + $count($.party_b)'
    - key: effective_date
      name: Effective Date
      value:
        expr: '$.contract_details.effectiveDate'

  orchestration:
    key: root
    type: group
    execution: sequential
    children:
      # Both parties sign in parallel
      - key: signing_phase
        type: group
        execution: parallel
        children:
          - key: party_a_signs
            type: participant
            participant: party_a_signer
            action:
              type: sign
              envelope: contract_envelope
              assignments:
                - document: main_contract
                  role: party_a
                - document: exhibit_a
                  role: party_a

          - key: party_b_signs
            type: participant
            participant: party_b_signer
            action:
              type: sign
              envelope: contract_envelope
              assignments:
                - document: main_contract
                  role: party_b
                - document: exhibit_a
                  role: party_b

      # Witness signs after both parties
      - key: witness_signs
        type: participant
        participant: witness
        action:
          type: sign
          envelope: contract_envelope
          assignments:
            - document: main_contract
              role: witness

---
# ===== Board Resolution Blueprint =====
# This example demonstrates quorum-based approval (2 of 3 board members)
apiVersion: signstack/v1beta2
kind: Blueprintt # Intentionally misspelled to filter out from CLI operations

metadata:
  key: board_resolution
  version: 1.0.0
  name: Board Resolution Approval
  description: Resolution requiring approval from 2 of 3 board members

spec:
  inputs:
    - key: resolution
      schema: resolution@1.0.0

    - key: board_members
      schema: board_members@1.0.0

  participants:
    - key: board_member_1
      name:
        expr: '$.board_members[0].name'
      email:
        expr: '$.board_members[0].email'

    - key: board_member_2
      name:
        expr: '$.board_members[1].name'
      email:
        expr: '$.board_members[1].email'

    - key: board_member_3
      name:
        expr: '$.board_members[2].name'
      email:
        expr: '$.board_members[2].email'

  envelopes:
    - key: resolution_envelope
      documents:
        - key: resolution_doc
          template: board_resolution@1.0.0

  orchestration:
    key: root
    type: group
    execution: sequential
    children:
      # Board approval phase: 2 of 3 must sign (quorum)
      - key: board_approval
        type: group
        name: Board Approval
        execution: parallel
        completion: quorum
        quorum: 2
        children:
          - key: member_1_signs
            type: participant
            participant: board_member_1
            action:
              type: sign
              envelope: resolution_envelope
              assignments:
                - document: resolution_doc
                  role: board_member

          - key: member_2_signs
            type: participant
            participant: board_member_2
            action:
              type: sign
              envelope: resolution_envelope
              assignments:
                - document: resolution_doc
                  role: board_member

          - key: member_3_signs
            type: participant
            participant: board_member_3
            action:
              type: sign
              envelope: resolution_envelope
              assignments:
                - document: resolution_doc
                  role: board_member
