Gas flaring and produced water (PW) emissions are common environmental challenges with significant impacts on the oil production industry. This study proposes a novel approach to address these concerns by developing energy self-sufficient networks for simultaneous utilization of flare gas (FG) and PW for sustainable hydrogen production. Three distinct scenarios are analyzed: hydrogen production without CO2 capture (Hydra), fossil fuel-based (FF) hydrogen production with CO2 capture and utilization in enhanced oil recovery (HydraCap-FF), and a solar-thermal-assisted HydraCap-FF system (HydraCap-RE). Techno-economic and environmental models are constructed for each scenario's optimal configuration, enabling a comparative analysis of their economic viability and environmental impact. Additionally, the designed systems are examined through a flexibility analysis to evaluate input materials and their influence on the system's viability. Results highlight the promising potential of HydraCap-RE, achieving significant reductions in CO2 emissions and water footprint compared to traditional methods. The HydraCap-RE generates hydrogen with the cost of 2.86 $/kg H2, hydrogen yield of 31.25 kg H2/100 kg FG, specific emission of 0.86 kg CO2/kg H2 and water footprint of 0.185 kg H2O/kg H2. Moreover, the flexibility analysis indicates that an optimal FG/PW ratio of 0.53 yields the highest economic value. The results demonstrate that deploying HydraCap-FF in the Permian, Bakken, and Eagle Ford shale regions can produce 0.807, 0.454, and 0.121 million tons of hydrogen annually, respectively, and reduce CO2 emissions by 6.37, 3.58, and 0.956 million tons, respectively. Furthermore, the HydraCap-RE deployment in these shale fields outperformed, with 17 % higher hydrogen production and 28.42 % lower CO2 emissions. These findings provide valuable insights for decision-makers seeking to reduce CO2 emissions the oil and gas industries and providing clean and sustainable products for these areas.