# Tests that complex schema definitions with multiple type aliases, comprehensions,
# and nested struct unification evaluate correctly in the v3 evaluator. The issue
# reported that evalv3 produced validation errors for code accepted by evalv2.
# https://cuelang.org/issue/3805
-- x.cue --
#PatientViewSections: {
	[string]: #PatientViewSection
}

#PatientViewSection: #PvSection

#PvSection: {
	Section: {
		display?: string
		entries:  #PatientViewEntries
	}
	_json: [for k, v in Section.entries {(k): v._json}]
}

#PatientViewEntries: [string]: #PatientViewEntry

#PatientViewEntry: {
	#PvSingleForm | #PvLink
}

#PvSingleForm: {
	SingleForm: {
		formId:                 string
		showNumberOfInstances?: bool
	}
	_json: [{
		"iDoc":     "singleFormIDoc"
		"X-FormId": SingleForm.formId
	}]
}

#PvLink: {
	Link: {
		icon?:    string
		sections: #PatientViewSections
	}
	_json: [for k, v in Link.sections {(k): v._json}]
}

#TabcatsJson: {
	config: {
		tabcats: #PatientViewSections
		...
	}
	json: {
		//tabcats: #OrgContents
		tabcats: [for k, v in config.tabcats {(k): v._json}]
	}
}

tabcatsExample: #TabcatsJson
tabcatsExample: {
	_e1: #PatientViewEntry
	_e1: {
		Link: {
			sections: "Formulare": {
				Section: entries: "Fotodokumentation": _e2
			}
		}
	}
	_e2: #PatientViewEntry
	_e2: {
		SingleForm: {
			formId: "fotodokumentation"
		}
	}
	_s: #PvSection
	_s: Section: entries: "Dokumentation": _e1
	config: {
		tabcats: "02": _s
	}
}
-- out/compile --
--- x.cue
{
  #PatientViewSections: {
    [string]: 〈1;#PatientViewSection〉
  }
  #PatientViewSection: 〈0;#PvSection〉
  #PvSection: {
    Section: {
      display?: string
      entries: 〈2;#PatientViewEntries〉
    }
    _json: [
      for k, v in 〈1;Section〉.entries {
        〈1;k〉: 〈1;v〉._json
      },
    ]
  }
  #PatientViewEntries: {
    [string]: 〈1;#PatientViewEntry〉
  }
  #PatientViewEntry: {
    (〈1;#PvSingleForm〉|〈1;#PvLink〉)
  }
  #PvSingleForm: {
    SingleForm: {
      formId: string
      showNumberOfInstances?: bool
    }
    _json: [
      {
        iDoc: "singleFormIDoc"
        "X-FormId": 〈2;SingleForm〉.formId
      },
    ]
  }
  #PvLink: {
    Link: {
      icon?: string
      sections: 〈2;#PatientViewSections〉
    }
    _json: [
      for k, v in 〈1;Link〉.sections {
        〈1;k〉: 〈1;v〉._json
      },
    ]
  }
  #TabcatsJson: {
    config: {
      tabcats: 〈2;#PatientViewSections〉
      ...
    }
    json: {
      tabcats: [
        for k, v in 〈2;config〉.tabcats {
          〈1;k〉: 〈1;v〉._json
        },
      ]
    }
  }
  tabcatsExample: 〈0;#TabcatsJson〉
  tabcatsExample: {
    _e1: 〈1;#PatientViewEntry〉
    _e1: {
      Link: {
        sections: {
          Formulare: {
            Section: {
              entries: {
                Fotodokumentation: 〈6;_e2〉
              }
            }
          }
        }
      }
    }
    _e2: 〈1;#PatientViewEntry〉
    _e2: {
      SingleForm: {
        formId: "fotodokumentation"
      }
    }
    _s: 〈1;#PvSection〉
    _s: {
      Section: {
        entries: {
          Dokumentation: 〈3;_e1〉
        }
      }
    }
    config: {
      tabcats: {
        "02": 〈2;_s〉
      }
    }
  }
}
