terraform {
  required_providers {
    cloudflare = {
      source  = "cloudflare/cloudflare"
      version = "~> 5"
    }
  }
}

provider "cloudflare" {
  api_token = var.cloudflare_apitoken
}

locals {
  ztariq_records = {
    # --- A Records ---
    beszel = { name = "beszel", type = "A", content = "198.23.169.195", ttl = 1, proxied = true }
    ca     = { name = "ca", type = "A", content = "154.12.117.17", ttl = 1, proxied = false }
    nc     = { name = "nc", type = "A", content = "154.12.117.17", ttl = 1, proxied = false }
    nl     = { name = "nl", type = "A", content = "62.84.172.70", ttl = 1, proxied = false }
    reg    = { name = "reg", type = "A", content = "154.12.117.17", ttl = 1, proxied = false }
    tea    = { name = "tea", type = "A", content = "198.23.169.195", ttl = 1, proxied = false }
    uk     = { name = "uk", type = "A", content = "185.139.7.37", ttl = 1, proxied = false }
    uk2    = { name = "uk2", type = "A", content = "154.41.135.47", ttl = 1, proxied = false }
    uptime = { name = "uptime", type = "A", content = "198.23.169.195", ttl = 1, proxied = true }
    us     = { name = "us", type = "A", content = "198.23.169.195", ttl = 1, proxied = false }
    root_a = { name = "@", type = "A", content = "185.139.7.37", ttl = 1, proxied = true }
    # --- AAAA Records ---
    nl_aaaa      = { name = "nl", type = "AAAA", content = "2a12:bec4:1821:f0::a", ttl = 1, proxied = false }
    root_uk_aaaa = { name = "@", type = "AAAA", content = "2a12:ab46:5344:fd::a", ttl = 1, proxied = true }
    root_uk_uk   = { name = "uk", type = "AAAA", content = "2a12:ab46:5344:fd::a", ttl = 1, proxied = false }

    # --- CNAME Records ---
    autodiscover = { name = "autodiscover", type = "CNAME", content = "eu1.workspace.org.", ttl = 360, proxied = false }
    mail         = { name = "mail", type = "CNAME", content = "eu1.workspace.org.", ttl = 360, proxied = false }

    # --- MX Records ---
    mx_root = { name = "@", type = "MX", content = "eu1.workspace.org.", priority = 10, ttl = 360 }

    # --- TXT Records ---
    dmarc = {
      name    = "_dmarc"
      type    = "TXT"
      content = "v=DMARC1; p=quarantine; rua=mailto:postmaster@ztariq.com; ruf=mailto:postmaster@ztariq.com; fo=1; adkim=s; aspf=s"
      ttl     = 3600
    }
    dkim = {
      name    = "nd8ddf6995beebee4._domainkey"
      type    = "TXT"
      content = "v=DKIM1; k=rsa; h=sha256; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoi3yX1W5V6a9QbEXo00k9JCZ8Vew5rQEanHLIY0cOxCauAIZZIrhQsexZ0j45EFVtfMrBHeddUtolVSSDHvvJg49HzJqWsKOsN061uBgmdN69JEtzme04pRmz/7H+3Y0QDUSYDd+ffYzWaouplFqGuhYkQ5QG2J1JzofcetuAkQICIgWStcOO+av5WoyTdxfqsY64d/XFP4PZJJHX0XA1P2YaSuyNF5c7nv/+a9A6F5+OrgZhFNNWjUurkKKhFzhbR82BUPTXVuG3EI5wSQcIYjhXgINagsmvVyPL1XP584qtnq0ScGysSkh0T3Vhg/Kob9eHX1du7mZj7G0z3PHmwIDAQAB"
      ttl     = 360
    }
    workspace_verification = {
      name    = "workspace-verification"
      type    = "TXT"
      content = "f23716dd-2ad6-4dd4-8867-112e3c4c318d"
      ttl     = 360
    }
    spf = {
      name    = "@"
      type    = "TXT"
      content = "v=spf1 include:_spf.workspace.org -all"
      ttl     = 360
    }
  }
}

resource "cloudflare_dns_record" "ztariq" {
  for_each = local.ztariq_records

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

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

  lifecycle {
    prevent_destroy = false
  }
}
