package appsource

import "strings"

// OfficialAppPacksSourceRevision is the app-packs repository checkout revision
// used by CI/Docker to provide first-party backend source. Product runtime
// validators must use the per-pack lock revisions below, because an app-packs
// repo commit can advance for one product without changing another product's
// accepted installed source revision. The revision must stay on the app-packs
// main lineage so a normal main checkout can resolve it.
// fbb0db21 is the nexuscrm-app-packs#864 merge commit. Its transitional
// dialect no longer consumes the discovery and resource-document contracts
// retired by Core #4818.
// Per-pack installation locks below remain independent of this build input.
const OfficialAppPacksSourceRevision = "fbb0db216c3db5c25c2b9eaa40d0056e64b37c43"

// Executable L1 AppServices are independently published artifacts selected by
// the L2 deployment BOM. They intentionally have no source revision in this L0
// distribution source-supply file.

const (
	SalesforceCRMStandardPackName = "salesforce-crm-standard"

	// RetiredIssueTrackerStandardPackName is a negative composition tombstone,
	// not an installable pack or a product dependency. Historical builds could
	// persist this name as an active first-party product pack. Keeping the
	// identity in the L1 distribution coverage list makes current CRM startup
	// and setup fail closed until that obsolete row is deactivated; no runtime
	// surface, source payload, dialect, frontend, or fixture is retained.
	RetiredIssueTrackerStandardPackName = "issue-tracker-standard"

	// d05e54b5 is the merge of nexuscrm-app-packs#816. It contains the latest
	// reviewed CRM pack source and every payload contract required by the
	// current distribution runtime.
	SalesforceCRMStandardPackSourceRevision = "d05e54b587602050e48b1976c769edeb75eb98f8"

	// SalesforceCRMStandardPackPriorSourceRevision is the immediately
	// preceding shipped CRM pack revision. It has first-class CustomPermission
	// declarations but predates the deactivation-ticket mutation policy; stopped
	// apply advances it before the native SCIM route can serve.
	SalesforceCRMStandardPackPriorSourceRevision = "6f82feb41d0402151ea78af67ba2a98951d565fb"

	// SalesforceCRMStandardPackPrior2SourceRevision is the per-product CRM
	// pack revision shipped before SalesforceCRMStandardPackPriorSourceRevision.
	// It has first-class CustomPermission declarations but predates the neutral
	// assignment-policy Catalog mapping.
	SalesforceCRMStandardPackPrior2SourceRevision = "fc272da8700b0c2689e75788b513ccfb843f9ffc"

	// SalesforceCRMStandardPackPrior3SourceRevision is a historical per-product
	// CRM pack revision shipped before SalesforceCRMStandardPackPrior2SourceRevision.
	// Its payload contract remains available for stopped-upgrade diagnostics,
	// but the source is not bootable after CustomPermission exact-set activation.
	SalesforceCRMStandardPackPrior3SourceRevision = "fa7c3e05aeded37ecaab1de97b065f7f39887562"

	// SalesforceCRMStandardPackPrior4SourceRevision is a historical per-product
	// CRM pack revision shipped before SalesforceCRMStandardPackPrior3SourceRevision.
	// Its payload contract remains available for stopped-upgrade diagnostics,
	// but the source is not bootable after CustomPermission exact-set activation.
	SalesforceCRMStandardPackPrior4SourceRevision = "2425c00c77e968b0769d6bd247b59831a17de894"

	// SalesforceCRMStandardPackPrior5SourceRevision is a historical per-product
	// CRM pack revision shipped before SalesforceCRMStandardPackPrior4SourceRevision.
	// It has aged out of the bounded transition window and is no longer accepted
	// by the current CRM distribution lock.
	SalesforceCRMStandardPackPrior5SourceRevision = "1ad078b876614e6d1ff9472035b11ae3cc5c1ac1"

	// SalesforceCRMStandardPackPrior6SourceRevision is a historical per-product
	// CRM pack revision shipped before SalesforceCRMStandardPackPrior5SourceRevision.
	// It has aged out of the bounded transition window and is no longer accepted
	// by the current CRM distribution lock.
	SalesforceCRMStandardPackPrior6SourceRevision = "481cf292ac3fb353834014f7c307cfabcb4ffd6d"

	// SalesforceCRMStandardPackPrior7SourceRevision is a historical per-product
	// CRM pack revision shipped before SalesforceCRMStandardPackPrior6SourceRevision.
	// It has aged out of the bounded transition window and is no longer
	// accepted by the current CRM distribution lock.
	SalesforceCRMStandardPackPrior7SourceRevision = "69d51f83f96535e06dce0061d84d46e3b8a62a1a"

	// SalesforceCRMStandardPackPrior8SourceRevision is a historical per-product
	// CRM pack revision shipped before SalesforceCRMStandardPackPrior7SourceRevision.
	// It has aged out of the bounded transition window and is no longer
	// accepted by the current CRM distribution lock.
	SalesforceCRMStandardPackPrior8SourceRevision = "10770ea66e4802b8e60ad0687c5a017807096eb3"

	// SalesforceCRMStandardPackPrior9SourceRevision is a historical per-product
	// CRM pack revision shipped before SalesforceCRMStandardPackPrior8SourceRevision.
	// It has aged out of the bounded transition window and is no longer
	// accepted by the current CRM distribution lock.
	SalesforceCRMStandardPackPrior9SourceRevision = "e4c1d998c767d817981e69aece04e21b972af571"

	// SalesforceCRMStandardPackPrior10SourceRevision is a historical per-product
	// CRM pack revision shipped before SalesforceCRMStandardPackPrior9SourceRevision.
	// It predates the app-pack-owned _System_FolderType catalog and is no longer
	// accepted by the current CRM distribution lock.
	SalesforceCRMStandardPackPrior10SourceRevision = "66a65579c3bdab15b963bc519fa9b6b017846635"

	// SalesforceCRMStandardPackPrior11SourceRevision is the historical CRM pack
	// revision shipped before SalesforceCRMStandardPackPrior10SourceRevision.
	// It predates the app-pack-owned _System_FolderType catalog and is no longer
	// accepted by the current CRM distribution lock.
	SalesforceCRMStandardPackPrior11SourceRevision = "9722897a707cf32e55b36cef17c18ac533de4438"

	// SalesforceCRMStandardPackLegacyRepoWideSourceRevision is the repo-wide
	// app-packs checkout revision written by #2386 before official_repo fresh
	// installs learned to persist the CRM per-product lock. It predates the
	// app-pack-owned _System_FolderType catalog and is no longer accepted by the
	// current CRM distribution lock.
	SalesforceCRMStandardPackLegacyRepoWideSourceRevision = "adda65d91d3a0942e2419e34ea9224d540256003"
)

