locals {
  vps_1 = {
    id   = data.terraform_remote_state.contabo.outputs.instances[0].id
    name = data.terraform_remote_state.contabo.outputs.instances[0].name
    ip   = data.terraform_remote_state.contabo.outputs.instances[0].ip_config[0].v4[0].ip
  }

  default_ttl = "3600"

  domains = {
    "a13labs.me" = [
      {
        type  = "A"
        value = local.vps_1.ip
      },
      {
        type     = "MX"
        priority = "10"
        value    = "mail-pt.securemail.pro"
      },
      {
        type  = "TXT"
        value = "v=spf1 include:_spf.tem.scaleway.com include:spf.webapps.net ~all"
      },
      {
        name  = "imap"
        type  = "CNAME"
        value = "${local.vps_1.name}.a13labs.me"
      },
      {
        name  = "mail"
        type  = "CNAME"
        value = "mail-pt.securemail.pro"
      },
      {
        name  = "smtp"
        type  = "CNAME"
        value = "${local.vps_1.name}.a13labs.me"
      },
      {
        name  = "_dmarc"
        type  = "TXT"
        value = "v=DMARC1; p=none; rua=mailto:admin@a13labs.me"
      },
      {
        name  = local.vps_1.name
        type  = "A"
        value = local.vps_1.ip
      },
      {
        name     = "_imap._tcp"
        type     = "SRV"
        priority = "0"
        weight   = "1"
        port     = "993"
        value    = "imap.a13labs.me"
      },
      {
        name     = "_smtp._tcp"
        type     = "SRV"
        priority = "0"
        weight   = "1"
        port     = "465"
        value    = "smtp.a13labs.me"
      },
      {
        name  = "${var.scaleway_dkim_key}._domainkey"
        type  = "TXT"
        value = "v=DKIM1; h=sha256; k=rsa; p=${var.a13labs_me_scaleway_dkim_value}"
      },
    ],
    "alexpires.me" = [
      {
        type  = "A"
        value = local.vps_1.ip
      },
      {
        name     = ""
        type     = "MX"
        priority = "10"
        value    = "mail-pt.securemail.pro"
      },
      {
        type  = "TXT"
        value = "v=spf1 include:_spf.tem.scaleway.com include:spf.webapps.net ~all"
      },
      {
        name  = "tv"
        type  = "CNAME"
        value = "${local.vps_1.name}.alexpires.me"
      },
      {
        name  = "www"
        type  = "CNAME"
        value = "${local.vps_1.name}.alexpires.me"
      },
      {
        name  = "blog"
        type  = "CNAME"
        value = "${local.vps_1.name}.alexpires.me"
      },
      {
        name  = "code"
        type  = "CNAME"
        value = "${local.vps_1.name}.alexpires.me"
      },
      {
        name  = "imap"
        type  = "CNAME"
        value = "${local.vps_1.name}.alexpires.me"
      },
      {
        name  = "mail"
        type  = "CNAME"
        value = "mail-pt.securemail.pro"
      },
      {
        name  = "smtp"
        type  = "CNAME"
        value = "${local.vps_1.name}.alexpires.me"
      },
      {
        name  = "cloud"
        type  = "CNAME"
        value = "${local.vps_1.name}.alexpires.me"
      },
      {
        name  = "_dmarc"
        type  = "TXT"
        value = "v=DMARC1; p=none; rua=mailto:admin@alexpires.me"
      },
      {
        name  = "prod-01"
        type  = "CNAME"
        value = "${local.vps_1.name}.alexpires.me"
      },
      {
        name  = "_keybase"
        type  = "TXT"
        value = "keybase-site-verification=${var.keybase_site_verification}"
      },
      {
        name  = "rustdesk"
        type  = "CNAME"
        value = "${local.vps_1.name}.alexpires.me"
      },
      {
        name  = local.vps_1.name
        type  = "A"
        value = local.vps_1.ip
      },
      {
        name     = "_imap._tcp"
        type     = "SRV"
        priority = "0"
        weight   = "1"
        port     = "993"
        value    = "imap.alexpires.me"
      },
      {
        name     = "_smtp._tcp"
        type     = "SRV"
        priority = "0"
        weight   = "1"
        port     = "465"
        value    = "smtp.alexpires.me"
      },
      {
        name  = "${var.scaleway_dkim_key}._domainkey"
        type  = "TXT"
        value = "v=DKIM1; h=sha256; k=rsa; p=${var.alexpires_me_scaleway_dkim_value}"
      },
      {
        name  = "webdav"
        type  = "CNAME"
        value = "${local.vps_1.name}.alexpires.me"
      },
    ]
  }

  records = flatten([for domain, records in local.domains : [
    for record in records : {
      name     = lookup(record, "name", "")
      zone     = domain
      type     = record.type
      value    = record.value
      priority = lookup(record, "priority", null)
      weight   = lookup(record, "weight", null)
      port     = lookup(record, "port", null)
    }
  ]])
}

data "terraform_remote_state" "contabo" {
  backend = "s3"
  config = {
    bucket = "a13labs.infra.eu"
    key    = "contabo.core.infra.tfstate"
    region = "eu-west-1"
  }
}

