Elegant way to evaluate $\int_0^\pi\frac{\sin\left(M+\frac{1}{2}\right)\theta\;\sin\left(N+\frac{1}{2}\right)\theta}{\sin^2(\theta/2)}d\theta$?

Hint

$${\displaystyle {\begin{aligned}1+2\sum _{n=1}^{N}\cos(n\theta )&={\frac {\sin \left(\left(N+{\frac {1}{2}}\right)\theta \right)}{\sin \left({\frac {\theta }{2}}\right)}}\end{aligned}}}$$ and use the following fact $$\int_{0}^{\pi}\cos(n \theta)\cos(m \theta)d \theta=0$$ when $$m \ne n$$


$\newcommand{\bbx}[1]{\,\bbox[15px,border:1px groove navy]{\displaystyle{#1}}\,} \newcommand{\braces}[1]{\left\lbrace\,{#1}\,\right\rbrace} \newcommand{\bracks}[1]{\left\lbrack\,{#1}\,\right\rbrack} \newcommand{\dd}{\mathrm{d}} \newcommand{\ds}[1]{\displaystyle{#1}} \newcommand{\expo}[1]{\,\mathrm{e}^{#1}\,} \newcommand{\ic}{\mathrm{i}} \newcommand{\mc}[1]{\mathcal{#1}} \newcommand{\mrm}[1]{\mathrm{#1}} \newcommand{\pars}[1]{\left(\,{#1}\,\right)} \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}} \newcommand{\root}[2][]{\,\sqrt[#1]{\,{#2}\,}\,} \newcommand{\totald}[3][]{\frac{\mathrm{d}^{#1} #2}{\mathrm{d} #3^{#1}}} \newcommand{\verts}[1]{\left\vert\,{#1}\,\right\vert}$ \begin{align} &\bbox[10px,#ffd]{\left.\vphantom{\Large A}\mrm{I}\pars{M,N}\, \right\vert_{\ M, N\ \in\ \mathbb{N}_{\large\ \geq 0}}} \equiv \int_{0}^{\pi}{\sin\pars{\bracks{M + 1/2}\theta} \sin\pars{\bracks{N + 1/2}\theta} \over \sin^{2}\pars{\theta/2}}\,\dd\theta \\[5mm] = &\ {1 \over 2}\int_{0}^{\pi}{\cos\pars{\bracks{M - N}\theta} -\cos\pars{\bracks{M + N + 1}\theta} \over \sin^{2}\pars{\theta/2}}\,\dd\theta \\[5mm] = &\ {1 \over 2}\int_{0}^{\pi}{1 - \cos\pars{\bracks{M + N + 1}\theta} \over \sin^{2}\pars{\theta/2}}\,\dd\theta - {1 \over 2}\int_{0}^{\pi}{1 - \cos\pars{\verts{M - N}\theta} \over \sin^{2}\pars{\theta/2}}\,\dd\theta \\[5mm] = &\ \bbox[10px,#ffd]{\mc{J}\pars{M + N + 1} - \mc{J}\pars{\verts{M - N}}} \label{1}\tag{1} \\[5mm] &\ \mbox{where}\quad \left\{\begin{array}{rcl} \ds{\left.\vphantom{\Large A}\mc{J}\pars{a} \,\right\vert_{\ a\ \in\ \mathbb{N}_{\ \geq\ 0}}} & \ds{\equiv} & \ds{\int_{0}^{\pi/2} {1 - \cos\pars{2a\theta} \over \sin^{2}\pars{\theta}}\,\dd\theta} \\[2mm] & \ds{=} & \ds{\Re\int_{0}^{\pi/2} {1 + 2\ic a\theta - \expo{2\ic a\theta} \over \sin^{2}\pars{\theta}}\,\dd\theta} \end{array}\right. \end{align}

Lets evaluate $\ds{\mrm{J}\pars{a}}$:

\begin{align} \mc{J}\pars{a} & = \Re\int_{0}^{\pi/2} {1 + 2\ic a\theta - \expo{2\ic a\theta} \over \sin^{2}\pars{\theta}}\,\dd\theta \\[5mm] & = \left.\Re\int_{\theta\ =\ 0}^{\theta\ =\ \pi/2} {1 + 2\ic a\bracks{-\ic\ln\pars{z}} - z^{2a} \over -\pars{1 - z^{2}}^{2}/\pars{4z^{2}}}\,{\dd z \over \ic z} \,\right\vert_{\ z\ =\ \exp\pars{\ic\theta}} \\[5mm] & = \left.4\,\Im\int_{\theta\ =\ 0}^{\theta\ =\ \pi/2} {z^{2a} - 2a\ln\pars{z} - 1 \over \pars{1 - z^{2}}^{2}}\,z\,\dd z \,\right\vert_{\ z\ =\ \exp\pars{\ic\theta}} \\[5mm] & = -4\,\Im\int_{1}^{0} {y^{2a}\expo{\ic\pars{2a}\pi/2} - 2a\bracks{\ln\pars{y} + \ic\pi/2} - 1 \over \pars{1 + y^{2}}^{2}}\pars{\ic y}\,\ic\,\dd y \\[5mm] & = -4\int_{0}^{1} {y^{2a}\ \overbrace{\sin\pars{\pi a}}^{\ds{\color{red}{=\ 0}}}\ -\ \pi a \over \pars{1 + y^{2}}^{2}}\,y\,\dd y \\[5mm] & \stackrel{y^{2}\ \mapsto\ y}{=}\,\,\, 2\pi a\int_{0}^{1}{\dd y \over\pars{1 + y}^{2}} = \bbx{\pi a} \label{2}\tag{2} \end{align}

With \eqref{1} and \eqref{2}:

$$ \bbx{\mrm{I}\pars{M,N} = \pi\pars{M + N + 1 - \verts{M - N}}} $$