Evaluating complex integral $\int_{0}^{\pi} \frac {x \sin x}{1+a^2-2a(\cos x)} $ via different contour
We assume $a \gt 1$ for simplicity. Evaluation of this integral is greatly simplified by using the fact that
$$\frac{\sin{x}}{1+a^2-2 a \cos{x}} = \operatorname{Im}{\left (\frac1{a-e^{i x}} \right )}$$
Thus the integral of interest is
$$\frac12 \operatorname{Im} \int_{-\pi}^{\pi} dx \, \frac{x}{a-e^{i x}} $$
We may use Cauchy's theorem to evaluate the integral. To wit, consider the complex integral:
$$\oint_{C} dz \,\frac{\log{z}}{z (z-a)}$$
where $C$ is the following contour:
where the outer circle is the unit circle, the inner circle has a radius $\epsilon$ that will approach zero. There is a branch cut along the negative real axis - negative because the integral is over $[-\pi,\pi]$.
We may write the above complex integral as a sum of integrals as we parametrize the various pieces of the contour $C$:
$$\begin{align}\oint_{C} dz \,\frac{\log{z}}{z (z-a)} &= \int_{-\pi}^{\pi} dx \, \frac{x}{a-e^{i x}} + e^{i \pi} \int_{1}^{\epsilon} dx \, \frac{\log{x}+i \pi}{x (x+a)} \\ &+ i \epsilon \int_{\pi}^{-\pi} d\phi \, e^{i \phi} \, \frac{\log{\epsilon}+i \phi}{\left ( \epsilon e^{i \phi} \right ) \left ( \epsilon e^{i \phi} -a \right )} + e^{-i \pi} \int_{\epsilon}^1 dx \, \frac{\log{x}-i \pi}{x (x+a)} \end{align}$$
By Cauchy's theorem, the complex integral is zero. We also consider the limit as $\epsilon \to 0$. In this limit, the third integral (about the small circle) behaves as $i (2 \pi/a) \log{\epsilon}$, and we may write
$$\begin{align}\int_{-\pi}^{\pi} dx \, \frac{x}{a-e^{i x}} &= -i \frac{2 \pi}{a} \log{\epsilon} - i 2 \pi \int_{\epsilon}^1 dx \, \frac{dx}{x (x+a)} \\ &= -i \frac{2 \pi}{a} \log{\epsilon} - i \frac{2 \pi}{a} \int_{\epsilon}^{1} \frac{dx}{x} + i \frac{2 \pi}{a} \int_{\epsilon}^{1} \frac{dx}{x+a}\\ &= i \frac{2 \pi}{a} \log{\left ( \frac{1+a}{a} \right )}\end{align}$$
Note that the singular pieces in $\epsilon$ canceled. With this, we may reconstruct the original integral using the pieces at the top. The result is, for $a \gt 1$,
$$ \int_0^{\pi} dx \, \frac{x \sin{x}}{1+a^2-2 a \cos{x}} = \frac{\pi}{a} \log{\left ( \frac{1+a}{a} \right )} $$
I leave as an exercise for the reader what the value of the integral is when $|a| \lt 1$ or when $a \in \mathbb{C}$.