########################################
# Development Caddyfile for Sendico
########################################
{
  # No ACME email needed for local dev
  auto_https off
}

:80 {
  vars static_root /usr/share/pweb

  encode zstd gzip

  route {
    ########################################
    # Backend API - proxy to dev-bff
    ########################################
    handle /api/v1/* {
      reverse_proxy dev-bff:8080 {
        health_uri /api/v1/health
        health_interval 15s
        health_timeout 4s
        health_status 2xx
      }
      header Cache-Control "no-cache, no-store, must-revalidate"
    }

    # Telegram webhook -> notification service
    handle /telegram/webhook {
      reverse_proxy dev-notification:8081
    }

    # Monetix callbacks -> mntx gateway
    handle /gateway/m/* {
      rewrite * /monetix/callback
      reverse_proxy dev-mntx-gateway:8084
      header Cache-Control "no-cache, no-store, must-revalidate"
    }

    ########################################
    # Static assets
    ########################################
    handle /version.json {
      root * {vars.static_root}
      file_server
      header Cache-Control "no-cache"
    }

    handle /*.js {
      root * {vars.static_root}
      file_server
      header Cache-Control "public, max-age=3600"
    }

    handle /assets/* {
      root * {vars.static_root}
      file_server
      header Cache-Control "public, max-age=86400"
    }

    handle /canvaskit/* {
      root * {vars.static_root}
      file_server
      header Cache-Control "public, max-age=86400"
    }

    handle /icons/* {
      root * {vars.static_root}
      file_server
      header Cache-Control "public, max-age=86400"
    }

    handle /*.html {
      root * {vars.static_root}
      file_server
      header Cache-Control "no-cache"
    }

    handle /*.css {
      root * {vars.static_root}
      file_server
      header Cache-Control "public, max-age=3600"
    }

    handle /*.json {
      root * {vars.static_root}
      file_server
      header Cache-Control "public, max-age=3600"
    }

    ########################################
    # SPA fallback - serves index.html for all routes
    ########################################
    handle {
      root * {vars.static_root}
      try_files {path} /index.html
      file_server
      header Cache-Control "no-cache"
    }
  }
}