// SalesforceCRMStandardPackSourceContract records the payload capabilities of
// one accepted CRM pack revision. Source revision acceptance and payload
// validation consume this same contract so a revision cannot remain bootable
// after the compiled validator outgrows the payload that revision shipped.
type SalesforceCRMStandardPackSourceContract struct {
	// DeclaresCustomPermissionExactSet is the activation boundary for the
	// managed CustomPermission owner set. Once the runtime advertises ExactSet,
	// a source without first-class declarations would be interpreted as an
	// authoritative empty set. Such a revision remains describable for stopped
	// upgrade diagnostics but is no longer a bootable CRM source.
	DeclaresCustomPermissionExactSet                           bool
	RequiresProcessNodeRecordMutationPolicy                    bool
	RequiresIdentitySyncSettingRecordMutationPolicy            bool
	RequiresIdentitySyncDeactivationTicketRecordMutationPolicy bool
	UsesVirtualFieldHistoryReportSources                       bool
	UsesVirtualOpportunityHistoryReportSource                  bool
}

var salesforceCRMStandardPackSourceContracts = map[string]SalesforceCRMStandardPackSourceContract{
	"d05e54b587602050e48b1976c769edeb75eb98f8": {DeclaresCustomPermissionExactSet: true, RequiresProcessNodeRecordMutationPolicy: true, RequiresIdentitySyncSettingRecordMutationPolicy: true, RequiresIdentitySyncDeactivationTicketRecordMutationPolicy: true, UsesVirtualFieldHistoryReportSources: true, UsesVirtualOpportunityHistoryReportSource: true},
	"a4d6c59be56fa8357848412e5792003ef790db09": {DeclaresCustomPermissionExactSet: true, RequiresProcessNodeRecordMutationPolicy: true, RequiresIdentitySyncSettingRecordMutationPolicy: true, RequiresIdentitySyncDeactivationTicketRecordMutationPolicy: true, UsesVirtualFieldHistoryReportSources: true, UsesVirtualOpportunityHistoryReportSource: true},
	"6f82feb41d0402151ea78af67ba2a98951d565fb": {DeclaresCustomPermissionExactSet: true, RequiresProcessNodeRecordMutationPolicy: true, RequiresIdentitySyncSettingRecordMutationPolicy: true, UsesVirtualFieldHistoryReportSources: true, UsesVirtualOpportunityHistoryReportSource: true},
	"fc272da8700b0c2689e75788b513ccfb843f9ffc": {DeclaresCustomPermissionExactSet: true, RequiresProcessNodeRecordMutationPolicy: true, RequiresIdentitySyncSettingRecordMutationPolicy: true, UsesVirtualFieldHistoryReportSources: true, UsesVirtualOpportunityHistoryReportSource: true},
	"fa7c3e05aeded37ecaab1de97b065f7f39887562": {RequiresProcessNodeRecordMutationPolicy: true, RequiresIdentitySyncSettingRecordMutationPolicy: true, UsesVirtualFieldHistoryReportSources: true, UsesVirtualOpportunityHistoryReportSource: true},
	"2425c00c77e968b0769d6bd247b59831a17de894": {RequiresProcessNodeRecordMutationPolicy: true, RequiresIdentitySyncSettingRecordMutationPolicy: true, UsesVirtualFieldHistoryReportSources: true, UsesVirtualOpportunityHistoryReportSource: true},
	"d4b83bd65680c675a0fc24b0baf72996555240f4": {RequiresProcessNodeRecordMutationPolicy: true, RequiresIdentitySyncSettingRecordMutationPolicy: true, UsesVirtualFieldHistoryReportSources: true, UsesVirtualOpportunityHistoryReportSource: true},
	"fb8c8f4954a51498294a0f2062b14f096efa0fda": {RequiresProcessNodeRecordMutationPolicy: true, RequiresIdentitySyncSettingRecordMutationPolicy: true, UsesVirtualFieldHistoryReportSources: true, UsesVirtualOpportunityHistoryReportSource: true},
	"1ad078b876614e6d1ff9472035b11ae3cc5c1ac1": {RequiresProcessNodeRecordMutationPolicy: true, UsesVirtualFieldHistoryReportSources: true, UsesVirtualOpportunityHistoryReportSource: true},
	"481cf292ac3fb353834014f7c307cfabcb4ffd6d": {RequiresProcessNodeRecordMutationPolicy: true, UsesVirtualFieldHistoryReportSources: true, UsesVirtualOpportunityHistoryReportSource: true},
}

// LookupSalesforceCRMStandardPackSourceContract resolves a known source payload
// contract. The distribution lock separately decides whether that contract is
// bootable; historical contracts remain readable only so a stopped upgrade can
// diagnose and migrate them without inheriting current payload assumptions.
func LookupSalesforceCRMStandardPackSourceContract(revision string) (SalesforceCRMStandardPackSourceContract, bool) {
	contract, ok := salesforceCRMStandardPackSourceContracts[strings.ToLower(strings.TrimSpace(revision))]
	return contract, ok
}

// FirstPartyProductPackNames returns the current first-party product identities.
// Frontend source alignment and other positive product inventories consume this
// list, so retired identities must not be added here.
func FirstPartyProductPackNames() []string {
	return []string{
		SalesforceCRMStandardPackName,
	}
}

// RetiredFirstPartyProductPackNames returns negative composition tombstones for
// historical product identities. Product distributions add these names only to
// KnownProductAppPacks so an obsolete active installed row fails closed; the
// names do not imply a current product, frontend, source, or runtime surface.
func RetiredFirstPartyProductPackNames() []string {
	return []string{
		RetiredIssueTrackerStandardPackName,
	}
}
