The production of prompt D\(^{0}\), D\(^{+}\), and D\(^{*+}\) mesons was measured at midrapidity (|y|< 0.5) in Pb-Pb collisions at the centre-of-mass energy per nucleon-nucleon pair \(\sqrt{s_{\rm NN}}\) = 5.02 TeV with the ALICE detector at the LHC. The D mesons were reconstructed via their hadronic decay channels and their production yields were measured in central (0-10%) and semicentral (30-50%) collisions. The measurement was performed up to a transverse momentum (\(p_{\rm T}\)) of 36 or 50 GeV/\(c\) depending on the D meson species and the centrality interval. For the first time in Pb-Pb collisions at the LHC, the yield of D\(^0\) mesons was measured down to \(p_{\rm T}\) = 0, which allowed a model-independent determination of the \(p_{\rm T}\)-integrated yield per unit of rapidity (d\(N\)=d\(y\)). A maximum suppression by a factor 5 and 2.5 was observed with the nuclear modification factor (\(R_{\rm AA}\)) of prompt D mesons at \(p_{\rm T}\) = 6-8 GeV/\(c\) for the 0-10% and 30-50% centrality classes, respectively. The D-meson \(R_{\rm AA}\) is compared with that of charged pions, charged hadrons, and J/\(\psi\) mesons as well as with theoretical predictions. The analysis of the agreement between the measured \(R_{\rm AA}\), elliptic (\(v_2\)) and triangular (\(v_3\)) flow, and the model predictions allowed us to constrain the charm spatial diffusion coefficient \(D_s\). Furthermore the comparison of \(R_{\rm AA}\) and \(v_2\) with different implementations of the same models provides an important insight into the role of radiative energy loss as well as charm quark recombination in the hadronisation mechanisms.