The present paper leverages artificial intelligence algorithms to optimize and tune the parameters of PI controllers to enhance the performance of a PV STATCOM in terms of both dynamic and static responses. The proposed approach's effectiveness has been validated using a distribution grid model feeding capacitive and inductive loads at the point of common coupling (PCC) and controlled by three PI regulators. Comparative analysis between the traditional PI controllers and the refined parameters through Particle Swarm Optimization (PSO) and genetic algorithm (GA) reveals that this method substantially enhances both the static and dynamic performance characteristics of the whole system and allows providing dynamic grid support in faulty conditions. In addition, it ensures power factor correction while injecting active power. The study is performed and tested under technical Moroccan regulations. The STATCOM is implemented between a PV array module and the PCC to the distribution network, which is typical of a standard structure in the presence of renewable generators. The PV STATCOM is not yet used in current Moroccan electrical network context and the proposed system effectively manages network variability and grid disturbances, notably enhancing power quality at the (PCC). It dynamically adjusts to supply or absorb reactive power as needed, all within an acceptable reversing time, ensuring stability and resilience in the network. MATLAB Simulink software is used to conduct simulation, which provides the best outcomes while complying to the technical constraints set by local regulations.