locals {
  allowed_code_owner_teams = [
    "abacus",
    "accounts-identities-platform-aka-user-platform",
    "application-security",
    "business-solutions",
    "coda",
    "collaborators",
    "collections-fingerprinting",
    "collections-ownership",
    "collections-publishing-and-compositions",
    "content-creation-management-ccm",
    "content-delivery-asset-management-cdam",
    "content-review-management",
    "customer-accounting",
    "d2c",
    "data-platform",
    "devops-squad",
    "digital-exchange-dx",
    "digital-systems-dra",
    "fansifter",
    "insights",
    "integrations",
    "kafka-data-highway-kdh",
    "mobile-suite",
    "orchard-suite-platform-osp",
    "permissions-platform-pp",
    "podcast",
    "qa-automation-team",
    "royaltyshare",
    "royalties",
    "salesforce-business-systems",
    "salesforce-set",
    "songwhip",
    "sony-music-fans",
    "tax-and-payments",
    "transfer-product-ownership",
    "sme-ds-team"
  ]
}

# General variables
variable "repository_name" {
  type        = string
  description = "Name of the repository to create"
}

variable "description" {
  type        = string
  description = "Description of the repository"
}

variable "default_code_owner" {
  type        = string
  description = "The default owner for populating the initial CODEOWNERS file. This must be the slug of an existing GitHub team."

  validation {
    condition     = contains(local.allowed_code_owner_teams, var.default_code_owner)
    error_message = "Default code owner must be one of the following valid squad team slugs: ${join(", ", local.allowed_code_owner_teams)}."
  }
}

variable "branch_protection_enabled" {
  type        = bool
  description = "Whether to enable branch protection for the repository."
  default     = true
}

variable "branch_protection_enforce_admins" {
  type        = bool
  description = "Whether to enforce branch protection for repository admins."
  default     = true
}

variable "archived_repository" {
  type        = bool
  description = "Whether the repository is archived."
  default     = false
}

variable "homepage_url" {
  type        = string
  description = "Homepage URL of the repository. Defaults to the GitHub repository URL."
  default     = null
}

variable "has_downloads" {
  type        = bool
  description = "Whether the repository has downloads enabled."
  default     = false
}

variable "branch_protection_patterns" {
  type        = set(string)
  description = "A list of branch name patterns to apply branch protection rules to. If not specified, branch rules will be applied to the default branch."
  default     = null
}

# Access settings
variable "github_pull_teams" {
  type        = list(string)
  description = "A list containing teams with pull access. Specify teams with lower case."
  default     = []
}

variable "github_triage_teams" {
  type        = list(string)
  description = "A list containing teams with triage access. Specify teams with lower case."
  default = [
    "orcharddevs-ro"
  ]
}

variable "github_push_teams" {
  type        = list(any)
  description = "A list containing teams with push access. Specify teams with lower case."
  default = [
    "mergers",
  ]
}

variable "github_repository_pull_collaborators" {
  type        = list(string)
  description = "List of users with pull access. Should only be used for external collaborators - use teams for internal users."
  default     = []
}

variable "github_repository_push_collaborators" {
  type        = list(string)
  description = "List of users with push access. Should only be used for external collaborators - use teams for internal users."
  default     = []
}

variable "github_code_owner_reviews" {
  type        = bool
  description = "Whether or not to require code owners to review PRs"
  default     = false
}

# merge settings
variable "allow_merge_commit" {
  type        = bool
  description = "(Optional) Set to false to disable merge commits on the repository."
  default     = false
}

variable "allow_squash_merge" {
  type        = bool
  description = "(Optional) Set to false to disable squash merges on the repository."
  default     = true
}

variable "allow_rebase_merge" {
  type        = bool
  description = "(Optional) Set to false to disable rebase merges on the repository."
  default     = false
}

# pull request review settings
variable "dismiss_stale_reviews" {
  type        = bool
  description = "(Optional) Dismiss approved reviews automatically when a new commit is pushed."
  default     = true
}

variable "restrict_dismissals" {
  type        = bool
  description = "(Optional) Restrict pull request review dismissals."
  default     = true
}

variable "pull_request_bypassers" {
  type        = list(string)
  description = "(Optional) list of users who can bypass pull request review."
  default     = []
}

variable "dismissal_restriction_users" {
  type        = list(string)
  description = "(Optional) list of users who can dismiss pull request reviews."
  default     = []
}

variable "application_family" {
  type        = string
  description = "The application family"
}

variable "allows_force_pushes" {
  type    = bool
  default = false
}

variable "required_approving_review_count" {
  type        = number
  description = "The number of approving reviews required for a PR to be merged"
  default     = null
}

variable "delete_branch_on_merge" {
  type        = bool
  default     = true
  description = "Delete branches by default after a pull request is merged"
}

variable "require_last_push_approval" {
  type        = bool
  default     = true
  description = "Require the last push to be approved by someone other than the last pusher. Prevents users from pushing changes to someone else's PR and approving their own changes."
}

variable "require_branches_to_be_up_to_date" {
  type        = bool
  default     = true
  description = "Require branches to be up to date before merging. Note this only takes effect if at least one required status check is defined."
}

variable "required_status_checks" {
  type = list(string)
  default = [
    "Jenkins"
  ]
  description = <<EOF
    List of status checks that are required to pass in order to merge into protected branches.
    When this is non-empty, this will also enforce that branches are up to date before merging.
EOF
}

variable "default_branch_name" {
  type        = string
  description = "Default branch name"
  default     = "master"
}

variable "autolink_references" {
  type = list(object({
    key_prefix          = string
    target_url_template = string
    is_alphanumeric     = optional(bool, true)
  }))
  description = <<EOF
    List of autolink references to create for the repository. Each autolink reference automatically
    links references like 'SYS-123' in PR titles and descriptions to external URLs (e.g., Jira tickets).

    - key_prefix: The prefix to match (e.g., "SYS-" for Jira project SYS)
    - target_url_template: The URL template with <num> placeholder (e.g., "https://jira.example.com/browse/SYS-<num>")
    - is_alphanumeric: If true, matches alphanumeric characters after prefix. If false, matches only numeric characters.
EOF
  default     = []
}
