terraform {
  required_providers {
    cloudflare = {
      source  = "cloudflare/cloudflare"
      version = "5.8.2"
    }
  }
}

provider "cloudflare" {
  api_token = var.cloudflare_apitoken
}
locals {
  dream_records = {
    dream_mail = {
      name    = "mail"
      content = "168.119.13.219"
      type    = "A"
      ttl     = 86400
    }
    dream_mx = {
      name     = "@"
      content  = "mail.dreamartdecor.com"
      type     = "MX"
      ttl      = 86400
      priority = 10
    }
    dream_autoconfig = {
      name    = "autoconfig"
      content = "mail.dreamartdecor.com"
      type    = "CNAME"
      ttl     = 86400
    }
    dream_mail_aaaa = {
      name    = "mail"
      content = "2a01:4f8:242:4460::2"
      type    = "AAAA"
      ttl     = 86400
    }
    dream_txt_spf = {
      name    = "@"
      content = "v=spf1 a mx ip4:168.119.13.219 ip6:2a01:4f8:242:4460::2 ~all"
      type    = "TXT"
      ttl     = 86400
    }
    dream_txt_dkim = {
      name    = "x._domainkey"
      content = "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1GW9inpeYtcxMWY3JjUnVFzsPgKCZOfOCETVvk5wWOYZr9LJGz0YnJu3xGIZeJiFWDOgGV/xorzlcAzDqumh58cYPkDIzYVgbOp8vw1qS+a3iKMtRM99kyadEmUDyKjHk11HiCADNaEAgCD1vaKlQzRGAmdP15XhFC7xprSPQPAi6z/l2Iy3wsLdMpYR9P+tiSpS0msI86PBj4Kj5JRuzyHMw4YCqLRKMOIXTKO/zBWOAJOc/eKbjMyTT/iUJe9YE5yuzUHSZNT57aTHIGGadhFhMrkCNVFMyuCGZFt7fCF+Xzvu0iljYK/Uw4Zru73fTaUtq8SMcnvLjj7lm0fpvwIDAQAB"
      type    = "TXT"
      ttl     = 86400
    }
    dream_dmarc = {
      name    = "_dmarc"
      content = "v=DMARC1;  p=none; rua=mailto:4a937e10a8e144c89cb11f1272c159c0@dmarc-reports.cloudflare.net"
      type    = "TXT"
      ttl     = 86400
    }
    dream_www = {
      name    = "www"
      content = "dreamartdecor.com"
      type    = "CNAME"
      ttl     = 1
      proxied = true
    }
  }
}
resource "cloudflare_dns_record" "this" {
  for_each = local.dream_records

  zone_id = var.zone_id
  name    = each.value.name
  content = each.value.content
  type    = each.value.type
  ttl     = each.value.ttl

  proxied  = lookup(each.value, "proxied", false)
  priority = lookup(each.value, "priority", null)
}