Fallacy - where is the mistake? Could anyone help me to find the mistake in this fallacy? Because the actual result for $I$ is $\pi/2$

\begin{equation}
I = \int_{0}^{\pi} \cos^{2} x \; \textrm{d}x
\end{equation}

\begin{equation}
I = \int_{0}^{\pi} \cos x \cos x \; \textrm{d}x
\end{equation}
substitution:
\begin{equation}
\sin x = u
\end{equation}

\begin{equation}
\cos x \; \textrm{d}x = u
\end{equation}

\begin{equation}
\cos x = \cos (\arcsin x)
\end{equation}
and the limits:
\begin{align}
\begin{split}
x &amp;= 0 \quad \Rightarrow \quad u=0 \\
x &amp;= \pi \quad \Rightarrow \quad u=0
\end{split}
\end{align}
so
\begin{equation} 
I = \int_{0}^{0} \cos (\arcsin u) \textrm{d}u
\end{equation}

\begin{equation}
I=0
\end{equation}
The problem is in using $\cos x = \cos \sin^{-1} x$. Arcsine is not a single valued function on $x=[0,\pi]$. In the conventional sense, it's not defined outside of $[-\pi/2,\pi/2]$. You can only do a $u$ substitution using a one-to-one function.
if $\sin(x)=u$ we get $x=\arcsin(u)$ and $dx=\frac{1}{\sqrt{1-u^2}}du$ thus we get
$\int \cos(x)^2dx=\int 1-\sin(x)^2dx=\int \sqrt{1-u^2}du$