terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>3.0"
    }
  }
  backend "azurerm" {
    resource_group_name  = "terraform_state_rg"
    storage_account_name = "6lj6vpk4wqoqaifgbgtgrdzd"
    container_name       = "terraform-state"
    key                  = "azure-container-registry/terraform.tfstate"
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg" {
  name     = "container_registry_rg"
  location = "eastus2"
}

resource "azurerm_container_registry" "container_registry" {
  name                = "tidbcloudprod"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  sku                 = "Basic"
  admin_enabled       = false
}

resource "azurerm_user_assigned_identity" "acr_manage_identity" {
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  name                = "acr_manage_identity"
}

# resource "azurerm_role_assignment" "acr_role_assignment" {
#   scope                = azurerm_container_registry.container_registry.id
#   role_definition_name = "AcrPull" # Or "AcrPush" depending on your needs
#   principal_id         = azurerm_user_assigned_identity.acr_manage_identity.principal_id
# }


# resource "azurerm_role_assignment" "acr_role_assignment" {
#   principal_id         = azurerm_user_assigned_identity.container_registry.principal_id
#   role_definition_name = "Storage Blob Data Contributor"
#   scope                = azurerm_storage_account.storage_account.id
# }


output "login_server" {
  value = azurerm_container_registry.container_registry.login_server
}

