class TeamsApi
  URL = Rails.application.credentials.teams[:webhook_url]

  def self.send_message(message)
    TeamsApi.send_alert(message:)
  end

  def self.send_incident_alert(data)
    # Extract the incident hash (it's nested in multiple places, we'll use the top-level one)
    incident = data["incident"] || data["site"]["incident"]

    # Build formatted string with key information
    formatted = <<~TEXT
      Google Cloud Service Incident Report
      ======================================
      Incident ID: #{incident['incident_id']}
      Status: #{incident['state']}
      Summary: #{incident['summary']}

      Timeline:
      - Started: #{Time.at(incident['started_at']).strftime('%Y-%m-%d %H:%M:%S UTC')}

      Resource:
      - Type: #{incident['resource']['type']}
      - Name: #{incident['resource_display_name']}
      - ID: #{incident['resource_id']}

      Documentation:
      #{incident['documentation']}

      Example Domain Info:
      This domain is for use in illustrative examples in documents.
      May be used without prior coordination or permission.
    TEXT

    TeamsApi.send_alert(message: formatted)
  end

  def self.send_alert(message:)
    # Message payload
    payload = {
      text: message
    }

    # Send HTTP POST request
    response = HTTP.post(URL, json: payload)
  end
end
