provider "snowflake" {
  account_name      = "delphi"
  organization_name = "sme"
  role              = "PROD_ATLANTIS"
  warehouse         = "DEV_OWS_WAREHOUSE"
  preview_features_enabled = [
    "snowflake_stage_resource",
    "snowflake_storage_integration_resource",
  ]
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/snowflake/delphi/storage_integrations/prod-orcdbucket-feed-prod-sony-internal-stars/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

resource "snowflake_storage_integration" "integrations_snowflake_integration" {
  name             = var.snowflake_storage_integration_name
  storage_provider = "S3"

  storage_allowed_locations = var.snowflake_storage_allowed_locations

  storage_aws_role_arn = var.snowflake_storage_aws_role_arn
}

// Create QA stage for the Snowflake storage integration
resource "snowflake_stage" "integrations_stage_qa" {
  name                = var.snowflake_stage_name
  url                 = var.snowflake_stage_url
  database            = var.snowflake_stage_database
  schema              = var.snowflake_stage_qa_schema
  storage_integration = snowflake_storage_integration.integrations_snowflake_integration.name
}

module "integrations_stage_access_qa" {
  source = "git@github.com:theorchard/terraform-snowflake.git//modules/roles/stage_access?ref=7.3.1"

  database = snowflake_stage.integrations_stage_qa.database
  schema   = snowflake_stage.integrations_stage_qa.schema
  stage    = snowflake_stage.integrations_stage_qa.name
}

// Create PROD stage for the Snowflake storage integration
resource "snowflake_stage" "integrations_stage_prod" {
  name                = var.snowflake_stage_name
  url                 = var.snowflake_stage_url
  database            = var.snowflake_stage_database
  schema              = var.snowflake_stage_prod_schema
  storage_integration = snowflake_storage_integration.integrations_snowflake_integration.name
}

module "integrations_stage_access_prod" {
  source = "git@github.com:theorchard/terraform-snowflake.git//modules/roles/stage_access?ref=7.3.1"

  database = snowflake_stage.integrations_stage_prod.database
  schema   = snowflake_stage.integrations_stage_prod.schema
  stage    = snowflake_stage.integrations_stage_prod.name
}
