File: goal.cpp

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (3633 lines) | stat: -rw-r--r-- 166,469 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Libdnf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libdnf.  If not, see <https://www.gnu.org/licenses/>.

#include "libdnf5/base/goal.hpp"

#include "advisory/advisory_package_private.hpp"
#include "base_private.hpp"
#ifdef WITH_MODULEMD
#include "module/module_goal_private.hpp"
#include "module/module_sack_impl.hpp"
#endif
#include "rpm/package_query_impl.hpp"
#include "rpm/package_sack_impl.hpp"
#include "rpm/package_set_impl.hpp"
#include "rpm/solv/goal_private.hpp"
#include "solv/id_queue.hpp"
#include "solv/pool.hpp"
#include "solver_problems_internal.hpp"
#include "transaction/transaction_merge.hpp"
#include "transaction/transaction_sr.hpp"
#include "transaction_impl.hpp"
#include "utils/string.hpp"
#include "utils/url.hpp"

#include "libdnf5/common/exception.hpp"
#include "libdnf5/comps/environment/query.hpp"
#include "libdnf5/comps/group/query.hpp"
#ifdef WITH_MODULEMD
#include "libdnf5/module/module_errors.hpp"
#endif
#include "libdnf5/rpm/package_query.hpp"
#include "libdnf5/rpm/reldep.hpp"
#include "libdnf5/utils/bgettext/bgettext-mark-domain.h"
#include "libdnf5/utils/fs/file.hpp"
#include "libdnf5/utils/patterns.hpp"

#include <fnmatch.h>

#include <filesystem>
#include <iostream>
#include <map>
#include <unordered_map>

namespace {

void add_obsoletes_to_data(const libdnf5::rpm::PackageQuery & base_query, libdnf5::rpm::PackageSet & data) {
    libdnf5::rpm::PackageQuery data_query(data);

    // In case there is an installed package in the `data` behave consistently
    // with upgrade and add all the obsoleters.
    libdnf5::rpm::PackageQuery installed_data(data_query);
    installed_data.filter_installed();

    if (installed_data.empty()) {
        // If there is no installed package in the `data`, add only obsoleters
        // of the latest versions.  This should prevent unexpected results in
        // case a package has multiple versions and some older version is being
        // obsoleted.
        // See also https://bugzilla.redhat.com/show_bug.cgi?id=2176263
        data_query.filter_priority();
        data_query.filter_latest_evr();
    }

    libdnf5::rpm::PackageQuery obsoletes_query(base_query);
    obsoletes_query.filter_obsoletes(data_query);
    data |= obsoletes_query;
}

/// Add install job of debug packages for installed packages to Goal
///
/// @return bool False when no match for any package
bool install_debug_from_packages(
    libdnf5::BaseWeakPtr base,
    std::string & debug_name,
    const std::vector<libdnf5::rpm::Package> & packages,
    libdnf5::solv::IdQueue & result_queue,
    libdnf5::rpm::solv::GoalPrivate & goal,
    bool skip_broken,
    bool best,
    bool clean_requirements_on_remove) {
    std::vector<std::string> nevras;
    for (const auto & package : packages) {
        std::string nevra(debug_name);
        nevra.append("-");
        nevra.append(package.get_epoch());
        nevra.append(":");
        nevra.append(package.get_version());
        nevra.append("-");
        nevra.append(package.get_release());
        nevra.append(".");
        nevra.append(package.get_arch());
        nevras.emplace_back(std::move(nevra));
    }
    libdnf5::rpm::PackageQuery query(base);
    query.filter_nevra(nevras);
    if (query.empty()) {
        return false;
    }
    libdnf5::solv::IdQueue install_queue;
    for (auto package : query) {
        Id id = package.get_id().id;
        install_queue.push_back(id);
        result_queue.push_back(id);
    }
    goal.add_install(install_queue, skip_broken, best, clean_requirements_on_remove);
    return true;
}


}  // namespace


namespace libdnf5 {

namespace {

inline bool name_arch_compare_lower_solvable(const Solvable * first, const Solvable * second) {
    if (first->name != second->name) {
        return first->name < second->name;
    }
    return first->arch < second->arch;
}


}  // namespace

using GroupSpec = std::tuple<GoalAction, libdnf5::transaction::TransactionItemReason, std::string, GoalJobSettings>;

class Goal::Impl {
public:
    Impl(const BaseWeakPtr & base);
    ~Impl();

    void add_rpm_ids(GoalAction action, const rpm::Package & rpm_package, const GoalJobSettings & settings);
    void add_rpm_ids(GoalAction action, const rpm::PackageSet & package_set, const GoalJobSettings & settings);

    GoalProblem add_specs_to_goal(base::Transaction & transaction);
    GoalProblem resolve_group_specs(std::vector<GroupSpec> & specs, base::Transaction & transaction);
    void add_resolved_group_specs_to_goal(base::Transaction & transaction);
    void add_resolved_environment_specs_to_goal(base::Transaction & transaction);
#ifdef WITH_MODULEMD
    GoalProblem add_module_specs_to_goal(base::Transaction & transaction);
#endif
    GoalProblem add_serialized_transaction_to_goal(base::Transaction & transaction);
    GoalProblem add_reason_change_specs_to_goal(base::Transaction & transaction);

    GoalProblem resolve_reverted_transactions(base::Transaction & transaction);
    GoalProblem resolve_redo_transaction(base::Transaction & transaction);

    std::pair<GoalProblem, libdnf5::solv::IdQueue> add_install_to_goal(
        base::Transaction & transaction, GoalAction action, const std::string & spec, GoalJobSettings & settings);
    std::pair<GoalProblem, libdnf5::solv::IdQueue> add_install_debug_to_goal(
        base::Transaction & transaction, const std::string & spec, GoalJobSettings & settings);
    void add_provide_install_to_goal(const std::string & spec, GoalJobSettings & settings);
    GoalProblem add_reinstall_to_goal(
        base::Transaction & transaction, const std::string & spec, GoalJobSettings & settings);
    GoalProblem add_remove_to_goal(
        base::Transaction & transaction, const std::string & spec, GoalJobSettings & settings);
    GoalProblem add_up_down_distrosync_to_goal(
        base::Transaction & transaction,
        GoalAction action,
        const std::string & spec,
        GoalJobSettings & settings,
        bool minimal = false);
    void add_rpms_to_goal(base::Transaction & transaction);

    static void filter_candidates_for_advisory_upgrade(
        const BaseWeakPtr & base,
        libdnf5::rpm::PackageQuery & candidates,
        const libdnf5::advisory::AdvisoryQuery & advisories,
        bool add_obsoletes);

    void add_group_install_to_goal(
        base::Transaction & transaction,
        const transaction::TransactionItemReason reason,
        comps::GroupQuery group_query,
        GoalJobSettings & settings);
    void add_group_remove_to_goal(
        std::vector<std::tuple<std::string, transaction::TransactionItemReason, comps::GroupQuery, GoalJobSettings>> &
            groups_to_remove);
    void add_group_upgrade_to_goal(
        base::Transaction & transaction, comps::GroupQuery group_query, GoalJobSettings & settings);

    void add_environment_install_to_goal(
        base::Transaction & transaction, comps::EnvironmentQuery environment_query, GoalJobSettings & settings);
    void add_environment_remove_to_goal(
        base::Transaction & transaction,
        std::vector<std::tuple<std::string, comps::EnvironmentQuery, GoalJobSettings>> & environments_to_remove);
    void add_environment_upgrade_to_goal(
        base::Transaction & transaction, comps::EnvironmentQuery environment_query, GoalJobSettings & settings);

    GoalProblem add_reason_change_to_goal(
        base::Transaction & transaction,
        const std::string & spec,
        const transaction::TransactionItemReason reason,
        const std::optional<std::string> & group_id,
        GoalJobSettings & settings);

    /// Parse the spec (package, group, remote or local rpm file) and process it.
    /// Repository packages and groups are directly added to rpm_specs / group_specs,
    /// files are stored for being later downloaded and added to command line repo.
    void add_spec(GoalAction action, const std::string & spec, const GoalJobSettings & settings);

    /// Add all (remote or local) rpm paths to the goal.
    /// Remote URLs are first downloaded and all the paths are inserted into
    /// cmdline repo.
    void add_paths_to_goal();

    void set_exclude_from_weak(const std::vector<std::string> & exclude_from_weak);
    void autodetect_unsatisfied_installed_weak_dependencies();

    // Paths to elements (packages/groups/envs) in replay are taken relative to replay_location.
    GoalProblem add_replay_to_goal(
        base::Transaction & transaction,
        const transaction::TransactionReplay & replay,
        GoalJobSettings settings,
        std::filesystem::path replay_location = "");

private:
    friend class Goal;
    BaseWeakPtr base;
    std::vector<std::tuple<GoalAction, std::string, GoalJobSettings>> module_specs;
    /// <libdnf5::GoalAction, std::string pkg_spec, libdnf5::GoalJobSettings settings>
    std::vector<std::tuple<GoalAction, std::string, GoalJobSettings>> rpm_specs;
    /// <TransactionItemReason reason, std::string pkg_spec, optional<std::string> group id, libdnf5::GoalJobSettings settings>
    std::vector<std::tuple<
        libdnf5::transaction::TransactionItemReason,
        std::string,
        std::optional<std::string>,
        GoalJobSettings>>
        rpm_reason_change_specs;
    /// <libdnf5::GoalAction, rpm Ids, libdnf5::GoalJobSettings settings>
    std::vector<std::tuple<GoalAction, libdnf5::solv::IdQueue, GoalJobSettings>> rpm_ids;
    /// <libdnf5::GoalAction, std::string filepath, libdnf5::GoalJobSettings settings>
    std::vector<std::tuple<GoalAction, std::string, GoalJobSettings>> rpm_filepaths;

    // (spec, reason, query, settings)
    using GroupItem = std::tuple<std::string, transaction::TransactionItemReason, comps::GroupQuery, GoalJobSettings>;
    // To correctly remove all unneeded group packages when a group is removed,
    // the list of all other removed groups in the transaction is needed.
    // Therefore resolve spec -> group_query first.
    std::map<GoalAction, std::vector<GroupItem>> resolved_group_specs;

    using EnvironmentItem = std::tuple<std::string, comps::EnvironmentQuery, GoalJobSettings>;
    std::map<GoalAction, std::vector<EnvironmentItem>> resolved_environment_specs;

    /// group_specs contain both comps groups and environments.
    /// <libdnf5::GoalAction, TransactionItemReason reason, std::string group_spec, GoalJobSettings settings>
    std::vector<GroupSpec> group_specs;

    rpm::solv::GoalPrivate rpm_goal;
    bool allow_erasing{false};

    void install_group_package(base::Transaction & transaction, libdnf5::comps::Package pkg);
    void remove_group_packages(const rpm::PackageSet & remove_candidates);

    libdnf5::solv::SolvMap incoming_vendor_bypassed_solvables{0};

    // (path_to_serialized_transaction, settings)
    std::unique_ptr<std::tuple<std::filesystem::path, GoalJobSettings>> serialized_transaction;

    std::unique_ptr<std::tuple<std::vector<transaction::Transaction>, GoalJobSettings>> revert_transactions;
    std::unique_ptr<std::tuple<transaction::Transaction, GoalJobSettings>> redo_transaction;
};

Goal::Goal(const BaseWeakPtr & base) : p_impl(new Impl(base)) {}
Goal::Goal(Base & base) : p_impl(new Impl(base.get_weak_ptr())) {}

Goal::Impl::Impl(const BaseWeakPtr & base) : base(base), rpm_goal(base) {}

Goal::~Goal() = default;

Goal::Impl::~Impl() = default;

void Goal::add_module_enable(
    [[maybe_unused]] const std::string & spec, [[maybe_unused]] const libdnf5::GoalJobSettings & settings) {
#ifdef WITH_MODULEMD
    p_impl->module_specs.push_back(std::make_tuple(GoalAction::ENABLE, spec, settings));
#else
    libdnf_throw_assertion("libdnf5 compiled without module support.");
#endif
}

void Goal::add_module_disable(
    [[maybe_unused]] const std::string & spec, [[maybe_unused]] const libdnf5::GoalJobSettings & settings) {
#ifdef WITH_MODULEMD
    p_impl->module_specs.push_back(std::make_tuple(GoalAction::DISABLE, spec, settings));
#else
    libdnf_throw_assertion("libdnf5 compiled without module support.");
#endif
}

void Goal::add_module_reset(
    [[maybe_unused]] const std::string & spec, [[maybe_unused]] const libdnf5::GoalJobSettings & settings) {
#ifdef WITH_MODULEMD
    p_impl->module_specs.push_back(std::make_tuple(GoalAction::RESET, spec, settings));
#else
    libdnf_throw_assertion("libdnf5 compiled without module support.");
#endif
}

void Goal::add_install(const std::string & spec, const libdnf5::GoalJobSettings & settings) {
    p_impl->add_spec(GoalAction::INSTALL, spec, settings);
}

void Goal::add_debug_install(const std::string & spec, const libdnf5::GoalJobSettings & settings) {
    p_impl->add_spec(GoalAction::INSTALL_DEBUG, spec, settings);
}

void Goal::add_upgrade(const std::string & spec, const libdnf5::GoalJobSettings & settings, bool minimal) {
    p_impl->add_spec(minimal ? GoalAction::UPGRADE_MINIMAL : GoalAction::UPGRADE, spec, settings);
}

void Goal::add_downgrade(const std::string & spec, const libdnf5::GoalJobSettings & settings) {
    p_impl->add_spec(GoalAction::DOWNGRADE, spec, settings);
}

void Goal::add_reinstall(const std::string & spec, const GoalJobSettings & settings) {
    p_impl->add_spec(GoalAction::REINSTALL, spec, settings);
}

void Goal::add_remove(const std::string & spec, const GoalJobSettings & settings) {
    p_impl->add_spec(GoalAction::REMOVE, spec, settings);
}


void Goal::add_rpm_install(const std::string & spec, const GoalJobSettings & settings) {
    p_impl->rpm_specs.push_back(std::make_tuple(GoalAction::INSTALL, spec, settings));
}

void Goal::add_rpm_install(const rpm::Package & rpm_package, const GoalJobSettings & settings) {
    p_impl->add_rpm_ids(GoalAction::INSTALL, rpm_package, settings);
}

void Goal::add_rpm_install(const rpm::PackageSet & package_set, const GoalJobSettings & settings) {
    p_impl->add_rpm_ids(GoalAction::INSTALL, package_set, settings);
}

void Goal::add_rpm_install_or_reinstall(const rpm::Package & rpm_package, const GoalJobSettings & settings) {
    p_impl->add_rpm_ids(GoalAction::INSTALL_OR_REINSTALL, rpm_package, settings);
}

void Goal::add_rpm_install_or_reinstall(const rpm::PackageSet & package_set, const GoalJobSettings & settings) {
    p_impl->add_rpm_ids(GoalAction::INSTALL_OR_REINSTALL, package_set, settings);
}

void Goal::add_rpm_reinstall(const std::string & spec, const GoalJobSettings & settings) {
    p_impl->rpm_specs.push_back(std::make_tuple(GoalAction::REINSTALL, spec, settings));
}

void Goal::add_rpm_reinstall(const rpm::Package & rpm_package, const GoalJobSettings & settings) {
    p_impl->add_rpm_ids(GoalAction::REINSTALL, rpm_package, settings);
}

void Goal::add_rpm_remove(const std::string & spec, const GoalJobSettings & settings) {
    p_impl->rpm_specs.push_back(std::make_tuple(GoalAction::REMOVE, spec, settings));
}

void Goal::add_rpm_remove(const rpm::Package & rpm_package, const GoalJobSettings & settings) {
    p_impl->add_rpm_ids(GoalAction::REMOVE, rpm_package, settings);
}

void Goal::add_rpm_remove(const rpm::PackageSet & package_set, const GoalJobSettings & settings) {
    p_impl->add_rpm_ids(GoalAction::REMOVE, package_set, settings);
}

void Goal::add_rpm_upgrade(const std::string & spec, const GoalJobSettings & settings, bool minimal) {
    if (minimal) {
        p_impl->rpm_specs.push_back(std::make_tuple(GoalAction::UPGRADE_MINIMAL, spec, settings));
    } else {
        p_impl->rpm_specs.push_back(std::make_tuple(GoalAction::UPGRADE, spec, settings));
    }
}

void Goal::add_rpm_upgrade(const GoalJobSettings & settings, bool minimal) {
    if (settings.get_from_repo_ids().empty()) {
        const auto action = minimal ? GoalAction::UPGRADE_ALL_MINIMAL : GoalAction::UPGRADE_ALL;
        p_impl->rpm_specs.push_back(std::make_tuple(action, std::string(), settings));
    } else {
        // We want to perform upgrade only for packages installed from the from_repo_ids.
        // Using UPGRADE_ALL and UPGRADE_ALL_MINIMAL is not possible.
        const auto action = minimal ? GoalAction::UPGRADE_MINIMAL : GoalAction::UPGRADE;
        p_impl->rpm_specs.push_back(std::make_tuple(action, "*", settings));
    }
}

void Goal::add_rpm_upgrade(const rpm::Package & rpm_package, const GoalJobSettings & settings, bool minimal) {
    if (minimal) {
        p_impl->add_rpm_ids(GoalAction::UPGRADE_MINIMAL, rpm_package, settings);
    } else {
        p_impl->add_rpm_ids(GoalAction::UPGRADE, rpm_package, settings);
    }
}

void Goal::add_rpm_upgrade(const rpm::PackageSet & package_set, const GoalJobSettings & settings, bool minimal) {
    if (minimal) {
        p_impl->add_rpm_ids(GoalAction::UPGRADE_MINIMAL, package_set, settings);
    } else {
        p_impl->add_rpm_ids(GoalAction::UPGRADE, package_set, settings);
    }
}

void Goal::add_rpm_downgrade(const std::string & spec, const GoalJobSettings & settings) {
    p_impl->rpm_specs.push_back(std::make_tuple(GoalAction::DOWNGRADE, spec, settings));
}

void Goal::add_rpm_downgrade(const rpm::Package & rpm_package, const GoalJobSettings & settings) {
    p_impl->add_rpm_ids(GoalAction::DOWNGRADE, rpm_package, settings);
}

void Goal::add_rpm_distro_sync(const std::string & spec, const GoalJobSettings & settings) {
    p_impl->rpm_specs.push_back(std::make_tuple(GoalAction::DISTRO_SYNC, spec, settings));
}

void Goal::add_rpm_distro_sync(const GoalJobSettings & settings) {
    if (settings.get_from_repo_ids().empty()) {
        p_impl->rpm_specs.push_back(std::make_tuple(GoalAction::DISTRO_SYNC_ALL, std::string(), settings));
    } else {
        // We want to perform distro-sync only for packages installed from the from_repo_ids.
        // Using DISTROSYNC_ALL is not possible.
        p_impl->rpm_specs.push_back(std::make_tuple(GoalAction::DISTRO_SYNC, "*", settings));
    }
}

void Goal::add_rpm_distro_sync(const rpm::Package & rpm_package, const GoalJobSettings & settings) {
    p_impl->add_rpm_ids(GoalAction::DISTRO_SYNC, rpm_package, settings);
}

void Goal::add_rpm_distro_sync(const rpm::PackageSet & package_set, const GoalJobSettings & settings) {
    p_impl->add_rpm_ids(GoalAction::DISTRO_SYNC, package_set, settings);
}

void Goal::add_rpm_reason_change(
    const std::string & spec,
    const libdnf5::transaction::TransactionItemReason reason,
    const std::string & group_id,
    const libdnf5::GoalJobSettings & settings) {
    libdnf_user_assert(
        reason != libdnf5::transaction::TransactionItemReason::GROUP || !group_id.empty(),
        "group_id is required for setting reason \"GROUP\"");
    p_impl->rpm_reason_change_specs.push_back(std::make_tuple(reason, spec, group_id, settings));
}

void Goal::add_provide_install(const std::string & spec, const GoalJobSettings & settings) {
    p_impl->rpm_specs.push_back(std::make_tuple(GoalAction::INSTALL_VIA_PROVIDE, spec, settings));
}

void Goal::Impl::add_spec(GoalAction action, const std::string & spec, const GoalJobSettings & settings) {
    if (spec.starts_with("@")) {
        // spec is a group, environment or a module
        // TODO(mblaha): detect and process modules
        std::string group_spec = spec.substr(1);
        auto group_settings = libdnf5::GoalJobSettings(settings);
        // for compatibility reasons '@group-spec' can mean also environment
        group_settings.set_group_search_environments(true);
        // support for kickstart environmental groups syntax - '@^environment-spec'
        if (group_spec.starts_with("^")) {
            group_spec = group_spec.substr(1);
            group_settings.set_group_search_groups(false);
        } else {
            group_settings.set_group_search_groups(true);
        }
        group_specs.push_back(
            std::make_tuple(action, libdnf5::transaction::TransactionItemReason::USER, group_spec, group_settings));
    } else {
        const std::string_view ext(".rpm");
        if (libdnf5::utils::url::is_url(spec) || (spec.length() > ext.length() && spec.ends_with(ext))) {
            // spec is a remote rpm file or a local rpm file
            if (action == GoalAction::REMOVE) {
                throw RuntimeError(M_("Unsupported argument for REMOVE action: {}"), spec);
            }
            rpm_filepaths.emplace_back(action, spec, settings);
        } else {
            // otherwise the spec is a repository package
            rpm_specs.emplace_back(action, spec, settings);
        }
    }
}

void Goal::Impl::add_rpm_ids(GoalAction action, const rpm::Package & rpm_package, const GoalJobSettings & settings) {
    libdnf_assert_same_base(base, rpm_package.get_base());

    libdnf5::solv::IdQueue ids;
    ids.push_back(rpm_package.get_id().id);
    rpm_ids.push_back(std::make_tuple(action, std::move(ids), settings));
}

void Goal::Impl::add_rpm_ids(GoalAction action, const rpm::PackageSet & package_set, const GoalJobSettings & settings) {
    libdnf_assert_same_base(base, package_set.get_base());

    libdnf5::solv::IdQueue ids;
    for (auto package_id : *package_set.p_impl) {
        ids.push_back(package_id);
    }
    rpm_ids.push_back(std::make_tuple(action, std::move(ids), settings));
}

// @replaces part of libdnf/sack/query.cpp:method:filterAdvisory called with HY_EQG and HY_UPGRADE
void Goal::Impl::filter_candidates_for_advisory_upgrade(
    const BaseWeakPtr & base,
    libdnf5::rpm::PackageQuery & candidates,
    const libdnf5::advisory::AdvisoryQuery & advisories,
    bool add_obsoletes) {
    rpm::PackageQuery installed(base);
    installed.filter_installed();

    // When doing advisory upgrade consider only candidate pkgs that can possibly upgrade some pkg.
    // Both branches do candidates.filter_upgrades().
    // This basically means that it matches some already installed pkg by name, has higher evr and
    // has the same architecture or one of them is noarch.
    // This is required because otherwise a pkg with different Arch than installed or noarch can end
    // up in upgrade set which is wrong. It can result in dependency issues, reported as: RhBug:2088149.
    if (add_obsoletes) {
        rpm::PackageQuery obsoletes(candidates);

        candidates.filter_upgrades();

        obsoletes.filter_available();
        // Prepare obsoletes of installed as well as obsoletes of any possible upgrade that could happen (candidates)
        rpm::PackageQuery possibly_obsoleted(candidates);
        possibly_obsoleted |= installed;
        obsoletes.filter_obsoletes(possibly_obsoleted);

        // Add obsoletes to candidates
        candidates |= obsoletes;
    } else {
        candidates.filter_upgrades();
    }

    // Apply security filters only to packages with highest priority (lowest priority number),
    // to unify behaviour of upgrade and upgrade-minimal
    candidates.filter_priority();

    // Since we want to satisfy all advisory packages we can keep just the latest
    // (all lower EVR adv pkgs are satistified by the latests)
    // We also want to skip already resolved advisories.
    candidates.filter_latest_unresolved_advisories(advisories, installed, libdnf5::sack::QueryCmp::GTE);
}

void Goal::add_group_install(
    const std::string & spec,
    const libdnf5::transaction::TransactionItemReason reason,
    const GoalJobSettings & settings) {
    p_impl->group_specs.push_back(std::make_tuple(GoalAction::INSTALL, reason, spec, settings));
}

void Goal::add_group_remove(
    const std::string & spec,
    const libdnf5::transaction::TransactionItemReason reason,
    const GoalJobSettings & settings) {
    p_impl->group_specs.push_back(std::make_tuple(GoalAction::REMOVE, reason, spec, settings));
}

void Goal::add_group_upgrade(const std::string & spec, const libdnf5::GoalJobSettings & settings) {
    // upgrade keeps old reason, thus use NONE here
    p_impl->group_specs.push_back(
        std::make_tuple(GoalAction::UPGRADE, libdnf5::transaction::TransactionItemReason::NONE, spec, settings));
}

GoalProblem Goal::Impl::add_specs_to_goal(base::Transaction & transaction) {
    auto sack = base->get_rpm_package_sack();
    auto & cfg_main = base->get_config();
    auto ret = GoalProblem::NO_PROBLEM;
    for (auto & [action, spec, settings] : rpm_specs) {
        switch (action) {
            case GoalAction::INSTALL:
            case GoalAction::INSTALL_BY_COMPS: {
                auto [problem, idqueue] = add_install_to_goal(transaction, action, spec, settings);
                rpm_goal.add_transaction_user_installed(idqueue);
                ret |= problem;
            } break;
            case GoalAction::INSTALL_VIA_PROVIDE:
                add_provide_install_to_goal(spec, settings);
                break;
            case GoalAction::REINSTALL:
                ret |= add_reinstall_to_goal(transaction, spec, settings);
                break;
            case GoalAction::REMOVE:
                ret |= add_remove_to_goal(transaction, spec, settings);
                break;
            case GoalAction::DISTRO_SYNC:
            case GoalAction::DOWNGRADE:
            case GoalAction::UPGRADE:
                ret |= add_up_down_distrosync_to_goal(transaction, action, spec, settings);
                break;
            case GoalAction::UPGRADE_MINIMAL:
                ret |= add_up_down_distrosync_to_goal(transaction, action, spec, settings, true);
                break;
            case GoalAction::UPGRADE_ALL:
            case GoalAction::UPGRADE_ALL_MINIMAL: {
                rpm::PackageQuery query(base);

                if (!settings.get_to_repo_ids().empty()) {
                    query.filter_repo_id(settings.get_to_repo_ids(), sack::QueryCmp::GLOB);
                }

                const auto & to_vendors = settings.get_to_vendors();

                if (!to_vendors.empty()) {
                    query.filter_vendor(to_vendors, sack::QueryCmp::GLOB);
                }

                // Apply advisory filters
                if (settings.get_advisory_filter() != nullptr) {
                    filter_candidates_for_advisory_upgrade(
                        base, query, *settings.get_advisory_filter(), cfg_main.get_obsoletes_option().get_value());
                    if (query.empty()) {
                        transaction.p_impl->add_resolve_log(
                            action,
                            GoalProblem::NOT_FOUND_IN_ADVISORIES,
                            settings,
                            libdnf5::transaction::TransactionItemType::PACKAGE,
                            {},
                            {},
                            libdnf5::Logger::Level::WARNING);
                    }
                }

                if (query.empty()) {
                    return GoalProblem::NO_PROBLEM;
                }

                // Make the smallest possible upgrade
                if (action == GoalAction::UPGRADE_ALL_MINIMAL) {
                    query.filter_earliest_evr();
                }

                if (!to_vendors.empty()) {
                    incoming_vendor_bypassed_solvables |= *query.p_impl;
                }

                libdnf5::solv::IdQueue upgrade_ids;
                for (auto package_id : *query.p_impl) {
                    upgrade_ids.push_back(package_id);
                }
                rpm_goal.add_upgrade(
                    upgrade_ids, settings.resolve_best(cfg_main), settings.resolve_clean_requirements_on_remove());
            } break;
            case GoalAction::DISTRO_SYNC_ALL: {
                rpm::PackageQuery query(base);

                if (settings.get_to_repo_ids().empty()) {
                    // Since distro-sync uses SOLVER_TARGETED mode we cannot pass in installed packages because
                    // if we did updating only a subset of packages would be a valid solution. However in a distro-sync
                    // we want to ensure ALL packages are synchronized with the target repository.
                    query.filter_available();
                } else {
                    // Keep only the packages available in the specified repositories.
                    query.filter_repo_id(settings.get_to_repo_ids(), sack::QueryCmp::GLOB);
                }

                const auto & to_vendors = settings.get_to_vendors();

                if (!to_vendors.empty()) {
                    query.filter_vendor(to_vendors, sack::QueryCmp::GLOB);
                }

                if (query.empty()) {
                    return GoalProblem::NO_PROBLEM;
                }

                if (!to_vendors.empty()) {
                    incoming_vendor_bypassed_solvables |= *query.p_impl;
                }

                libdnf5::solv::IdQueue upgrade_ids;
                for (auto package_id : *query.p_impl) {
                    upgrade_ids.push_back(package_id);
                }
                rpm_goal.add_distro_sync(
                    upgrade_ids,
                    settings.resolve_skip_broken(cfg_main),
                    settings.resolve_best(cfg_main),
                    settings.resolve_clean_requirements_on_remove());
            } break;
            case GoalAction::INSTALL_DEBUG: {
                auto [problem, idqueue] = add_install_debug_to_goal(transaction, spec, settings);
                rpm_goal.add_transaction_user_installed(idqueue);
                ret |= problem;
            } break;
            case GoalAction::INSTALL_OR_REINSTALL: {
                libdnf_throw_assertion("Unsupported action \"INSTALL_OR_REINSTALL\"");
            }
            case GoalAction::RESOLVE: {
                libdnf_throw_assertion("Unsupported action \"RESOLVE\"");
            }
            case GoalAction::REASON_CHANGE: {
                libdnf_throw_assertion("Unsupported action \"REASON_CHANGE\"");
            }
            case GoalAction::ENABLE: {
                libdnf_throw_assertion("Unsupported action \"ENABLE\"");
            }
            case GoalAction::DISABLE: {
                libdnf_throw_assertion("Unsupported action \"DISABLE\"");
            }
            case GoalAction::RESET: {
                libdnf_throw_assertion("Unsupported action \"RESET\"");
            }
            case GoalAction::REPLAY_PARSE: {
                libdnf_throw_assertion("Unsupported action \"REPLAY PARSE\"");
            }
            case GoalAction::REPLAY_INSTALL: {
                libdnf_throw_assertion("Unsupported action \"REPLAY INSTALL\"");
            }
            case GoalAction::REPLAY_REMOVE: {
                libdnf_throw_assertion("Unsupported action \"REPLAY REMOVE\"");
            }
            case GoalAction::REPLAY_UPGRADE: {
                libdnf_throw_assertion("Unsupported action \"REPLAY UPGRADE\"");
            }
            case GoalAction::REPLAY_REINSTALL: {
                libdnf_throw_assertion("Unsupported action \"REPLAY REINSTALL\"");
            }
            case GoalAction::REPLAY_REASON_CHANGE: {
                libdnf_throw_assertion("Unsupported action \"REPLAY REASON CHANGE\"");
            }
            case GoalAction::REPLAY_REASON_OVERRIDE: {
                libdnf_throw_assertion("Unsupported action \"REPLAY REASON OVERRIDE\"");
            }
            case GoalAction::REVERT_COMPS_UPGRADE: {
                libdnf_throw_assertion("Unsupported action \"REVERT_COMPS_UPGRADE\"");
            }
            case GoalAction::MERGE: {
                libdnf_throw_assertion("Unsupported action \"MERGE\"");
            }
        }
    }
    return ret;
}


#ifdef WITH_MODULEMD
GoalProblem Goal::Impl::add_module_specs_to_goal(base::Transaction & transaction) {
    auto ret = GoalProblem::NO_PROBLEM;
    module::ModuleSack & module_sack = *base->get_module_sack();

    std::vector<std::string> missing_module_specs;
    for (auto & [action, spec, settings] : module_specs) {
        try {
            switch (action) {
                case GoalAction::ENABLE: {
                    bool skip_broken = settings.resolve_skip_broken(base->get_config());
                    auto log_level = skip_broken ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR;
                    const auto & enable_ret = module_sack.p_impl->enable(spec);
                    if (!enable_ret.second.empty()) {
                        transaction.p_impl->add_resolve_log(
                            action,
                            GoalProblem::MULTIPLE_STREAMS,
                            GoalJobSettings(),
                            libdnf5::transaction::TransactionItemType::MODULE,
                            spec,
                            enable_ret.second,
                            log_level);
                        if (!skip_broken) {
                            ret |= GoalProblem::MULTIPLE_STREAMS;
                        }
                    }
                    break;
                }
                case GoalAction::DISABLE:
                    module_sack.p_impl->disable(spec);
                    break;
                case GoalAction::RESET:
                    module_sack.p_impl->reset(spec);
                    break;
                default:
                    libdnf_throw_assertion("Unsupported action \"{}\"", goal_action_to_string(action));
            }
        } catch (const module::NoModuleError &) {
            bool skip_unavailable = settings.resolve_skip_unavailable(base->get_config());
            auto log_level = skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR;
            transaction.p_impl->add_resolve_log(
                action,
                GoalProblem::NOT_FOUND,
                GoalJobSettings(),
                libdnf5::transaction::TransactionItemType::MODULE,
                spec,
                {},
                log_level);
            if (!skip_unavailable) {
                ret |= GoalProblem::NOT_FOUND;
            }
        }
    }
    return ret;
}
#endif

GoalProblem Goal::Impl::add_serialized_transaction_to_goal(base::Transaction & transaction) {
    if (!serialized_transaction) {
        return GoalProblem::NO_PROBLEM;
    }

    auto & [replay_path, settings] = *serialized_transaction;
    utils::fs::File replay_file(replay_path, "r");
    auto replay_location = replay_path;
    replay_location.remove_filename();
    try {
        auto replay = transaction::parse_transaction_replay(replay_file.read());
        return add_replay_to_goal(transaction, replay, settings, replay_location);
    } catch (const libdnf5::transaction::TransactionReplayError & ex) {
        transaction.p_impl->add_resolve_log(
            GoalAction::REPLAY_PARSE,
            libdnf5::GoalProblem::MALFORMED,
            settings,
            libdnf5::transaction::TransactionItemType::PACKAGE,
            replay_path,
            {ex.what()},
            libdnf5::Logger::Level::ERROR);
        return libdnf5::GoalProblem::MALFORMED;
    }
}

static std::set<std::string> query_to_vec_of_nevra_str(const libdnf5::rpm::PackageQuery & query) {
    std::set<std::string> query_set = {};
    std::transform(query.begin(), query.end(), std::inserter(query_set, query_set.begin()), [](const auto & pkg) {
        return pkg.to_string();
    });

    return query_set;
}

GoalProblem Goal::Impl::add_replay_to_goal(
    base::Transaction & transaction,
    const transaction::TransactionReplay & replay,
    GoalJobSettings settings,
    std::filesystem::path replay_location) {
    auto ret = GoalProblem::NO_PROBLEM;
    bool skip_unavailable = settings.resolve_skip_unavailable(base->get_config());

    std::unordered_set<std::string> rpm_nevra_cache;

    for (const auto & package_replay : replay.packages) {
        rpm_nevra_cache.insert(package_replay.nevra);
        libdnf5::GoalJobSettings settings_per_package = settings;
        settings_per_package.set_clean_requirements_on_remove(libdnf5::GoalSetting::SET_FALSE);

        std::optional<libdnf5::rpm::Package> local_pkg;
        if (!package_replay.package_path.empty()) {
            // Package paths are relative to replay location
            local_pkg = base->get_repo_sack()->add_stored_transaction_package(
                replay_location / package_replay.package_path, package_replay.repo_id);
        }

        const auto nevras = rpm::Nevra::parse(package_replay.nevra, {rpm::Nevra::Form::NEVRA});
        libdnf_assert(
            nevras.size() == 1,
            "Cannot parse rpm nevra or ambiguous \"{}\" while replaying transaction.",
            package_replay.nevra);

        rpm::PackageQuery query_na(base);
        query_na.filter_name(nevras[0].get_name());
        query_na.filter_arch(nevras[0].get_arch());
        auto query_nevra = query_na;
        query_nevra.filter_nevra(nevras[0]);

        if (!package_replay.repo_id.empty()) {
            repo::RepoQuery enabled_repos(base);
            enabled_repos.filter_enabled(true);
            enabled_repos.filter_id(package_replay.repo_id);
            if (!enabled_repos.empty()) {
                settings_per_package.set_to_repo_ids({package_replay.repo_id});
            }
        }

        if (package_replay.action == transaction::TransactionItemAction::UPGRADE ||
            package_replay.action == transaction::TransactionItemAction::INSTALL ||
            package_replay.action == transaction::TransactionItemAction::DOWNGRADE) {
            if (query_nevra.empty()) {
                auto problem = transaction.p_impl->report_not_found(
                    GoalAction::REPLAY_INSTALL,
                    package_replay.nevra,
                    settings,
                    skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR);
                if (!skip_unavailable) {
                    ret |= problem;
                }
                continue;
            }

            // In order to properly report an error when another version of a package with action INSTALL is already
            // installed we have to verify several conditions.
            // - There is another versions installed for this package (name-arch).
            // - The package isn't installonly.
            // - The transaction doesn't contain an outbound action for this name-arch.
            //   This could happend during transaction reverting because upgrade/downgrade/reinstall (and obsoleting) actions are reverted as a REMOVE.
            //   For example upgrade transaction: [a-2 Upgrade, a-1 Replaced] is reverted to [a-2 Remove, a-1 Install].
            //   This is because we don't store the "replaces" relationship in history DB (there is a table `item_replaced_by`, but it is not populated
            //   and it doesn't seem worth it to populate it because of this use case) so we don't know which action to pick. We could try to guess
            //   based on the transaction packages but check seems easier.
            if (package_replay.action == transaction::TransactionItemAction::INSTALL) {
                bool na_has_outbound_action = false;
                query_na.filter_installed();
                for (const auto & installed_na : query_na) {
                    na_has_outbound_action |=
                        std::find_if(replay.packages.begin(), replay.packages.end(), [&installed_na](const auto & r) {
                            return r.nevra == installed_na.get_nevra() && transaction_item_action_is_outbound(r.action);
                        }) != replay.packages.end();
                    if (na_has_outbound_action) {
                        break;
                    }
                }
                if (!na_has_outbound_action) {
                    auto is_installonly = query_na;
                    is_installonly.filter_installonly();

                    if (!query_na.empty() && is_installonly.empty()) {
                        query_nevra.filter_installed();
                        auto problem = GoalProblem::INSTALLED_IN_DIFFERENT_VERSION;

                        if (!query_nevra.empty()) {
                            problem = GoalProblem::ALREADY_INSTALLED;
                            if (settings.get_override_reasons()) {
                                if ((*query_nevra.begin()).get_reason() != package_replay.reason) {
                                    rpm_reason_change_specs.push_back(std::make_tuple(
                                        package_replay.reason,
                                        package_replay.nevra,
                                        package_replay.group_id,
                                        settings_per_package));
                                }
                            }
                        }

                        auto log_level = libdnf5::Logger::Level::WARNING;
                        if (!settings.get_ignore_installed()) {
                            log_level = libdnf5::Logger::Level::ERROR;
                            ret = problem;
                        }

                        transaction.p_impl->add_resolve_log(
                            GoalAction::REPLAY_INSTALL,
                            problem,
                            settings,
                            libdnf5::transaction::TransactionItemType::PACKAGE,
                            package_replay.nevra,
                            query_to_vec_of_nevra_str(query_na),
                            log_level);
                        if (problem == GoalProblem::ALREADY_INSTALLED) {
                            continue;
                        }
                    }
                }
            }

            if (local_pkg) {
                add_rpm_ids(GoalAction::INSTALL, *local_pkg, settings_per_package);
            } else {
                rpm_specs.emplace_back(GoalAction::INSTALL, package_replay.nevra, settings_per_package);
            }
            transaction.p_impl->rpm_reason_overrides[package_replay.nevra] = package_replay.reason;
        } else if (package_replay.action == transaction::TransactionItemAction::REINSTALL) {
            if (query_nevra.empty()) {
                auto problem = transaction.p_impl->report_not_found(
                    GoalAction::REPLAY_REINSTALL,
                    package_replay.nevra,
                    settings,
                    skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR);
                if (!skip_unavailable) {
                    ret |= problem;
                }
                continue;
            }

            if (local_pkg) {
                add_rpm_ids(GoalAction::REINSTALL, *local_pkg, settings_per_package);
            } else {
                rpm_specs.emplace_back(GoalAction::REINSTALL, package_replay.nevra, settings_per_package);
            }
            transaction.p_impl->rpm_reason_overrides[package_replay.nevra] = package_replay.reason;
        } else if (package_replay.action == transaction::TransactionItemAction::REMOVE) {
            if (query_nevra.empty()) {
                auto problem = transaction.p_impl->report_not_found(
                    GoalAction::REPLAY_REMOVE,
                    package_replay.nevra,
                    settings,
                    skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR);
                if (!skip_unavailable) {
                    ret |= problem;
                }
                continue;
            }

            query_nevra.filter_installed();
            if (query_nevra.empty()) {
                auto log_level = libdnf5::Logger::Level::WARNING;
                query_na.filter_installed();
                auto problem =
                    query_na.empty() ? GoalProblem::NOT_INSTALLED : GoalProblem::INSTALLED_IN_DIFFERENT_VERSION;
                if (!settings.get_ignore_installed()) {
                    log_level = libdnf5::Logger::Level::ERROR;
                    ret |= problem;
                }
                transaction.p_impl->add_resolve_log(
                    GoalAction::REPLAY_REMOVE,
                    problem,
                    settings,
                    libdnf5::transaction::TransactionItemType::PACKAGE,
                    package_replay.nevra,
                    query_to_vec_of_nevra_str(query_na),
                    log_level);
                continue;
            }

            if (local_pkg) {
                add_rpm_ids(GoalAction::REMOVE, *local_pkg, settings_per_package);
            } else {
                rpm_specs.emplace_back(GoalAction::REMOVE, package_replay.nevra, settings_per_package);
            }
            transaction.p_impl->rpm_reason_overrides[package_replay.nevra] = package_replay.reason;
        } else if (package_replay.action == transaction::TransactionItemAction::REPLACED) {
            if (query_nevra.empty()) {
                auto problem = transaction.p_impl->report_not_found(
                    GoalAction::REPLAY_REMOVE,
                    package_replay.nevra,
                    settings,
                    skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR);
                if (!skip_unavailable) {
                    ret |= problem;
                }
                continue;
            }

            query_nevra.filter_installed();
            if (query_nevra.empty()) {
                auto log_level = libdnf5::Logger::Level::WARNING;
                query_na.filter_installed();
                auto problem =
                    query_na.empty() ? GoalProblem::NOT_INSTALLED : GoalProblem::INSTALLED_IN_DIFFERENT_VERSION;
                if (!settings.get_ignore_installed()) {
                    log_level = libdnf5::Logger::Level::ERROR;
                    ret |= problem;
                }
                transaction.p_impl->add_resolve_log(
                    GoalAction::REPLAY_REMOVE,
                    problem,
                    settings,
                    libdnf5::transaction::TransactionItemType::PACKAGE,
                    package_replay.nevra,
                    query_to_vec_of_nevra_str(query_na),
                    log_level);
                continue;
            }
            // Removing the original versions (the reverse part of an action like e.g. Upgrade) is more robust,
            // but we can't do it if skip_unavailable is set because if the inbound action is skipped we would
            // simply remove the package.
            if (!skip_unavailable) {
                if (local_pkg) {
                    add_rpm_ids(GoalAction::REMOVE, *local_pkg, settings_per_package);
                } else {
                    rpm_specs.emplace_back(GoalAction::REMOVE, package_replay.nevra, settings_per_package);
                }
            }
        } else if (package_replay.action == transaction::TransactionItemAction::REASON_CHANGE) {
            if (query_nevra.empty()) {
                auto problem = transaction.p_impl->report_not_found(
                    GoalAction::REPLAY_REASON_CHANGE,
                    package_replay.nevra,
                    settings,
                    skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR);
                if (!skip_unavailable) {
                    ret |= problem;
                }
                continue;
            }

            rpm_reason_change_specs.emplace_back(
                package_replay.reason, package_replay.nevra, package_replay.group_id, settings_per_package);
        } else {
            libdnf_throw_assertion(
                "Unsupported package replay action \"{}\"", transaction_item_action_to_string(package_replay.action));
        }
    }

    transaction.p_impl->rpm_replays_nevra_cache.emplace_back(rpm_nevra_cache, settings);

    for (const auto & group_replay : replay.groups) {
        libdnf5::GoalJobSettings settings_per_group = settings;
        settings_per_group.set_group_no_packages(true);
        settings_per_group.set_group_package_types(group_replay.package_types);
        settings_per_group.set_group_search_groups(true);
        settings_per_group.set_group_search_environments(false);
        if (!group_replay.repo_id.empty()) {
            repo::RepoQuery enabled_repos(base);
            enabled_repos.filter_enabled(true);
            enabled_repos.filter_id(group_replay.repo_id);
            if (!enabled_repos.empty()) {
                //TODO(amatej): set_to_repo_ids is only for packages, remove it? Or update set_to_repo_ids
                settings_per_group.set_to_repo_ids({group_replay.repo_id});
            }
        }
        if (!group_replay.group_path.empty()) {
            // Group paths are relative to replay location
            base->get_repo_sack()->add_stored_transaction_comps(
                replay_location / group_replay.group_path, group_replay.repo_id);
        }

        comps::GroupQuery group_query_installed(base);
        group_query_installed.filter_groupid(group_replay.group_id);
        group_query_installed.filter_installed(true);

        if (group_replay.action == transaction::TransactionItemAction::INSTALL) {
            group_specs.emplace_back(
                GoalAction::INSTALL, group_replay.reason, group_replay.group_id, settings_per_group);
        } else if (group_replay.action == transaction::TransactionItemAction::UPGRADE) {
            if (group_query_installed.empty()) {
                auto log_level = libdnf5::Logger::Level::WARNING;
                if (!settings.get_ignore_installed()) {
                    log_level = libdnf5::Logger::Level::ERROR;
                    ret = GoalProblem::NOT_INSTALLED;
                }
                transaction.p_impl->add_resolve_log(
                    GoalAction::REPLAY_UPGRADE,
                    GoalProblem::NOT_INSTALLED,
                    settings,
                    libdnf5::transaction::TransactionItemType::GROUP,
                    group_replay.group_id,
                    {transaction_item_action_to_string(group_replay.action)},
                    log_level);
            }
            group_specs.emplace_back(
                GoalAction::UPGRADE, group_replay.reason, group_replay.group_id, settings_per_group);
        } else if (group_replay.action == transaction::TransactionItemAction::REMOVE) {
            if (group_query_installed.empty()) {
                auto log_level = libdnf5::Logger::Level::WARNING;
                if (!settings.get_ignore_installed()) {
                    log_level = libdnf5::Logger::Level::ERROR;
                    ret = GoalProblem::NOT_INSTALLED;
                }
                transaction.p_impl->add_resolve_log(
                    GoalAction::REPLAY_REMOVE,
                    GoalProblem::NOT_INSTALLED,
                    settings,
                    libdnf5::transaction::TransactionItemType::GROUP,
                    group_replay.group_id,
                    {transaction_item_action_to_string(group_replay.action)},
                    log_level);
            }
            group_specs.emplace_back(
                GoalAction::REMOVE, group_replay.reason, group_replay.group_id, settings_per_group);
        } else {
            libdnf_throw_assertion(
                "Unsupported group replay action \"{}\"", transaction_item_action_to_string(group_replay.action));
        }
    }

    for (const auto & env_replay : replay.environments) {
        libdnf5::GoalJobSettings settings_per_environment = settings;
        settings_per_environment.set_environment_no_groups(true);
        settings_per_environment.set_group_search_groups(false);
        settings_per_environment.set_group_search_environments(true);
        if (!env_replay.repo_id.empty()) {
            repo::RepoQuery enabled_repos(base);
            enabled_repos.filter_enabled(true);
            enabled_repos.filter_id(env_replay.repo_id);
            if (!enabled_repos.empty()) {
                //TODO(amatej): add ci test where we limit an env to a repo
                settings_per_environment.set_to_repo_ids({env_replay.repo_id});
            }
        }

        comps::EnvironmentQuery env_query_installed(base);
        env_query_installed.filter_environmentid(env_replay.environment_id);
        env_query_installed.filter_installed(true);

        if (!env_replay.environment_path.empty()) {
            // Environment paths are relative to replay location
            base->get_repo_sack()->add_stored_transaction_comps(
                replay_location / env_replay.environment_path, env_replay.repo_id);
        }
        if (env_replay.action == transaction::TransactionItemAction::INSTALL) {
            group_specs.emplace_back(
                GoalAction::INSTALL,
                transaction::TransactionItemReason::USER,
                env_replay.environment_id,
                settings_per_environment);
        } else if (env_replay.action == transaction::TransactionItemAction::UPGRADE) {
            if (env_query_installed.empty()) {
                auto log_level = libdnf5::Logger::Level::WARNING;
                if (!settings.get_ignore_installed()) {
                    log_level = libdnf5::Logger::Level::ERROR;
                    ret = GoalProblem::NOT_INSTALLED;
                }
                transaction.p_impl->add_resolve_log(
                    GoalAction::REPLAY_UPGRADE,
                    GoalProblem::NOT_INSTALLED,
                    settings,
                    libdnf5::transaction::TransactionItemType::ENVIRONMENT,
                    env_replay.environment_id,
                    {transaction_item_action_to_string(env_replay.action)},
                    log_level);
            }
            group_specs.emplace_back(
                GoalAction::UPGRADE,
                transaction::TransactionItemReason::USER,
                env_replay.environment_id,
                settings_per_environment);
        } else if (env_replay.action == transaction::TransactionItemAction::REMOVE) {
            if (env_query_installed.empty()) {
                auto log_level = libdnf5::Logger::Level::WARNING;
                if (!settings.get_ignore_installed()) {
                    log_level = libdnf5::Logger::Level::ERROR;
                    ret = GoalProblem::NOT_INSTALLED;
                }
                transaction.p_impl->add_resolve_log(
                    GoalAction::REPLAY_REMOVE,
                    GoalProblem::NOT_INSTALLED,
                    settings,
                    libdnf5::transaction::TransactionItemType::ENVIRONMENT,
                    env_replay.environment_id,
                    {transaction_item_action_to_string(env_replay.action)},
                    log_level);
            }
            group_specs.emplace_back(
                GoalAction::REMOVE,
                transaction::TransactionItemReason::USER,
                env_replay.environment_id,
                settings_per_environment);
        } else {
            libdnf_throw_assertion(
                "Unsupported environment replay action \"{}\"", transaction_item_action_to_string(env_replay.action));
        }
    }

    return ret;
}

GoalProblem Goal::Impl::resolve_group_specs(std::vector<GroupSpec> & specs, base::Transaction & transaction) {
    auto ret = GoalProblem::NO_PROBLEM;
    auto & cfg_main = base->get_config();
    // optimization - creating a group query from scratch is relatively expensive,
    // but the copy construction is cheap, so prepare a base query to be copied.
    comps::GroupQuery base_groups_query(base);
    comps::EnvironmentQuery base_environments_query(base);
    for (auto & [action, reason, spec, settings] : specs) {
        // For the REMOVE action, skip_unavailable defaults to true, ensuring
        // that the removal of a not-installed group is not treated as an error.
        bool skip_unavailable = (action == GoalAction::REMOVE && settings.get_skip_unavailable() == GoalSetting::AUTO)
                                    ? true
                                    : settings.resolve_skip_unavailable(cfg_main);
        auto log_level = skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR;
        if (action != GoalAction::INSTALL && action != GoalAction::INSTALL_BY_COMPS && action != GoalAction::REMOVE &&
            action != GoalAction::UPGRADE) {
            transaction.p_impl->add_resolve_log(
                action,
                GoalProblem::UNSUPPORTED_ACTION,
                settings,
                libdnf5::transaction::TransactionItemType::GROUP,
                spec,
                {},
                log_level);
            ret |= GoalProblem::UNSUPPORTED_ACTION;
            continue;
        }
        sack::QueryCmp cmp = settings.get_ignore_case() ? sack::QueryCmp::IGLOB : sack::QueryCmp::GLOB;
        bool spec_resolved{false};

        // Get query of groups which match the spec for the given action.
        comps::GroupQuery group_query(base, true);
        if (settings.get_group_search_groups()) {
            comps::GroupQuery spec_groups_query(base_groups_query);
            // for REMOVE / UPGRADE actions take only installed groups into account
            // for INSTALL only available groups
            spec_groups_query.filter_installed(action != GoalAction::INSTALL && action != GoalAction::INSTALL_BY_COMPS);
            if (settings.get_group_with_id()) {
                comps::GroupQuery group_query_id(spec_groups_query);
                group_query_id.filter_groupid(spec, cmp);
                group_query |= group_query_id;
            }
            // TODO(mblaha): reconsider usefulness of searching groups by names
            if (settings.get_group_with_name()) {
                comps::GroupQuery group_query_name(spec_groups_query);
                group_query_name.filter_name(spec, cmp);
                group_query |= group_query_name;
            }
        }
        // Get query of environments which match the spec for the given action.
        comps::EnvironmentQuery environment_query(base, true);
        if (settings.get_group_search_environments()) {
            comps::EnvironmentQuery spec_environments_query(base_environments_query);
            spec_environments_query.filter_installed(action != GoalAction::INSTALL);
            if (settings.get_group_with_id()) {
                comps::EnvironmentQuery environment_query_id(spec_environments_query);
                environment_query_id.filter_environmentid(spec, cmp);
                environment_query |= environment_query_id;
            }
            // TODO(mblaha): reconsider usefulness of searching groups by names
            if (settings.get_group_with_name()) {
                comps::EnvironmentQuery environment_query_name(spec_environments_query);
                environment_query_name.filter_name(spec, cmp);
                environment_query |= environment_query_name;
            }
        }

        // Use the environment query only if environments are preffered or if the spec didn't match any groups.
        if (!environment_query.empty() &&
            (settings.get_comps_type_preferred() != CompsTypePreferred::GROUP || group_query.empty())) {
            resolved_environment_specs[action].push_back({spec, std::move(environment_query), settings});
            spec_resolved = true;
        }

        // Use the group query only if groups are preffered or if the spec didn't match any environments.
        if (!group_query.empty() &&
            (settings.get_comps_type_preferred() != CompsTypePreferred::ENVIRONMENT || !spec_resolved)) {
            comps::GroupQuery already_handled_groups(base, true);
            // Check if there are other actions for selected groups,
            // we don't want to have multiple actions per one group id.
            for (const auto & group : group_query) {
                for (auto & [key_action, value_group_items] : resolved_group_specs) {
                    for (auto & group_item : value_group_items) {
                        auto & group_q = std::get<comps::GroupQuery>(group_item);
                        // We cannot simply compare the groups because they can have different libsolv ids,
                        // we have to compare them by groupid.
                        auto group_q_copy = group_q;
                        group_q_copy.filter_groupid(group.get_groupid());
                        if (!group_q_copy.empty()) {
                            // If we have multiple different actions per group it always ends up as upgrade.
                            // This is because there are only 3 actions: INSTALL (together with INSTALL_BY_COMPS),
                            // UPGRADE and REMOVE, any two of them mean an UPGRADE.
                            // (Given that groups are not versioned the UPGRADE action basically means synchronization
                            //  with currently loaded metadata.)
                            //  TODO(amatej): When we have REMOVE and INSTALL the behavior doesn't match doing the actions separately,
                            //                consider adding REINSTALL action.
                            if (action != key_action && key_action != GoalAction::UPGRADE) {
                                group_q -= group_q_copy;
                                // INSTALL and INSTALL_BY_COMPS should result in INSTALL instead of UPGRADE.
                                if ((action == GoalAction::INSTALL && key_action == GoalAction::INSTALL_BY_COMPS) ||
                                    (action == GoalAction::INSTALL_BY_COMPS && key_action == GoalAction::INSTALL)) {
                                    action = GoalAction::INSTALL;
                                } else {
                                    action = GoalAction::UPGRADE;
                                }
                            } else {
                                // If there already is this action for this group set only the stronger reason
                                auto & already_present_reason =
                                    std::get<transaction::TransactionItemReason>(group_item);
                                if (already_present_reason < reason) {
                                    already_present_reason = reason;
                                }
                                already_handled_groups.add(group);
                                spec_resolved = true;
                            }
                        }
                    }
                }
            }

            group_query -= already_handled_groups;

            if (!group_query.empty()) {
                resolved_group_specs[action].push_back({spec, reason, std::move(group_query), settings});
                spec_resolved = true;
            }
        }

        if (!spec_resolved) {
            transaction.p_impl->add_resolve_log(
                action,
                GoalProblem::NOT_FOUND,
                settings,
                libdnf5::transaction::TransactionItemType::GROUP,
                spec,
                {},
                log_level);
            if (!skip_unavailable) {
                ret |= GoalProblem::NOT_FOUND;
            }
        }
    }

    return ret;
}

void Goal::Impl::add_resolved_environment_specs_to_goal(base::Transaction & transaction) {
    add_environment_remove_to_goal(transaction, resolved_environment_specs[GoalAction::REMOVE]);

    for (auto & [spec, environment_query, settings] : resolved_environment_specs[GoalAction::INSTALL]) {
        add_environment_install_to_goal(transaction, environment_query, settings);
    }

    for (auto & [spec, environment_query, settings] : resolved_environment_specs[GoalAction::UPGRADE]) {
        add_environment_upgrade_to_goal(transaction, environment_query, settings);
    }
}

void Goal::Impl::add_resolved_group_specs_to_goal(base::Transaction & transaction) {
    // process group removals first
    add_group_remove_to_goal(resolved_group_specs[GoalAction::REMOVE]);

    for (const auto & action : std::vector<GoalAction>{GoalAction::INSTALL, GoalAction::INSTALL_BY_COMPS}) {
        for (auto & [spec, reason, group_query, settings] : resolved_group_specs[action]) {
            add_group_install_to_goal(transaction, reason, group_query, settings);
        }
    }

    for (auto & [spec, reason, group_query, settings] : resolved_group_specs[GoalAction::UPGRADE]) {
        add_group_upgrade_to_goal(transaction, group_query, settings);
    }
}


GoalProblem Goal::Impl::add_reason_change_specs_to_goal(base::Transaction & transaction) {
    auto ret = GoalProblem::NO_PROBLEM;
    for (auto & [reason, spec, group_id, settings] : rpm_reason_change_specs) {
        ret |= add_reason_change_to_goal(transaction, spec, reason, group_id, settings);
    }
    return ret;
}

std::pair<GoalProblem, libdnf5::solv::IdQueue> Goal::Impl::add_install_to_goal(
    base::Transaction & transaction, GoalAction action, const std::string & spec, GoalJobSettings & settings) {
    auto sack = base->get_rpm_package_sack();
    auto & pool = get_rpm_pool(base);
    auto & cfg_main = base->get_config();
    bool skip_unavailable = settings.resolve_skip_unavailable(cfg_main);
    auto log_level = skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR;
    bool best = settings.resolve_best(cfg_main);
    bool clean_requirements_on_remove = settings.resolve_clean_requirements_on_remove();

    auto multilib_policy = cfg_main.get_multilib_policy_option().get_value();
    libdnf5::solv::IdQueue result_queue;
    rpm::PackageQuery base_query(base);

    rpm::PackageQuery query(base_query);
    auto nevra_pair = query.resolve_pkg_spec(spec, settings, false);
    if (!nevra_pair.first) {
        auto problem = transaction.p_impl->report_not_found(action, spec, settings, log_level);
        if (skip_unavailable) {
            return {GoalProblem::NO_PROBLEM, result_queue};
        } else {
            return {problem, result_queue};
        }
    }

    const auto & to_vendors = settings.get_to_vendors();

    // The correct evaluation of rich dependencies can be only performed by solver.
    // There are some limitations - solver is unable to handle when operation is limited to packages from the
    // particular repository and multilib_policy `all`.
    if (libdnf5::rpm::Reldep::is_rich_dependency(spec) && settings.get_to_repo_ids().empty() && to_vendors.empty()) {
        add_provide_install_to_goal(spec, settings);
        return {GoalProblem::NO_PROBLEM, result_queue};
    }

    rpm::PackageQuery installed(query);
    installed.filter_installed();

    if (!settings.get_to_repo_ids().empty()) {
        query.filter_repo_id(settings.get_to_repo_ids(), sack::QueryCmp::GLOB);
        if (query.empty()) {
            transaction.p_impl->add_resolve_log(
                action,
                GoalProblem::NOT_FOUND_IN_REPOSITORIES,
                settings,
                libdnf5::transaction::TransactionItemType::PACKAGE,
                spec,
                {},
                log_level);
            return {skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_FOUND_IN_REPOSITORIES, result_queue};
        }

        // We require packages only from the to_repo_ids repositories. We'll select only the corresponding installed
        // packages (matching name and architecture, or the same NEVRA for install-only packages)
        installed.filter_name_arch(query);
        rpm::PackageQuery installed_install_only(installed);
        installed_install_only.filter_installonly();
        installed -= installed_install_only;
        installed_install_only.filter_nevra(query);
        installed |= installed_install_only;

        // Return the corresponding installed packages to the query
        // TODO(jrohel): Is it needed?
        query |= installed;
    }

    if (!to_vendors.empty()) {
        query.filter_vendor(to_vendors, sack::QueryCmp::GLOB);
        if (query.empty()) {
            transaction.p_impl->add_resolve_log(
                action,
                GoalProblem::NOT_FOUND_IN_REPOSITORIES,  // TODO(jrohel): NOT_FOUND_FROM_VENDOR
                settings,
                libdnf5::transaction::TransactionItemType::PACKAGE,
                spec,
                {},
                log_level);
            return {skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_FOUND_IN_REPOSITORIES, result_queue};
        }

        // We require packages only from the to_vendors vendors. We'll select only the corresponding installed
        // packages (matching name and architecture, or the same NEVRA for install-only packages)
        installed.filter_name_arch(query);
        rpm::PackageQuery installed_install_only(installed);
        installed_install_only.filter_installonly();
        installed -= installed_install_only;
        installed_install_only.filter_nevra(query);
        installed |= installed_install_only;

        // Return the corresponding installed packages to the query
        // TODO(jrohel): Is it needed?
        query |= installed;
    }

    bool has_just_name = nevra_pair.second.has_just_name();
    bool add_obsoletes = cfg_main.get_obsoletes_option().get_value() && has_just_name;

    for (auto package_id : *installed.p_impl) {
        transaction.p_impl->add_resolve_log(
            action,
            GoalProblem::ALREADY_INSTALLED,
            settings,
            libdnf5::transaction::TransactionItemType::PACKAGE,
            spec,
            {pool.get_nevra(package_id)},
            libdnf5::Logger::Level::WARNING);
    }

    if (!to_vendors.empty()) {
        if (settings.get_advisory_filter() != nullptr) {
            rpm::PackageQuery tmp_query(query);
            tmp_query.filter_advisories(*settings.get_advisory_filter(), libdnf5::sack::QueryCmp::EQ);
            incoming_vendor_bypassed_solvables |= *tmp_query.p_impl;
        } else {
            incoming_vendor_bypassed_solvables |= *query.p_impl;
        }
    }

    bool skip_broken = settings.resolve_skip_broken(cfg_main);

    if (multilib_policy == "all" || utils::is_glob_pattern(nevra_pair.second.get_arch().c_str())) {
        // Apply advisory filters
        if (settings.get_advisory_filter() != nullptr) {
            query.filter_advisories(*settings.get_advisory_filter(), libdnf5::sack::QueryCmp::EQ);
            if (query.empty()) {
                transaction.p_impl->add_resolve_log(
                    action,
                    GoalProblem::NOT_FOUND_IN_ADVISORIES,
                    settings,
                    libdnf5::transaction::TransactionItemType::PACKAGE,
                    spec,
                    {},
                    log_level);
                return {
                    skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_FOUND_IN_ADVISORIES, result_queue};
            }
        }

        /// <name, <arch, std::vector<pkg Solvables>>>
        std::unordered_map<Id, std::unordered_map<Id, std::vector<Solvable *>>> na_map;

        for (auto package_id : *query.p_impl) {
            Solvable * solvable = pool.id2solvable(package_id);
            na_map[solvable->name][solvable->arch].push_back(solvable);
        }

        rpm::PackageSet selected(base);
        rpm::PackageSet selected_noarch(base);
        for (auto & name_iter : na_map) {
            if (name_iter.second.size() == 1) {
                selected.clear();
                for (auto * solvable : name_iter.second.begin()->second) {
                    selected.p_impl->add(pool.solvable2id(solvable));
                }
                if (add_obsoletes) {
                    add_obsoletes_to_data(base_query, selected);
                }
                solv_map_to_id_queue(result_queue, *selected.p_impl);
                rpm_goal.add_install(result_queue, skip_broken, best, clean_requirements_on_remove);
            } else {
                // when multiple architectures -> add noarch solvables into each architecture solvable set
                auto noarch = name_iter.second.find(ARCH_NOARCH);
                if (noarch != name_iter.second.end()) {
                    selected_noarch.clear();
                    for (auto * solvable : noarch->second) {
                        selected_noarch.p_impl->add(pool.solvable2id(solvable));
                    }
                    if (add_obsoletes) {
                        add_obsoletes_to_data(base_query, selected_noarch);
                    }
                    for (auto & arch_iter : name_iter.second) {
                        if (arch_iter.first == ARCH_NOARCH) {
                            continue;
                        }
                        selected.clear();
                        for (auto * solvable : arch_iter.second) {
                            selected.p_impl->add(pool.solvable2id(solvable));
                        }
                        if (add_obsoletes) {
                            add_obsoletes_to_data(base_query, selected);
                        }
                        selected |= selected_noarch;
                        solv_map_to_id_queue(result_queue, *selected.p_impl);
                        rpm_goal.add_install(result_queue, skip_broken, best, clean_requirements_on_remove);
                    }
                } else {
                    for (auto & arch_iter : name_iter.second) {
                        selected.clear();
                        for (auto * solvable : arch_iter.second) {
                            selected.p_impl->add(pool.solvable2id(solvable));
                        }
                        if (add_obsoletes) {
                            add_obsoletes_to_data(base_query, selected);
                        }
                        solv_map_to_id_queue(result_queue, *selected.p_impl);
                        rpm_goal.add_install(result_queue, skip_broken, best, clean_requirements_on_remove);
                    }
                }
            }
        }
        // TODO(jmracek) Implement all logic for modules and comps groups
    } else if (multilib_policy == "best") {
        if ((!utils::is_file_pattern(spec) && utils::is_glob_pattern(spec.c_str())) ||
            (nevra_pair.second.get_name().empty() &&
             (!nevra_pair.second.get_epoch().empty() || !nevra_pair.second.get_version().empty() ||
              !nevra_pair.second.get_release().empty() || !nevra_pair.second.get_arch().empty()))) {
            // Apply advisory filters
            if (settings.get_advisory_filter() != nullptr) {
                query.filter_advisories(*settings.get_advisory_filter(), libdnf5::sack::QueryCmp::EQ);
                if (query.empty()) {
                    transaction.p_impl->add_resolve_log(
                        action,
                        GoalProblem::NOT_FOUND_IN_ADVISORIES,
                        settings,
                        libdnf5::transaction::TransactionItemType::PACKAGE,
                        spec,
                        {},
                        log_level);
                    return {
                        skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_FOUND_IN_ADVISORIES,
                        result_queue};
                }
            }

            rpm::PackageQuery available(query);
            available.filter_available();

            // keep only installed that has a partner in available
            std::unordered_set<Id> names;
            for (auto package_id : *available.p_impl) {
                Solvable * solvable = pool.id2solvable(package_id);
                names.insert(solvable->name);
            }
            for (auto package_id : *installed.p_impl) {
                Solvable * solvable = pool.id2solvable(package_id);
                auto name_iterator = names.find(solvable->name);
                if (name_iterator == names.end()) {
                    installed.p_impl->remove_unsafe(package_id);
                }
            }
            available |= installed;
            std::vector<Solvable *> tmp_solvables;
            for (auto package_id : *available.p_impl) {
                Solvable * solvable = pool.id2solvable(package_id);
                tmp_solvables.push_back(solvable);
            }

            rpm::PackageSet selected(base);
            if (!tmp_solvables.empty()) {
                Id current_name = 0;
                std::sort(tmp_solvables.begin(), tmp_solvables.end(), nevra_solvable_cmp_key);
                auto * first = tmp_solvables[0];
                current_name = first->name;
                selected.p_impl->add_unsafe(pool.solvable2id(first));

                for (auto iter = std::next(tmp_solvables.begin()); iter != tmp_solvables.end(); ++iter) {
                    if ((*iter)->name == current_name) {
                        selected.p_impl->add_unsafe(pool.solvable2id(*iter));
                        continue;
                    }
                    if (add_obsoletes) {
                        add_obsoletes_to_data(base_query, selected);
                    }
                    solv_map_to_id_queue(result_queue, static_cast<libdnf5::solv::SolvMap>(*selected.p_impl));
                    rpm_goal.add_install(result_queue, skip_broken, best, clean_requirements_on_remove);
                    selected.clear();
                    selected.p_impl->add_unsafe(pool.solvable2id(*iter));
                    current_name = (*iter)->name;
                }
            }
            if (add_obsoletes) {
                add_obsoletes_to_data(base_query, selected);
            }
            solv_map_to_id_queue(result_queue, static_cast<libdnf5::solv::SolvMap>(*selected.p_impl));
            rpm_goal.add_install(result_queue, skip_broken, best, clean_requirements_on_remove);
        } else {
            if (add_obsoletes) {
                add_obsoletes_to_data(base_query, query);
            }

            // Apply advisory filters
            if (settings.get_advisory_filter() != nullptr) {
                query.filter_advisories(*settings.get_advisory_filter(), libdnf5::sack::QueryCmp::EQ);
                if (query.empty()) {
                    transaction.p_impl->add_resolve_log(
                        action,
                        GoalProblem::NOT_FOUND_IN_ADVISORIES,
                        settings,
                        libdnf5::transaction::TransactionItemType::PACKAGE,
                        spec,
                        {},
                        log_level);
                    return {
                        skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_FOUND_IN_ADVISORIES,
                        result_queue};
                }
            }
            solv_map_to_id_queue(result_queue, *query.p_impl);
            rpm_goal.add_install(result_queue, skip_broken, best, clean_requirements_on_remove);
        }
    } else {
        // TODO(lukash) throw a proper exception
        throw RuntimeError(M_("Incorrect configuration value for multilib_policy: {}"), multilib_policy);
    }

    return {GoalProblem::NO_PROBLEM, result_queue};
}

std::pair<GoalProblem, libdnf5::solv::IdQueue> Goal::Impl::add_install_debug_to_goal(
    base::Transaction & transaction, const std::string & spec, GoalJobSettings & settings) {
    auto & cfg_main = base->get_config();
    bool skip_unavailable = settings.resolve_skip_unavailable(cfg_main);
    auto log_level = skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR;
    bool best = settings.resolve_best(cfg_main);
    bool clean_requirements_on_remove = settings.resolve_clean_requirements_on_remove();
    bool skip_broken = settings.resolve_skip_broken(cfg_main);

    libdnf5::solv::IdQueue result_queue;

    rpm::PackageQuery query(base);
    auto nevra_pair = query.resolve_pkg_spec(spec, settings, false);
    if (!nevra_pair.first) {
        auto problem = transaction.p_impl->report_not_found(GoalAction::INSTALL_DEBUG, spec, settings, log_level);
        if (skip_unavailable) {
            return {GoalProblem::NO_PROBLEM, result_queue};
        } else {
            return {problem, result_queue};
        }
    }
    // Use a package name as a key
    std::unordered_map<std::string, std::vector<libdnf5::rpm::Package>> candidate_map;
    std::unordered_map<std::string, std::vector<libdnf5::rpm::Package>> available;
    for (auto package : query) {
        if (package.is_installed()) {
            candidate_map[package.get_name()].push_back(package);
        } else {
            available[package.get_name()].push_back(package);
        }
    }
    // installed versions of packages have priority, replace / add them to the m
    candidate_map.merge(available);

    const std::string debug_suffix{"-debuginfo"};
    const std::string debug_source_suffix{"-debugsource"};

    // Remove debuginfo packages if their base packages are in the query.
    // They can get there through globs and they break the installation
    // of debug packages with the same version as the installed base
    // packages. If the base package of a debuginfo package is not in
    // the query, the user specified a debug package on the command
    // line. We don't want to ignore those, so we will install them.
    // But, in this case the version will not be matched to the
    // installed version of the base package, as that would require
    // another query and is further complicated if the user specifies a
    // version themselves etc.
    for (auto iter = candidate_map.begin(); iter != candidate_map.end();) {
        auto name = iter->first;
        if (libdnf5::utils::string::ends_with(name, debug_suffix)) {
            name.resize(name.size() - debug_suffix.size());
            auto iterator = candidate_map.find(name);
            if (iterator != candidate_map.end()) {
                // remove debuginfo when base name is in candidate_map
                candidate_map.erase(iter++);
                continue;
            }
            // Install debuginfo and remove it from candiddates (to prevent double testing)
            libdnf5::solv::IdQueue install_queue;
            for (auto package : iter->second) {
                Id pkg_id = package.get_id().id;
                install_queue.push_back(pkg_id);
                result_queue.push_back(pkg_id);
            }
            rpm_goal.add_install(install_queue, skip_broken, best, clean_requirements_on_remove);
            candidate_map.erase(iter++);
            continue;
        } else if (libdnf5::utils::string::ends_with(name, debug_source_suffix)) {
            name.resize(name.size() - debug_source_suffix.size());
            auto iterator = candidate_map.find(name);
            if (iterator != candidate_map.end()) {
                candidate_map.erase(iter++);
                continue;
            }
            // Install debugsource and remove it from candiddates (to prevent double testing)
            libdnf5::solv::IdQueue install_queue;
            for (auto package : iter->second) {
                Id pkg_id = package.get_id().id;
                install_queue.push_back(pkg_id);
                result_queue.push_back(pkg_id);
            }
            rpm_goal.add_install(install_queue, skip_broken, best, clean_requirements_on_remove);
            candidate_map.erase(iter++);
            continue;
        }
        ++iter;
    }

    std::set<std::string> no_debuginfo_for_packages;
    std::set<std::string> no_debugsource_for_packages;
    for (auto & item : candidate_map) {
        auto & first_pkg = *item.second.begin();
        auto debug_name = first_pkg.get_debuginfo_name();
        auto debuginfo_name_of_source = first_pkg.get_debuginfo_name_of_source();
        auto debug_source_name = first_pkg.get_debugsource_name();

        if (first_pkg.is_installed()) {
            std::unordered_map<std::string, std::vector<libdnf5::rpm::Package>> arch_map;
            for (auto & package : item.second) {
                arch_map[package.get_arch()].push_back(package);
            }
            for (auto arch_item : arch_map) {
                if (!install_debug_from_packages(
                        base,
                        debug_name,
                        arch_item.second,
                        result_queue,
                        rpm_goal,
                        skip_broken,
                        best,
                        clean_requirements_on_remove)) {
                    // Because there is no debuginfo for the package, lets install deguginfo of the source package
                    if (!install_debug_from_packages(
                            base,
                            debuginfo_name_of_source,
                            arch_item.second,
                            result_queue,
                            rpm_goal,
                            skip_broken,
                            best,
                            clean_requirements_on_remove)) {
                        for (auto & package : arch_item.second) {
                            no_debuginfo_for_packages.emplace(package.get_full_nevra());
                        }
                    }
                }
                if (!install_debug_from_packages(
                        base,
                        debug_source_name,
                        arch_item.second,
                        result_queue,
                        rpm_goal,
                        skip_broken,
                        best,
                        clean_requirements_on_remove)) {
                    for (auto & package : arch_item.second) {
                        no_debugsource_for_packages.emplace(package.get_full_nevra());
                    }
                }
            }
            continue;
        }
        if (!install_debug_from_packages(
                base,
                debug_name,
                item.second,
                result_queue,
                rpm_goal,
                skip_broken,
                best,
                clean_requirements_on_remove)) {
            // Because there is no debuginfo for the package, lets install deguginfo of the source package
            if (!install_debug_from_packages(
                    base,
                    debuginfo_name_of_source,
                    item.second,
                    result_queue,
                    rpm_goal,
                    skip_broken,
                    best,
                    clean_requirements_on_remove)) {
                for (auto & package : item.second) {
                    no_debuginfo_for_packages.emplace(package.get_full_nevra());
                }
            }
        }
        if (!install_debug_from_packages(
                base,
                debug_source_name,
                item.second,
                result_queue,
                rpm_goal,
                skip_broken,
                best,
                clean_requirements_on_remove)) {
            for (auto & package : item.second) {
                no_debugsource_for_packages.emplace(package.get_full_nevra());
            }
        }
    }
    if (!no_debuginfo_for_packages.empty()) {
        transaction.p_impl->add_resolve_log(
            GoalAction::INSTALL_DEBUG,
            GoalProblem::NOT_FOUND_DEBUGINFO,
            settings,
            libdnf5::transaction::TransactionItemType::PACKAGE,
            spec,
            no_debuginfo_for_packages,
            libdnf5::Logger::Level::WARNING);
    }
    if (!no_debugsource_for_packages.empty()) {
        transaction.p_impl->add_resolve_log(
            GoalAction::INSTALL_DEBUG,
            GoalProblem::NOT_FOUND_DEBUGSOURCE,
            settings,
            libdnf5::transaction::TransactionItemType::PACKAGE,
            spec,
            no_debugsource_for_packages,
            libdnf5::Logger::Level::WARNING);
    }
    return {GoalProblem::NO_PROBLEM, result_queue};
}

void Goal::Impl::add_provide_install_to_goal(const std::string & spec, GoalJobSettings & settings) {
    auto & cfg_main = base->get_config();
    bool skip_broken = settings.resolve_skip_broken(cfg_main);
    bool best = settings.resolve_best(cfg_main);
    bool clean_requirements_on_remove = settings.resolve_clean_requirements_on_remove();
    rpm::Reldep reldep(base, spec);
    rpm_goal.add_provide_install(reldep.get_id(), skip_broken, best, clean_requirements_on_remove);
}

GoalProblem Goal::Impl::add_reinstall_to_goal(
    base::Transaction & transaction, const std::string & spec, GoalJobSettings & settings) {
    // Resolve all settings before the first report => they will be storred in settings
    auto & cfg_main = base->get_config();
    bool skip_unavailable = settings.resolve_skip_unavailable(cfg_main);
    auto log_level = skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR;
    bool best = settings.resolve_best(cfg_main);
    bool clean_requirements_on_remove = settings.resolve_clean_requirements_on_remove();
    auto sack = base->get_rpm_package_sack();
    rpm::PackageQuery query(base);
    auto nevra_pair = query.resolve_pkg_spec(spec, settings, false);
    if (!nevra_pair.first) {
        auto problem = transaction.p_impl->report_not_found(GoalAction::REINSTALL, spec, settings, log_level);
        return skip_unavailable ? GoalProblem::NO_PROBLEM : problem;
    }

    // Report when package is not installed
    rpm::PackageQuery query_installed(query);
    query_installed.filter_installed();
    if (query_installed.empty()) {
        transaction.p_impl->add_resolve_log(
            GoalAction::REINSTALL,
            GoalProblem::NOT_INSTALLED,
            settings,
            libdnf5::transaction::TransactionItemType::PACKAGE,
            spec,
            {},
            log_level);
        return skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_INSTALLED;
    }

    // keep only available packages
    query -= query_installed;

    // filtering from_repo_ids
    if (!settings.get_from_repo_ids().empty()) {
        query_installed.filter_from_repo_id(settings.get_from_repo_ids(), sack::QueryCmp::GLOB);
        // Report when package is not installed from repositories
        if (query_installed.empty()) {
            // TODO(jrohel) no solution for the spec => mark result - not from repository
            transaction.p_impl->add_resolve_log(
                GoalAction::REINSTALL,
                GoalProblem::NOT_INSTALLED,
                settings,
                libdnf5::transaction::TransactionItemType::PACKAGE,
                spec,
                {},
                log_level);
            return skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_INSTALLED;
        }
    }

    if (query.empty()) {
        transaction.p_impl->add_resolve_log(
            GoalAction::REINSTALL,
            GoalProblem::NOT_AVAILABLE,
            settings,
            libdnf5::transaction::TransactionItemType::PACKAGE,
            spec,
            {},
            log_level);
        return skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_AVAILABLE;
    }

    // keeps only available packages that are installed with same NEVRA
    rpm::PackageQuery relevant_available(query);
    relevant_available.filter_nevra(query_installed);
    if (relevant_available.empty()) {
        rpm::PackageQuery relevant_available_na(query);
        relevant_available_na.filter_name_arch(query_installed);
        if (!relevant_available_na.empty()) {
            transaction.p_impl->add_resolve_log(
                GoalAction::REINSTALL,
                GoalProblem::INSTALLED_IN_DIFFERENT_VERSION,
                settings,
                libdnf5::transaction::TransactionItemType::PACKAGE,
                spec,
                query_to_vec_of_nevra_str(relevant_available_na),
                log_level);
            return skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::INSTALLED_IN_DIFFERENT_VERSION;
        } else {
            rpm::PackageQuery relevant_available_n(query);
            relevant_available_n.filter_name(query_installed);
            if (relevant_available_n.empty()) {
                transaction.p_impl->add_resolve_log(
                    GoalAction::REINSTALL,
                    GoalProblem::NOT_INSTALLED,
                    settings,
                    libdnf5::transaction::TransactionItemType::PACKAGE,
                    spec,
                    {},
                    log_level);
                return skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_INSTALLED;
            } else {
                transaction.p_impl->add_resolve_log(
                    GoalAction::REINSTALL,
                    GoalProblem::NOT_INSTALLED_FOR_ARCHITECTURE,
                    settings,
                    libdnf5::transaction::TransactionItemType::PACKAGE,
                    spec,
                    {},
                    log_level);
                return skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_INSTALLED_FOR_ARCHITECTURE;
            }
        }
    }

    if (!settings.get_to_repo_ids().empty()) {
        relevant_available.filter_repo_id(settings.get_to_repo_ids(), sack::QueryCmp::GLOB);
        if (relevant_available.empty()) {
            transaction.p_impl->add_resolve_log(
                GoalAction::REINSTALL,
                GoalProblem::NOT_FOUND_IN_REPOSITORIES,
                settings,
                libdnf5::transaction::TransactionItemType::PACKAGE,
                spec,
                {},
                log_level);
            return skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_FOUND_IN_REPOSITORIES;
        }
    }

    const auto & to_vendors = settings.get_to_vendors();

    if (!to_vendors.empty()) {
        relevant_available.filter_vendor(to_vendors, sack::QueryCmp::GLOB);
        if (relevant_available.empty()) {
            transaction.p_impl->add_resolve_log(
                GoalAction::REINSTALL,
                GoalProblem::NOT_FOUND_IN_REPOSITORIES,  // TODO(jrohel): NOT_FOUND_FROM_VENDOR
                settings,
                libdnf5::transaction::TransactionItemType::PACKAGE,
                spec,
                {},
                log_level);
            return skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_FOUND_IN_REPOSITORIES;
        }
    }

    if (!to_vendors.empty()) {
        incoming_vendor_bypassed_solvables |= *query.p_impl;
    }

    Id current_name = 0;
    Id current_arch = 0;
    std::vector<Solvable *> tmp_solvables;
    auto & pool = get_rpm_pool(base);

    for (auto package_id : *relevant_available.p_impl) {
        tmp_solvables.push_back(pool.id2solvable(package_id));
    }
    std::sort(tmp_solvables.begin(), tmp_solvables.end(), nevra_solvable_cmp_key);

    libdnf5::solv::IdQueue tmp_queue;

    {
        auto * first = (*tmp_solvables.begin());
        current_name = first->name;
        current_arch = first->arch;
        tmp_queue.push_back(pool.solvable2id(first));
    }

    bool skip_broken = settings.resolve_skip_broken(cfg_main);

    for (auto iter = std::next(tmp_solvables.begin()); iter != tmp_solvables.end(); ++iter) {
        if ((*iter)->name == current_name && (*iter)->arch == current_arch) {
            tmp_queue.push_back(pool.solvable2id(*iter));
            continue;
        }
        rpm_goal.add_install(tmp_queue, skip_broken, best, clean_requirements_on_remove);
        tmp_queue.clear();
        tmp_queue.push_back(pool.solvable2id(*iter));
        current_name = (*iter)->name;
        current_arch = (*iter)->arch;
    }
    rpm_goal.add_install(tmp_queue, skip_broken, best, clean_requirements_on_remove);
    return GoalProblem::NO_PROBLEM;
}

void Goal::Impl::add_rpms_to_goal(base::Transaction & transaction) {
    auto sack = base->get_rpm_package_sack();
    auto & pool = get_rpm_pool(base);
    auto & cfg_main = base->get_config();

    rpm::PackageQuery installed(base, rpm::PackageQuery::ExcludeFlags::IGNORE_EXCLUDES);
    installed.filter_installed();
    for (auto [action, ids, settings] : rpm_ids) {
        switch (action) {
            case GoalAction::INSTALL: {
                bool skip_broken = settings.resolve_skip_broken(cfg_main);
                bool skip_unavailable = settings.resolve_skip_unavailable(cfg_main);
                auto log_level = skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR;
                bool best = settings.resolve_best(cfg_main);
                bool clean_requirements_on_remove = settings.resolve_clean_requirements_on_remove();
                //  include installed packages with the same NEVRA into transaction to prevent reinstall
                std::vector<std::string> nevras;
                for (auto id : ids) {
                    nevras.push_back(pool.get_nevra(id));
                }
                rpm::PackageQuery query(installed);
                query.filter_nevra(nevras);
                //  report already installed packages with the same NEVRA
                for (auto package_id : *query.p_impl) {
                    transaction.p_impl->add_resolve_log(
                        action,
                        GoalProblem::ALREADY_INSTALLED,
                        settings,
                        libdnf5::transaction::TransactionItemType::PACKAGE,
                        {},
                        {pool.get_nevra(package_id)},
                        log_level);
                    ids.push_back(package_id);
                }
                rpm_goal.add_install(ids, skip_broken, best, clean_requirements_on_remove);
                rpm_goal.add_transaction_user_installed(ids);
            } break;
            case GoalAction::INSTALL_OR_REINSTALL:
                rpm_goal.add_install(
                    ids,
                    settings.resolve_skip_broken(cfg_main),
                    settings.resolve_best(cfg_main),
                    settings.resolve_clean_requirements_on_remove());
                break;
            case GoalAction::REINSTALL: {
                bool skip_unavailable = settings.resolve_skip_unavailable(cfg_main);
                auto log_level = skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR;
                bool skip_broken = settings.resolve_skip_broken(cfg_main);
                bool best = settings.resolve_best(cfg_main);
                bool clean_requirements_on_remove = settings.resolve_clean_requirements_on_remove();
                solv::IdQueue ids_nevra_installed;
                for (auto id : ids) {
                    rpm::PackageQuery query(installed);
                    query.filter_nevra(pool.get_nevra(id));
                    if (query.empty()) {
                        // Report when package with the same NEVRA is not installed
                        transaction.p_impl->add_resolve_log(
                            action,
                            GoalProblem::NOT_INSTALLED,
                            settings,
                            libdnf5::transaction::TransactionItemType::PACKAGE,
                            {pool.get_nevra(id)},
                            {},
                            log_level);
                    } else {
                        // Only installed packages can be reinstalled
                        ids_nevra_installed.push_back(id);
                    }
                }
                rpm_goal.add_install(ids_nevra_installed, skip_broken, best, clean_requirements_on_remove);
            } break;
            case GoalAction::UPGRADE: {
                bool best = settings.resolve_best(cfg_main);
                bool clean_requirements_on_remove = settings.resolve_clean_requirements_on_remove();
                // TODO(jrohel): Now logs all packages that are not upgrades. It can be confusing in some cases.
                for (auto id : ids) {
                    if (cfg_main.get_obsoletes_option().get_value()) {
                        rpm::PackageQuery query_id(base, rpm::PackageQuery::ExcludeFlags::IGNORE_EXCLUDES, true);
                        query_id.add(rpm::Package(base, rpm::PackageId(id)));
                        query_id.filter_obsoletes(installed);
                        if (!query_id.empty()) {
                            continue;
                        }
                    }
                    rpm::PackageQuery query(installed);
                    query.filter_name(pool.get_name(id));
                    if (query.empty()) {
                        // Report when package with the same name is not installed
                        transaction.p_impl->add_resolve_log(
                            action,
                            GoalProblem::NOT_INSTALLED,
                            settings,
                            libdnf5::transaction::TransactionItemType::PACKAGE,
                            {pool.get_nevra(id)},
                            {},
                            libdnf5::Logger::Level::WARNING);
                        continue;
                    }
                    std::string arch = pool.get_arch(id);
                    if (arch != "noarch") {
                        query.filter_arch({arch, "noarch"});
                        if (query.empty()) {
                            // Report when package with the same name is installed for a different architecture
                            // Conversion from/to "noarch" is allowed for upgrade.
                            transaction.p_impl->add_resolve_log(
                                action,
                                GoalProblem::NOT_INSTALLED_FOR_ARCHITECTURE,
                                settings,
                                libdnf5::transaction::TransactionItemType::PACKAGE,
                                {pool.get_nevra(id)},
                                {},
                                libdnf5::Logger::Level::WARNING);
                            continue;
                        }
                    }
                    query.filter_evr(pool.get_evr(id), sack::QueryCmp::GTE);
                    if (!query.empty()) {
                        // Report when package with higher or equal version is installed
                        transaction.p_impl->add_resolve_log(
                            action,
                            GoalProblem::ALREADY_INSTALLED,
                            settings,
                            libdnf5::transaction::TransactionItemType::PACKAGE,
                            {pool.get_nevra(id)},
                            {pool.get_name(id) + ("." + arch)},
                            libdnf5::Logger::Level::WARNING);
                        // include installed packages with higher or equal version into transaction to prevent downgrade
                        for (auto installed_id : *query.p_impl) {
                            ids.push_back(installed_id);
                        }
                    }
                }
                rpm_goal.add_upgrade(ids, best, clean_requirements_on_remove);
            } break;
            case GoalAction::DOWNGRADE: {
                bool skip_unavailable = settings.resolve_skip_unavailable(cfg_main);
                auto log_level = skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR;
                bool skip_broken = settings.resolve_skip_broken(cfg_main);
                bool best = settings.resolve_best(cfg_main);
                bool clean_requirements_on_remove = settings.resolve_clean_requirements_on_remove();
                solv::IdQueue ids_downgrades;
                for (auto id : ids) {
                    rpm::PackageQuery query(installed);
                    query.filter_name(pool.get_name(id));
                    if (query.empty()) {
                        // Report when package with the same name is not installed
                        transaction.p_impl->add_resolve_log(
                            action,
                            GoalProblem::NOT_INSTALLED,
                            settings,
                            libdnf5::transaction::TransactionItemType::PACKAGE,
                            {pool.get_nevra(id)},
                            {},
                            log_level);
                        continue;
                    }
                    query.filter_arch(pool.get_arch(id));
                    if (query.empty()) {
                        // Report when package with the same name is installed for a different architecture
                        transaction.p_impl->add_resolve_log(
                            action,
                            GoalProblem::NOT_INSTALLED_FOR_ARCHITECTURE,
                            settings,
                            libdnf5::transaction::TransactionItemType::PACKAGE,
                            {pool.get_nevra(id)},
                            {},
                            log_level);
                        continue;
                    }
                    query.filter_evr(pool.get_evr(id), sack::QueryCmp::LTE);
                    if (!query.empty()) {
                        // Report when package with lower or equal version is installed
                        std::string name_arch(pool.get_name(id));
                        name_arch.append(".");
                        name_arch.append(pool.get_arch(id));
                        transaction.p_impl->add_resolve_log(
                            action,
                            GoalProblem::INSTALLED_LOWEST_VERSION,
                            settings,
                            libdnf5::transaction::TransactionItemType::PACKAGE,
                            {pool.get_nevra(id)},
                            {name_arch},
                            log_level);
                        continue;
                    }

                    // Only installed packages with same name, architecture and higher version can be downgraded
                    ids_downgrades.push_back(id);
                }
                rpm_goal.add_install(ids_downgrades, skip_broken, best, clean_requirements_on_remove);
            } break;
            case GoalAction::DISTRO_SYNC: {
                rpm_goal.add_distro_sync(
                    ids,
                    settings.resolve_skip_broken(cfg_main),
                    settings.resolve_best(cfg_main),
                    settings.resolve_clean_requirements_on_remove());
            } break;
            case GoalAction::REMOVE:
                rpm_goal.add_remove(ids, settings.resolve_clean_requirements_on_remove(cfg_main));
                break;
            default:
                throw std::invalid_argument("Unsupported action");
        }
    }
}


GoalProblem Goal::Impl::add_remove_to_goal(
    base::Transaction & transaction, const std::string & spec, GoalJobSettings & settings) {
    bool clean_requirements_on_remove = settings.resolve_clean_requirements_on_remove(base->get_config());
    auto & cfg_main = base->get_config();
    const bool skip_unavailable =
        settings.get_skip_unavailable() == GoalSetting::AUTO ? true : settings.resolve_skip_unavailable(cfg_main);
    rpm::PackageQuery query(base);
    query.filter_installed();

    auto nevra_pair = query.resolve_pkg_spec(spec, settings, false);
    if (!nevra_pair.first) {
        auto problem = transaction.p_impl->report_not_found(
            GoalAction::REMOVE,
            spec,
            settings,
            skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR);
        return skip_unavailable ? GoalProblem::NO_PROBLEM : problem;
    }

    // filtering from_repo_ids
    if (!settings.get_from_repo_ids().empty()) {
        query.filter_from_repo_id(settings.get_from_repo_ids(), sack::QueryCmp::GLOB);
        // Report when package is not installed from repositories
        if (query.empty()) {
            // TODO(jrohel) no solution for the spec => mark result - not from repository
            transaction.p_impl->add_resolve_log(
                GoalAction::REMOVE,
                GoalProblem::NOT_INSTALLED,
                settings,
                libdnf5::transaction::TransactionItemType::PACKAGE,
                spec,
                {},
                skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR);
            return skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_INSTALLED;
        }
    }
    rpm_goal.add_remove(*query.p_impl, clean_requirements_on_remove);
    return GoalProblem::NO_PROBLEM;
}

GoalProblem Goal::Impl::add_up_down_distrosync_to_goal(
    base::Transaction & transaction,
    GoalAction action,
    const std::string & spec,
    GoalJobSettings & settings,
    bool minimal) {
    // Get values before the first report to set in GoalJobSettings used values
    const bool best = settings.resolve_best(base->get_config());
    const bool skip_broken = action == GoalAction::UPGRADE ? true : settings.resolve_skip_broken(base->get_config());
    const bool clean_requirements_on_remove = settings.resolve_clean_requirements_on_remove();
    const bool skip_unavailable = settings.resolve_skip_unavailable(base->get_config());

    auto sack = base->get_rpm_package_sack();
    rpm::PackageQuery base_query(base);
    auto obsoletes = base->get_config().get_obsoletes_option().get_value();
    libdnf5::solv::IdQueue tmp_queue;
    rpm::PackageQuery query(base_query);
    auto nevra_pair = query.resolve_pkg_spec(spec, settings, false);
    if (!nevra_pair.first) {
        auto problem = transaction.p_impl->report_not_found(action, spec, settings, libdnf5::Logger::Level::WARNING);
        return skip_unavailable ? GoalProblem::NO_PROBLEM : problem;
    }

    rpm::PackageQuery all_installed(base, rpm::PackageQuery::ExcludeFlags::IGNORE_EXCLUDES);
    all_installed.filter_installed();

    rpm::PackageQuery installed(all_installed);
    if (!settings.get_from_repo_ids().empty()) {
        installed.filter_from_repo_id(settings.get_from_repo_ids(), libdnf5::sack::QueryCmp::GLOB);
    }

    bool obsoleters = false;
    rpm::PackageQuery obsoleters_query(query);
    if (obsoletes && action != GoalAction::DOWNGRADE) {
        obsoleters_query.filter_obsoletes(installed);  // Keep only packages that obsolete some installed packages
        if (!obsoleters_query.empty()) {
            obsoleters = true;
        }
    }

    rpm::PackageQuery relevant_installed_n(installed);
    relevant_installed_n.filter_name(query);
    rpm::PackageQuery relevant_installed_na(installed);
    relevant_installed_na.filter_name_arch(query);

    // If from_repo_ids is defined, keep only the relevant packages in the query
    if (!settings.get_from_repo_ids().empty()) {
        rpm::PackageQuery relevant_all_installed_na(all_installed);
        relevant_all_installed_na.filter_name_arch(query);
        rpm::PackageQuery query_relevant_installed_name(query);

        // Keep in the query only those packages that have installed counterparts with the same name and architecture,
        // installed from repositories specified in from_repo_ids.
        query.filter_name_arch(relevant_installed_na);

        // Restore to the query those packages for which installed counterparts exist with the same name but
        // a different architecture, originating from repositories specified in from_repo_ids, and for which
        // no installed version with the desired architecture exists from a different repository
        query_relevant_installed_name.filter_name(relevant_installed_n);
        query_relevant_installed_name -= query;
        query_relevant_installed_name.filter_name_arch(relevant_all_installed_na, sack::QueryCmp::NEQ);
        query |= query_relevant_installed_name;

        // Restore to the query packages that obsolete those installed from repositories specified in from_repo_ids.
        if (obsoleters) {
            query |= obsoleters_query;
        }
    }

    // Report when package is not installed
    // Report only not installed if not obsoleters - https://bugzilla.redhat.com/show_bug.cgi?id=1818118
    if (!obsoleters) {
        if (relevant_installed_na.empty()) {
            if (relevant_installed_n.empty()) {
                transaction.p_impl->add_resolve_log(
                    action,
                    GoalProblem::NOT_INSTALLED,
                    settings,
                    libdnf5::transaction::TransactionItemType::PACKAGE,
                    spec,
                    {},
                    libdnf5::Logger::Level::WARNING);
                return skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_INSTALLED;
            }
            transaction.p_impl->add_resolve_log(
                action,
                GoalProblem::NOT_INSTALLED_FOR_ARCHITECTURE,
                settings,
                libdnf5::transaction::TransactionItemType::PACKAGE,
                spec,
                {},
                libdnf5::Logger::Level::WARNING);
            return skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_INSTALLED_FOR_ARCHITECTURE;
        }
    }

    bool add_obsoletes = obsoletes && nevra_pair.second.has_just_name() && action != GoalAction::DOWNGRADE;
    rpm::PackageQuery query_installed(query);
    if (settings.get_from_repo_ids().empty()) {
        query_installed.filter_installed();
    } else {
        query_installed.filter_from_repo_id(settings.get_from_repo_ids(), libdnf5::sack::QueryCmp::GLOB);
    }

    // TODO(jmracek) Apply latest filters on installed (or later)
    if (add_obsoletes) {
        // Obsoletes are not added to downgrade set
        if (action == GoalAction::UPGRADE) {
            // Do not add obsoleters of packages that are not upgrades. Such packages can be confusing for the solver.
            rpm::PackageQuery obsoletes_query(base_query);
            obsoletes_query.filter_available();
            rpm::PackageQuery to_obsolete_query(query);
            to_obsolete_query.filter_upgrades();
            to_obsolete_query |= query_installed;
            obsoletes_query.filter_obsoletes(to_obsolete_query);
            query |= obsoletes_query;
        } else if (action == GoalAction::DISTRO_SYNC) {
            rpm::PackageQuery obsoletes_query(base_query);
            obsoletes_query.filter_available();
            obsoletes_query.filter_obsoletes(query);
            query |= obsoletes_query;
        }
    }
    if (!settings.get_to_repo_ids().empty()) {
        query.filter_repo_id(settings.get_to_repo_ids(), sack::QueryCmp::GLOB);
        if (query.empty()) {
            transaction.p_impl->add_resolve_log(
                action,
                GoalProblem::NOT_FOUND_IN_REPOSITORIES,
                settings,
                libdnf5::transaction::TransactionItemType::PACKAGE,
                spec,
                {},
                libdnf5::Logger::Level::WARNING);
            return skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_FOUND_IN_REPOSITORIES;
        }
    }

    const auto & to_vendors = settings.get_to_vendors();

    if (!to_vendors.empty()) {
        query.filter_vendor(to_vendors, sack::QueryCmp::GLOB);
        if (query.empty()) {
            transaction.p_impl->add_resolve_log(
                action,
                GoalProblem::NOT_FOUND_IN_REPOSITORIES,  // TODO(jrohel): NOT_FOUND_FROM_VENDOR
                settings,
                libdnf5::transaction::TransactionItemType::PACKAGE,
                spec,
                {},
                libdnf5::Logger::Level::WARNING);
            return skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_FOUND_IN_REPOSITORIES;
        }
    }

    // Apply advisory filters
    if (settings.get_advisory_filter() != nullptr) {
        filter_candidates_for_advisory_upgrade(base, query, *settings.get_advisory_filter(), obsoletes);
        if (query.empty()) {
            transaction.p_impl->add_resolve_log(
                action,
                GoalProblem::NOT_FOUND_IN_ADVISORIES,
                settings,
                libdnf5::transaction::TransactionItemType::PACKAGE,
                spec,
                {},
                libdnf5::Logger::Level::WARNING);
            return skip_unavailable ? GoalProblem::NO_PROBLEM : GoalProblem::NOT_FOUND_IN_ADVISORIES;
        }
    }

    if (minimal) {
        query.filter_earliest_evr();
    }

    if (!to_vendors.empty()) {
        incoming_vendor_bypassed_solvables |= *query.p_impl;
    }

    switch (action) {
        case GoalAction::UPGRADE_MINIMAL:
        case GoalAction::UPGRADE:
            query.filter_available();
            // Given that we use libsolv's targeted transactions, we need to ensure that the transaction contains both
            // the new targeted version and also the current installed version (for the upgraded package). This is
            // because if it only contained the new version, libsolv would decide to reinstall the package even if it
            // had just a different buildtime or vendor but the same version
            // (https://github.com/openSUSE/libsolv/issues/287)
            //   - Make sure that query contains both the new and installed versions (i.e. add installed versions)
            //   - However we need to add installed versions of just the packages that are being upgraded. We don't want
            //     to add all installed packages because it could increase the number of solutions for the transaction
            //     (especially with --no-best) and since libsolv prefers the smallest possible upgrade it could result
            //     in no upgrade even if there is one available. This is a problem in general but its critical with
            //     --security transactions (https://bugzilla.redhat.com/show_bug.cgi?id=2097757)
            installed.filter_name(query);
            //   - We want to add only the latest versions of installed packages, this is specifically for installonly
            //     packages. Otherwise if for example kernel-1 and kernel-3 were installed and present in the
            //     transaction libsolv could decide to install kernel-2 because it is an upgrade for kernel-1 even
            //     though we don't want it because there already is a newer version present.
            installed.filter_latest_evr();
            query |= installed;
            solv_map_to_id_queue(tmp_queue, *query.p_impl);
            rpm_goal.add_upgrade(tmp_queue, best, clean_requirements_on_remove);
            break;
        case GoalAction::DISTRO_SYNC:
            solv_map_to_id_queue(tmp_queue, *query.p_impl);
            rpm_goal.add_distro_sync(tmp_queue, skip_broken, best, clean_requirements_on_remove);
            break;
        case GoalAction::DOWNGRADE: {
            query.filter_available();
            query.filter_downgrades();
            auto & pool = get_rpm_pool(base);
            std::vector<Solvable *> tmp_solvables;
            for (auto pkg_id : *query.p_impl) {
                tmp_solvables.push_back(pool.id2solvable(pkg_id));
            }
            std::sort(tmp_solvables.begin(), tmp_solvables.end(), nevra_solvable_cmp_key);
            std::map<Id, std::vector<Id>> name_arches;
            // Make for each name arch only one downgrade job
            for (auto installed_id : *relevant_installed_na.p_impl) {
                Solvable * solvable = pool.id2solvable(installed_id);
                auto & arches = name_arches[solvable->name];
                bool unique = true;
                for (Id arch : arches) {
                    if (arch == solvable->arch) {
                        unique = false;
                        break;
                    }
                }
                if (unique) {
                    arches.push_back(solvable->arch);
                    tmp_queue.clear();
                    auto low = std::lower_bound(
                        tmp_solvables.begin(), tmp_solvables.end(), solvable, name_arch_compare_lower_solvable);
                    while (low != tmp_solvables.end() && (*low)->name == solvable->name &&
                           (*low)->arch == solvable->arch) {
                        tmp_queue.push_back(pool.solvable2id(*low));
                        ++low;
                    }
                    if (tmp_queue.empty()) {
                        std::string name_arch(pool.get_name(installed_id));
                        name_arch.append(".");
                        name_arch.append(pool.get_arch(installed_id));
                        transaction.p_impl->add_resolve_log(
                            action,
                            GoalProblem::INSTALLED_LOWEST_VERSION,
                            settings,
                            libdnf5::transaction::TransactionItemType::PACKAGE,
                            spec,
                            {name_arch},
                            libdnf5::Logger::Level::WARNING);
                    } else {
                        rpm_goal.add_install(tmp_queue, skip_broken, best, clean_requirements_on_remove);
                    }
                }
            }
        } break;
        default:
            throw std::invalid_argument("Unsupported action");
    }
    return GoalProblem::NO_PROBLEM;
}

void Goal::Impl::install_group_package(base::Transaction & transaction, libdnf5::comps::Package pkg) {
    auto pkg_settings = GoalJobSettings();
    pkg_settings.set_with_provides(false);
    pkg_settings.set_with_filenames(false);
    pkg_settings.set_with_binaries(false);
    pkg_settings.set_nevra_forms({rpm::Nevra::Form::NAME});

    // TODO(mblaha): apply pkg.basearchonly when available in comps
    auto pkg_name = pkg.get_name();
    auto pkg_condition = pkg.get_condition();
    if (pkg_condition.empty()) {
        auto [pkg_problem, pkg_queue] =
            // TODO(mblaha): add_install_to_goal needs group spec for better problems reporting
            add_install_to_goal(transaction, GoalAction::INSTALL_BY_COMPS, pkg_name, pkg_settings);
        rpm_goal.add_transaction_group_reason(pkg_queue);
    } else {
        // check whether condition can even be met
        rpm::PackageQuery condition_query(base);
        condition_query.filter_name(std::vector<std::string>{pkg_condition});
        if (!condition_query.empty()) {
            // remember names to identify GROUP reason of conditional packages
            rpm::PackageQuery query(base);
            query.filter_name(std::vector<std::string>{pkg_name});
            // TODO(mblaha): log absence of pkg in case the query is empty
            if (!query.empty()) {
                add_provide_install_to_goal(fmt::format("({} if {})", pkg_name, pkg_condition), pkg_settings);
                rpm_goal.add_transaction_group_reason(*query.p_impl);
            }
        }
    }
}

void Goal::Impl::remove_group_packages(const rpm::PackageSet & remove_candidates) {
    // all installed packages, that are not candidates for removal
    rpm::PackageQuery dependent_base(base);
    dependent_base.filter_installed();
    {
        // create auxiliary goal to resolve all unused dependencies that are going to be
        // removed together with removal candidates
        Goal goal_tmp(base);
        goal_tmp.add_rpm_remove(remove_candidates);
        for (const auto & tspkg : goal_tmp.resolve().get_transaction_packages()) {
            if (transaction_item_action_is_outbound(tspkg.get_action())) {
                dependent_base.remove(tspkg.get_package());
            }
        }
    }

    // The second step of packages removal - filter out packages that are
    // dependencies of a package that is not also being removed.
    libdnf5::solv::IdQueue packages_to_remove_ids;
    for (const auto & pkg : remove_candidates) {
        // if the package is required by another installed package, it is
        // not removed, but it's reason is changed to DEPENDENCY
        rpm::PackageQuery dependent(dependent_base);
        dependent.filter_requires(pkg.get_provides());
        if (dependent.size() > 0) {
            rpm_goal.add_reason_change(pkg, transaction::TransactionItemReason::DEPENDENCY, std::nullopt);
        } else {
            packages_to_remove_ids.push_back(pkg.get_id().id);
        }
    }

    auto & cfg_main = base->get_config();
    rpm_goal.add_remove(packages_to_remove_ids, cfg_main.get_clean_requirements_on_remove_option().get_value());
    rpm_goal.add_transaction_group_reason(packages_to_remove_ids);
}

void Goal::Impl::add_group_install_to_goal(
    base::Transaction & transaction,
    const transaction::TransactionItemReason reason,
    comps::GroupQuery group_query,
    GoalJobSettings & settings) {
    auto & cfg_main = base->get_config();
    auto allowed_package_types = settings.resolve_group_package_types(cfg_main);
    for (auto group : group_query) {
        comps::GroupQuery installed_group = comps::GroupQuery(base, false);
        installed_group.filter_groupid(group.get_groupid());
        installed_group.filter_installed(true);
        if (installed_group.empty()) {
            // Mark the group for installation since it wasn't previously installed.
            rpm_goal.add_group(group, transaction::TransactionItemAction::INSTALL, reason, allowed_package_types);
        } else if (installed_group.get().get_reason() < reason) {
            // Change the reason of the group.
            rpm_goal.add_group(group, transaction::TransactionItemAction::REASON_CHANGE, reason, allowed_package_types);
        } else {
            // The group is already installed with the same reason, don't mark it for installation again.
            transaction.p_impl->add_resolve_log(
                GoalAction::INSTALL,
                GoalProblem::ALREADY_INSTALLED,
                settings,
                libdnf5::transaction::TransactionItemType::GROUP,
                group.get_groupid(),
                {},
                libdnf5::Logger::Level::WARNING);
        }
        if (settings.get_group_no_packages()) {
            continue;
        }
        std::vector<libdnf5::comps::Package> packages;
        // TODO(mblaha): filter packages by p.arch attribute when supported by comps
        for (const auto & p : group.get_packages()) {
            if (any(allowed_package_types & p.get_type())) {
                packages.emplace_back(std::move(p));
            }
        }
        for (const auto & pkg : packages) {
            install_group_package(transaction, pkg);
        }
    }
}

void Goal::Impl::add_group_remove_to_goal(
    std::vector<std::tuple<std::string, transaction::TransactionItemReason, comps::GroupQuery, GoalJobSettings>> &
        groups_to_remove) {
    if (groups_to_remove.empty()) {
        return;
    }

    // get list of group ids being removed in this transaction
    std::set<std::string> removed_groups_ids;
    for (auto & [spec, reason, group_query, settings] : groups_to_remove) {
        for (const auto & group : group_query) {
            removed_groups_ids.emplace(group.get_groupid());
        }
    }
    rpm::PackageQuery query_installed(base);
    query_installed.filter_installed();
    auto & system_state = base->p_impl->get_system_state();
    // packages that are candidates for removal
    rpm::PackageSet remove_candidates(base);
    for (auto & [spec, reason, group_query, settings] : groups_to_remove) {
        for (const auto & group : group_query) {
            rpm_goal.add_group(group, transaction::TransactionItemAction::REMOVE, reason, {});
            if (settings.get_group_no_packages()) {
                continue;
            }
            // get all packages installed by the group
            rpm::PackageQuery group_packages(query_installed);
            group_packages.filter_name(system_state.get_group_state(group.get_groupid()).packages);
            // Remove packages installed by the group.
            // First collect packages that are not part of any other
            // installed group and are not user-installed.
            for (const auto & pkg : group_packages) {
                // is the package part of another group which is not being removed?
                auto pkg_groups = system_state.get_package_groups(pkg.get_name());
                // remove from the list all groups being removed in this transaction
                for (const auto & id : removed_groups_ids) {
                    pkg_groups.erase(id);
                }
                if (pkg_groups.size() > 0) {
                    continue;
                }

                // was the package user-installed?
                if (pkg.get_reason() > transaction::TransactionItemReason::GROUP) {
                    continue;
                }

                remove_candidates.add(pkg);
            }
        }
    }
    if (remove_candidates.empty()) {
        return;
    }

    remove_group_packages(remove_candidates);
}

void Goal::Impl::add_group_upgrade_to_goal(
    base::Transaction & transaction, comps::GroupQuery group_query, GoalJobSettings & settings) {
    auto & system_state = base->p_impl->get_system_state();

    comps::GroupQuery available_groups(base);
    available_groups.filter_installed(false);

    for (auto installed_group : group_query) {
        auto group_id = installed_group.get_groupid();
        // find available group of the same id
        comps::GroupQuery available_group_query(available_groups);
        available_group_query.filter_groupid(group_id);
        if (available_group_query.empty()) {
            // group is not available any more
            transaction.p_impl->add_resolve_log(
                GoalAction::UPGRADE,
                GoalProblem::NOT_AVAILABLE,
                settings,
                libdnf5::transaction::TransactionItemType::GROUP,
                group_id,
                {},
                libdnf5::Logger::Level::WARNING);
            continue;
        }
        auto available_group = available_group_query.get();
        auto state_group = system_state.get_group_state(group_id);

        // upgrade the group itself
        rpm_goal.add_group(
            available_group,
            transaction::TransactionItemAction::UPGRADE,
            installed_group.get_reason(),
            state_group.package_types);

        if (settings.get_group_no_packages()) {
            continue;
        }


        // set of package names that are part of the installed version of the group
        std::set<std::string> old_set{};
        for (const auto & pkg : installed_group.get_packages()) {
            old_set.emplace(pkg.get_name());
        }
        // set of package names that are part of the available version of the group
        std::vector<std::string> new_set;
        const auto & pkgs_from_available_group = available_group.get_packages();
        new_set.reserve(pkgs_from_available_group.size());
        for (const auto & pkg : pkgs_from_available_group) {
            new_set.push_back(pkg.get_name());
        }

        // install packages newly added to the group
        for (const auto & pkg : available_group.get_packages_of_type(state_group.package_types)) {
            if (!old_set.contains(pkg.get_name())) {
                install_group_package(transaction, pkg);
            }
        }

        // upgrade installed packages that are part of the group (even if not installed with the group)
        auto pkg_settings = GoalJobSettings();
        pkg_settings.set_with_provides(false);
        pkg_settings.set_with_filenames(false);
        pkg_settings.set_with_binaries(false);
        pkg_settings.set_nevra_forms({rpm::Nevra::Form::NAME});
        rpm::PackageQuery query_installed(base);
        query_installed.filter_name(new_set);
        query_installed.filter_installed();
        for (const auto & pkg : query_installed) {
            add_up_down_distrosync_to_goal(transaction, GoalAction::UPGRADE, pkg.get_name(), pkg_settings);
        }
    }
}

void Goal::Impl::add_environment_install_to_goal(
    base::Transaction & transaction, comps::EnvironmentQuery environment_query, GoalJobSettings & settings) {
    auto & cfg_main = base->get_config();
    bool with_optional = any(settings.resolve_group_package_types(cfg_main) & libdnf5::comps::PackageType::OPTIONAL);
    auto group_settings = libdnf5::GoalJobSettings(settings);
    group_settings.set_group_search_environments(false);
    group_settings.set_group_search_groups(true);
    std::vector<GroupSpec> env_group_specs;
    for (auto environment : environment_query) {
        comps::EnvironmentQuery installed_env = comps::EnvironmentQuery(base, false);
        installed_env.filter_environmentid(environment.get_environmentid());
        installed_env.filter_installed(true);
        if (installed_env.empty()) {
            // Mark the environment for installation since it wasn't previously installed.
            rpm_goal.add_environment(environment, transaction::TransactionItemAction::INSTALL, with_optional);
        } else {
            // The environment is already installed, don't mark it for installation again.
            transaction.p_impl->add_resolve_log(
                GoalAction::INSTALL,
                GoalProblem::ALREADY_INSTALLED,
                settings,
                libdnf5::transaction::TransactionItemType::ENVIRONMENT,
                environment.get_environmentid(),
                {},
                libdnf5::Logger::Level::WARNING);
        }
        if (settings.get_environment_no_groups()) {
            continue;
        }
        for (const auto & grp_id : environment.get_groups()) {
            env_group_specs.emplace_back(
                GoalAction::INSTALL_BY_COMPS, transaction::TransactionItemReason::DEPENDENCY, grp_id, group_settings);
        }
        if (with_optional) {
            for (const auto & grp_id : environment.get_optional_groups()) {
                env_group_specs.emplace_back(
                    GoalAction::INSTALL_BY_COMPS,
                    transaction::TransactionItemReason::DEPENDENCY,
                    grp_id,
                    group_settings);
            }
        }
    }
    resolve_group_specs(env_group_specs, transaction);
}

void Goal::Impl::add_environment_remove_to_goal(
    base::Transaction & transaction,
    std::vector<std::tuple<std::string, comps::EnvironmentQuery, GoalJobSettings>> & environments_to_remove) {
    if (environments_to_remove.empty()) {
        return;
    }

    // get list of environment ids being removed in this transaction
    std::set<std::string> removed_environments_ids;
    for (auto & [spec, environment_query, settings] : environments_to_remove) {
        for (const auto & environment : environment_query) {
            removed_environments_ids.emplace(environment.get_environmentid());
        }
    }
    comps::GroupQuery query_installed(base);
    query_installed.filter_installed(true);
    auto & system_state = base->p_impl->get_system_state();
    // groups that are candidates for removal
    std::vector<GroupSpec> remove_group_specs;
    auto group_settings = libdnf5::GoalJobSettings();
    group_settings.set_group_search_environments(false);
    group_settings.set_group_search_groups(true);
    for (auto & [spec, environment_query, settings] : environments_to_remove) {
        for (const auto & environment : environment_query) {
            rpm_goal.add_environment(environment, transaction::TransactionItemAction::REMOVE, {});
            if (settings.get_environment_no_groups()) {
                continue;
            }
            // get all groups installed by the environment
            comps::GroupQuery environment_groups(query_installed);
            environment_groups.filter_groupid(
                system_state.get_environment_state(environment.get_environmentid()).groups);
            // Remove groups installed by the environment in case they are installed
            // as dependencies and are not part of another installed environment.
            for (const auto & grp : environment_groups) {
                // is the group part of another environment which is not being removed?
                auto grp_environments = system_state.get_group_environments(grp.get_groupid());
                // remove from the list all environments being removed in this transaction
                for (const auto & id : removed_environments_ids) {
                    grp_environments.erase(id);
                }
                if (grp_environments.size() > 0) {
                    continue;
                }

                // was the group user-installed?
                if (grp.get_reason() > transaction::TransactionItemReason::GROUP) {
                    continue;
                }

                remove_group_specs.emplace_back(
                    GoalAction::REMOVE,
                    transaction::TransactionItemReason::DEPENDENCY,
                    grp.get_groupid(),
                    group_settings);
            }
        }
    }
    resolve_group_specs(remove_group_specs, transaction);
}

void Goal::Impl::add_environment_upgrade_to_goal(
    base::Transaction & transaction, comps::EnvironmentQuery environment_query, GoalJobSettings & settings) {
    auto & system_state = base->p_impl->get_system_state();

    comps::EnvironmentQuery available_environments(base);
    available_environments.filter_installed(false);

    std::vector<GroupSpec> env_group_specs;
    auto group_settings = libdnf5::GoalJobSettings(settings);
    group_settings.set_group_search_environments(false);
    group_settings.set_group_search_groups(true);

    for (auto installed_environment : environment_query) {
        auto environment_id = installed_environment.get_environmentid();
        // find available environment of the same id
        comps::EnvironmentQuery available_environment_query(available_environments);
        available_environment_query.filter_environmentid(environment_id);
        if (available_environment_query.empty()) {
            // environment is not available any more
            transaction.p_impl->add_resolve_log(
                GoalAction::UPGRADE,
                GoalProblem::NOT_AVAILABLE,
                settings,
                libdnf5::transaction::TransactionItemType::ENVIRONMENT,
                environment_id,
                {},
                libdnf5::Logger::Level::WARNING);
            continue;
        }
        auto available_environment = available_environment_query.get();

        // upgrade the environment itself
        rpm_goal.add_environment(available_environment, transaction::TransactionItemAction::UPGRADE, {});

        if (settings.get_environment_no_groups()) {
            continue;
        }

        // group names that are part of the installed version of the environment
        auto old_groups = installed_environment.get_groups();

        // group names that are part of the new version of the environment
        auto available_groups = available_environment.get_groups();

        for (const auto & grp : available_groups) {
            if (std::find(old_groups.begin(), old_groups.end(), grp) != old_groups.end()) {
                // the group was already part of environment definition when it was installed.
                // upgrade the group if is installed
                try {
                    auto group_state = system_state.get_group_state(grp);
                    env_group_specs.emplace_back(
                        GoalAction::UPGRADE, transaction::TransactionItemReason::DEPENDENCY, grp, group_settings);
                } catch (const system::StateNotFoundError &) {
                    continue;
                }
            } else {
                // newly added group to environment definition, install it.
                env_group_specs.emplace_back(
                    GoalAction::INSTALL_BY_COMPS, transaction::TransactionItemReason::DEPENDENCY, grp, group_settings);
            }
        }

        // upgrade also installed optional groups
        auto old_optionals = installed_environment.get_optional_groups();
        old_groups.insert(old_groups.end(), old_optionals.begin(), old_optionals.end());
        for (const auto & grp : available_environment.get_optional_groups()) {
            available_groups.emplace_back(grp);
            if (std::find(old_groups.begin(), old_groups.end(), grp) != old_groups.end()) {
                try {
                    auto group_state = system_state.get_group_state(grp);
                    env_group_specs.emplace_back(
                        GoalAction::UPGRADE, transaction::TransactionItemReason::DEPENDENCY, grp, group_settings);
                } catch (const system::StateNotFoundError &) {
                    continue;
                }
            }
        }
    }

    resolve_group_specs(env_group_specs, transaction);
}

GoalProblem Goal::Impl::add_reason_change_to_goal(
    base::Transaction & transaction,
    const std::string & spec,
    const transaction::TransactionItemReason reason,
    const std::optional<std::string> & group_id,
    GoalJobSettings & settings) {
    auto & cfg_main = base->get_config();
    bool skip_unavailable = settings.resolve_skip_unavailable(cfg_main);
    auto log_level = skip_unavailable ? libdnf5::Logger::Level::WARNING : libdnf5::Logger::Level::ERROR;
    rpm::PackageQuery query(base);
    query.filter_installed();
    auto nevra_pair = query.resolve_pkg_spec(spec, settings, false);
    if (!nevra_pair.first) {
        auto problem = transaction.p_impl->report_not_found(GoalAction::REASON_CHANGE, spec, settings, log_level);
        if (skip_unavailable) {
            return GoalProblem::NO_PROBLEM;
        } else {
            return problem;
        }
    }
    for (const auto & pkg : query) {
        // check if the package is already installed with the requested reason or
        // if the requested reason is GROUP and the package is already part of the group
        if (pkg.get_reason() == reason &&
            (reason != transaction::TransactionItemReason::GROUP ||
             (group_id && base->p_impl->get_system_state().get_package_groups(pkg.get_name()).contains(*group_id)))) {
            // pkg is already installed with correct reason
            transaction.p_impl->add_resolve_log(
                GoalAction::REASON_CHANGE,
                GoalProblem::ALREADY_INSTALLED,
                settings,
                libdnf5::transaction::TransactionItemType::PACKAGE,
                pkg.get_nevra(),
                {libdnf5::transaction::transaction_item_reason_to_string(reason)},
                libdnf5::Logger::Level::WARNING);
            continue;
        }
        rpm_goal.add_reason_change(pkg, reason, group_id);
    }
    return GoalProblem::NO_PROBLEM;
}

GoalProblem Goal::Impl::resolve_reverted_transactions(base::Transaction & transaction) {
    if (!revert_transactions) {
        return GoalProblem::NO_PROBLEM;
    }
    auto ret = GoalProblem::NO_PROBLEM;

    using Action = transaction::TransactionItemAction;
    using Reason = transaction::TransactionItemReason;
    const std::unordered_map<Action, Action> REVERT_ACTION = {
        {Action::INSTALL, Action::REMOVE},
        {Action::UPGRADE, Action::REPLACED},
        {Action::DOWNGRADE, Action::REPLACED},
        {Action::REINSTALL, Action::REINSTALL},
        {Action::REMOVE, Action::INSTALL},
        {Action::REPLACED, Action::INSTALL},
        {Action::REASON_CHANGE, Action::REASON_CHANGE},
    };
    auto history = base->get_transaction_history();

    auto & [reverting_transactions, settings] = *revert_transactions;
    std::vector<transaction::TransactionReplay> reverted_transactions;

    for (auto & reverting_transaction : reverting_transactions) {
        transaction::TransactionReplay replay;
        for (const auto & pkg : reverting_transaction.get_packages()) {
            transaction::PackageReplay package_replay;
            package_replay.nevra = libdnf5::rpm::to_nevra_string(pkg);
            auto reverted_action = REVERT_ACTION.find(pkg.get_action());
            libdnf_assert(
                reverted_action != REVERT_ACTION.end(),
                "Cannot revert action: \"{}\"",
                transaction_item_action_to_string(pkg.get_action()));
            package_replay.action = reverted_action->second;

            // We cannot tell the previous reason if the action is REASON_CHANGE it could have been anything.
            // For reverted action INSTALL and reason CLEAN the previous reason could have been either DEPENDENCY or WEAK DEPENDENCY
            // to pick the right one we have to look into history.
            if ((package_replay.action == Action::REASON_CHANGE) ||
                (package_replay.action == Action::INSTALL && pkg.get_reason() == Reason::CLEAN)) {
                // We look up the reason based on only name and arch, this means we could find a different
                // version of installonly package however we store only one reason for ALL versions of
                // installonly packages so it doesn't matter.
                package_replay.reason = history->transaction_item_reason_at(
                    pkg.get_name(), pkg.get_arch(), reverting_transaction.get_id() - 1);
            } else if (
                package_replay.action == Action::REMOVE &&
                (pkg.get_reason() == Reason::DEPENDENCY || pkg.get_reason() == Reason::WEAK_DEPENDENCY)) {
                package_replay.reason = Reason::CLEAN;
            } else {
                package_replay.reason = pkg.get_reason();
            }

            replay.packages.push_back(package_replay);
        }

        for (const auto & group : reverting_transaction.get_comps_groups()) {
            transaction::GroupReplay group_replay;
            group_replay.group_id = group.to_string();
            // Do not revert UPGRADE for groups. Groups don't have an upgrade path so they cannot be
            // upgraded or downgraded. The UPGRADE action is basically a synchronization with
            // current group definition. Revert happens automatically by reverting the rpm actions.
            if (group.get_action() != transaction::TransactionItemAction::UPGRADE) {
                auto reverted_action = REVERT_ACTION.find(group.get_action());
                if (reverted_action == REVERT_ACTION.end()) {
                    libdnf_throw_assertion(
                        "Cannot revert action: \"{}\"", transaction_item_action_to_string(group.get_action()));
                }
                group_replay.action = reverted_action->second;
            } else {
                transaction.p_impl->add_resolve_log(
                    GoalAction::REVERT_COMPS_UPGRADE,
                    libdnf5::GoalProblem::UNSUPPORTED_ACTION,
                    settings,
                    libdnf5::transaction::TransactionItemType::GROUP,
                    group_replay.group_id,
                    {},
                    libdnf5::Logger::Level::WARNING);
                continue;
            }

            if (group_replay.action == Action::INSTALL && group.get_reason() == Reason::CLEAN) {
                group_replay.reason = Reason::DEPENDENCY;
            } else if (group_replay.action == Action::REMOVE && group.get_reason() == Reason::DEPENDENCY) {
                group_replay.reason = Reason::CLEAN;
            } else {
                group_replay.reason = group.get_reason();
            }

            replay.groups.push_back(group_replay);
        }

        for (const auto & env : reverting_transaction.get_comps_environments()) {
            transaction::EnvironmentReplay env_replay;
            env_replay.environment_id = env.to_string();
            // Do not revert UPGRADE for environments. Environments don't have an upgrade path so they cannot be
            // upgraded or downgraded. The UPGRADE action is basically a synchronization with
            // current environment definition. Revert happens automatically by reverting the rpm
            // actions.
            if (env.get_action() != transaction::TransactionItemAction::UPGRADE) {
                auto reverted_action = REVERT_ACTION.find(env.get_action());
                if (reverted_action == REVERT_ACTION.end()) {
                    libdnf_throw_assertion(
                        "Cannot revert action: \"{}\"", transaction_item_action_to_string(env.get_action()));
                }
                env_replay.action = reverted_action->second;
            } else {
                transaction.p_impl->add_resolve_log(
                    GoalAction::REVERT_COMPS_UPGRADE,
                    libdnf5::GoalProblem::UNSUPPORTED_ACTION,
                    settings,
                    libdnf5::transaction::TransactionItemType::ENVIRONMENT,
                    env_replay.environment_id,
                    {},
                    libdnf5::Logger::Level::WARNING);
                continue;
            }

            replay.environments.push_back(env_replay);
        }

        reverted_transactions.push_back(replay);
    }

    std::reverse(reverted_transactions.begin(), reverted_transactions.end());

    // Prepare installed map which is needed for merging
    libdnf5::rpm::PackageQuery installed_query(base, libdnf5::rpm::PackageQuery::ExcludeFlags::IGNORE_EXCLUDES);
    installed_query.filter_installed();
    std::unordered_map<std::string, std::vector<std::string>> installed;
    for (const auto & pkg : installed_query) {
        const auto name_arch = pkg.get_name() + "." + pkg.get_arch();
        if (installed.contains(name_arch)) {
            installed[name_arch].push_back(pkg.get_nevra());
        } else {
            installed[name_arch] = {pkg.get_nevra()};
        }
    }
    auto [merged_transactions, problems] = merge_transactions(
        reverted_transactions, installed, base->get_config().get_installonlypkgs_option().get_value());

    for (const auto & problem : problems) {
        transaction.p_impl->add_resolve_log(
            GoalAction::MERGE,
            libdnf5::GoalProblem::MERGE_ERROR,
            settings,
            libdnf5::transaction::TransactionItemType::PACKAGE,
            {},
            {problem},
            libdnf5::Logger::Level::WARNING);
    }

    ret |= add_replay_to_goal(transaction, merged_transactions, settings);

    return ret;
}

GoalProblem Goal::Impl::resolve_redo_transaction(base::Transaction & transaction) {
    if (!redo_transaction) {
        return GoalProblem::NO_PROBLEM;
    }
    auto & [trans, settings] = *redo_transaction;
    return add_replay_to_goal(transaction, transaction::to_replay(trans), settings);
}

void Goal::Impl::add_paths_to_goal() {
    if (rpm_filepaths.empty()) {
        return;
    }

    // fill the command line repo with paths to rpm files
    std::vector<std::string> paths;

    for (const auto & [action, path, settings] : rpm_filepaths) {
        paths.emplace_back(path);
    }
    auto cmdline_packages = base->get_repo_sack()->add_cmdline_packages(paths);

    // add newly created packages to the goal
    for (const auto & [action, path, settings] : rpm_filepaths) {
        auto pkg = cmdline_packages.find(path);
        if (pkg != cmdline_packages.end()) {
            if (const auto & to_vendors = settings.get_to_vendors(); !to_vendors.empty()) {
                auto pkg_vendor = pkg->second.get_vendor();
                bool match = false;
                for (const auto & to_vendor : to_vendors) {
                    if (fnmatch(to_vendor.c_str(), pkg_vendor.c_str(), 0) == 0) {
                        auto id = pkg->second.get_id().id;
                        incoming_vendor_bypassed_solvables.grow(id);
                        incoming_vendor_bypassed_solvables.add_unsafe(id);
                        match = true;
                        break;
                    }
                }
                if (!match) {
                    continue;
                }
            }
            add_rpm_ids(action, pkg->second, settings);
        }
    }

    // clear rpm_filepaths so that they do not get inserted into command line
    // repo again in case the goal is resolved multiple times.
    rpm_filepaths.clear();
}

void Goal::Impl::set_exclude_from_weak(const std::vector<std::string> & exclude_from_weak) {
    for (const auto & exclude_weak : exclude_from_weak) {
        rpm::PackageQuery weak_query(base, rpm::PackageQuery::ExcludeFlags::APPLY_EXCLUDES);
        libdnf5::ResolveSpecSettings settings;
        settings.set_with_nevra(true);
        settings.set_with_provides(false);
        settings.set_with_filenames(true);
        settings.set_with_binaries(true);
        weak_query.resolve_pkg_spec(exclude_weak, settings, false);
        weak_query.filter_available();
        rpm_goal.add_exclude_from_weak(*weak_query.p_impl);
    }
}

void Goal::Impl::autodetect_unsatisfied_installed_weak_dependencies() {
    rpm::PackageQuery installed_query(base, rpm::PackageQuery::ExcludeFlags::IGNORE_EXCLUDES);
    installed_query.filter_installed();
    if (installed_query.empty()) {
        return;
    }
    rpm::PackageQuery base_query(base, rpm::PackageQuery::ExcludeFlags::APPLY_EXCLUDES);
    rpm::ReldepList reldep_list(base);

    std::vector<std::string> installed_names;
    installed_names.reserve(installed_query.size());

    // Investigate uninstalled recommends of installed packages
    for (const auto & pkg : installed_query) {
        installed_names.push_back(pkg.get_name());
        for (const auto & recommend : pkg.get_recommends()) {
            if (libdnf5::rpm::Reldep::is_rich_dependency(recommend.to_string())) {
                // Rich dependencies are skipped because they are too complicated to provide correct result
                continue;
            };
            rpm::PackageQuery query(base_query);

            //  There can be installed provider in a different version or upgraded package can recommend a different
            //  version therefore ignore the version and to search only using reldep name
            if (auto version = recommend.get_version(); version && strlen(version) > 0) {
                auto & pool = get_rpm_pool(base);
                Id id = pool.str2id(recommend.get_name(), 0);
                reldep_list.add(rpm::ReldepId(id));
            } else {
                reldep_list.add(recommend);
            };
            query.filter_provides(reldep_list);
            reldep_list.clear();
            // No providers of recommend => continue
            if (query.empty()) {
                continue;
            }
            rpm::PackageQuery test_installed(query);
            test_installed.filter_installed();
            // when there is not installed any provider of recommend, exclude it
            if (test_installed.empty()) {
                rpm_goal.add_exclude_from_weak(*query.p_impl);
            }
        }
    }

    // Investigate supplements of only available packages with a different name to installed packages
    // We can use base_query, because it is not useful anymore
    base_query.filter_name(installed_names, sack::QueryCmp::NEQ);
    // We have to remove all installed packages from testing set
    base_query -= installed_query;
    rpm::PackageSet exclude_supplements(base);
    for (const auto & pkg : base_query) {
        auto supplements = pkg.get_supplements();
        if (supplements.empty()) {
            continue;
        }
        for (const auto & supplement : supplements) {
            if (libdnf5::rpm::Reldep::is_rich_dependency(supplement.to_string())) {
                // Rich dependencies are skipped because they are too complicated to provide correct result
                continue;
            };
            reldep_list.add(supplement);
        }
        if (reldep_list.empty()) {
            continue;
        }
        rpm::PackageQuery query(installed_query);
        query.filter_provides(reldep_list);
        reldep_list.clear();
        if (!query.empty()) {
            exclude_supplements.add(pkg);
        }
    }
    if (!exclude_supplements.empty()) {
        rpm_goal.add_exclude_from_weak(*exclude_supplements.p_impl);
    }
}

void Goal::set_allow_erasing(bool value) {
    p_impl->allow_erasing = value;
}

bool Goal::get_allow_erasing() const {
    return p_impl->allow_erasing;
}

base::Transaction Goal::resolve() {
    libdnf_user_assert(p_impl->base->is_initialized(), "Base instance was not fully initialized by Base::setup()");

    p_impl->rpm_goal = rpm::solv::GoalPrivate(p_impl->base);

    base::Transaction transaction(p_impl->base);
    auto ret = GoalProblem::NO_PROBLEM;

    // Transaction replay has to be added first because it only adds to other vectors
    // of specs, it doesn't resolve anything. Therefore it doesn't need any Sacks to be ready.
    // In fact given that it can add to rpm_filepaths it has to be added before `add_paths_to_goal()`
    // and thus before the provides are computed.
    // Both serialized and reverted transactions use TransactionReplay.
    ret |= p_impl->add_serialized_transaction_to_goal(transaction);
    ret |= p_impl->resolve_reverted_transactions(transaction);
    ret |= p_impl->resolve_redo_transaction(transaction);

    p_impl->add_paths_to_goal();

    auto sack = p_impl->base->get_rpm_package_sack();

    sack->p_impl->recompute_considered_in_pool();
    sack->p_impl->make_provides_ready();


#ifdef WITH_MODULEMD
    module::ModuleSack & module_sack = *p_impl->base->get_module_sack();
    ret |= p_impl->add_module_specs_to_goal(transaction);

    // Check for switched module streams
    if (!p_impl->base->get_config().get_module_stream_switch_option().get_value()) {
        auto switched_streams = module_sack.p_impl->module_db->get_all_newly_switched_streams();
        if (!switched_streams.empty()) {
            for (auto item : switched_streams) {
                transaction.p_impl->add_resolve_log(
                    GoalAction::ENABLE,
                    GoalProblem::MODULE_CANNOT_SWITH_STREAMS,
                    GoalJobSettings(),
                    libdnf5::transaction::TransactionItemType::MODULE,
                    item.first,
                    {"0:" + item.second.first, "1:" + item.second.second},
                    libdnf5::Logger::Level::ERROR);
            }
            ret |= GoalProblem::MODULE_CANNOT_SWITH_STREAMS;
        }
    }
    // Resolve modules
    auto result = module_sack.resolve_active_module_items();
    auto module_solver_problems = result.first;
    auto module_error = result.second;

    if (module_error != GoalProblem::NO_PROBLEM) {
        // Report problems from the module solver
        transaction.p_impl->add_resolve_log(module_error, module_solver_problems);

        // Ignore MODULE_SOLVER_ERROR_DEFAULTS and MODULE_SOLVER_ERROR_LATEST with best=false
        // Other errors (MODULE_SOLVER_ERROR and MODULE_SOLVER_ERROR_LATEST with best=true) are returned
        if (module_error != GoalProblem::MODULE_SOLVER_ERROR_DEFAULTS &&
            (module_error != GoalProblem::MODULE_SOLVER_ERROR_LATEST ||
             p_impl->base->get_config().get_best_option().get_value())) {
            ret |= module_error;
        }
    }

    module_sack.p_impl->enable_dependent_modules();
#endif


    // TODO(jmracek) Apply comps second or later
    // TODO(jmracek) Reset rpm_goal, setup rpm-goal flags according to conf, (allow downgrade), obsoletes, vendor, ...
    ret |= p_impl->add_specs_to_goal(transaction);
    p_impl->add_rpms_to_goal(transaction);

    // Resolve group specs to group/environment queries first for two reasons:
    // 1. group spec can also contain an environmental groups
    // 2. group removal needs a list of all groups being removed to correctly remove packages
    ret |= p_impl->resolve_group_specs(p_impl->group_specs, transaction);

    // Handle environments before groups because they will add/remove groups
    p_impl->add_resolved_environment_specs_to_goal(transaction);

    // Then handle groups
    p_impl->add_resolved_group_specs_to_goal(transaction);

    ret |= p_impl->add_reason_change_specs_to_goal(transaction);

    auto & cfg_main = p_impl->base->get_config();
    // Set goal flags
    p_impl->rpm_goal.set_allow_vendor_change(cfg_main.get_allow_vendor_change_option().get_value());
    p_impl->rpm_goal.set_allow_erasing(p_impl->allow_erasing);
    p_impl->rpm_goal.set_install_weak_deps(cfg_main.get_install_weak_deps_option().get_value());
    p_impl->rpm_goal.set_allow_downgrade(cfg_main.get_allow_downgrade_option().get_value());

    if (cfg_main.get_protect_running_kernel_option().get_value()) {
        p_impl->rpm_goal.set_protected_running_kernel(sack->p_impl->get_running_kernel_id());
    }

    // Set user-installed packages (installed packages with reason USER or GROUP)
    // proceed only if the transaction could result in removal of unused dependencies
    if (p_impl->rpm_goal.is_clean_deps_present()) {
        libdnf5::solv::IdQueue user_installed_packages;
        rpm::PackageQuery installed_query(p_impl->base, rpm::PackageQuery::ExcludeFlags::IGNORE_EXCLUDES);
        installed_query.filter_installed();
        for (const auto & pkg : installed_query) {
            if (pkg.get_reason() > transaction::TransactionItemReason::DEPENDENCY) {
                user_installed_packages.push_back(pkg.get_id().id);
            }
        }
        p_impl->rpm_goal.set_user_installed_packages(std::move(user_installed_packages));
    }

    // Add protected packages
    {
        auto & protected_packages = cfg_main.get_protected_packages_option().get_value();
        rpm::PackageQuery protected_query(p_impl->base, rpm::PackageQuery::ExcludeFlags::IGNORE_EXCLUDES);
        protected_query.filter_name(protected_packages);
        p_impl->rpm_goal.add_protected_packages(*protected_query.p_impl);
    }

    // Set installonly packages
    {
        auto & installonly_packages = cfg_main.get_installonlypkgs_option().get_value();
        p_impl->rpm_goal.set_installonly(installonly_packages);
        p_impl->rpm_goal.set_installonly_limit(cfg_main.get_installonly_limit_option().get_value());
    }

    // Set exclude weak dependencies from configuration
    {
        p_impl->set_exclude_from_weak(cfg_main.get_exclude_from_weak_option().get_value());
        if (cfg_main.get_exclude_from_weak_autodetect_option().get_value()) {
            p_impl->autodetect_unsatisfied_installed_weak_dependencies();
        }
    }

    auto & pool = get_rpm_pool(p_impl->base);
    pool.get_incoming_vendor_bypassed_solvables() = p_impl->incoming_vendor_bypassed_solvables;

    ret |= p_impl->rpm_goal.resolve();

    // Write debug solver data
    // Note: Modules debug data are handled separately when resolving module goal in ModuleSack::Impl::module_solve()
    if (cfg_main.get_debug_solver_option().get_value()) {
        auto debug_dir = std::filesystem::path(cfg_main.get_debugdir_option().get_value());
        auto pkgs_debug_dir = std::filesystem::absolute(debug_dir / "packages");
        auto comps_debug_dir = std::filesystem::absolute(debug_dir / "comps");

        // Ensures the presence of the directories.
        std::filesystem::create_directories(pkgs_debug_dir);
        std::filesystem::create_directories(comps_debug_dir);

        p_impl->rpm_goal.write_debugdata(pkgs_debug_dir);
        p_impl->base->get_repo_sack()->dump_comps_debugdata(comps_debug_dir);

        transaction.p_impl->add_resolve_log(
            GoalAction::RESOLVE,
            GoalProblem::WRITE_DEBUG,
            {},
            libdnf5::transaction::TransactionItemType::PACKAGE,
            "",
            {std::filesystem::canonical(debug_dir)},
            libdnf5::Logger::Level::WARNING);
    }

    transaction.p_impl->set_transaction(
        p_impl->rpm_goal,
#ifdef WITH_MODULEMD
        module_sack,
#endif
        ret);

    auto & plugins = p_impl->base->p_impl->get_plugins();
    plugins.goal_resolved(transaction);

    return transaction;
}

void Goal::add_serialized_transaction(
    const std::filesystem::path & transaction_path, const libdnf5::GoalJobSettings & settings) {
    libdnf_user_assert(!p_impl->serialized_transaction, "Serialized transaction cannot be set multiple times.");
    p_impl->serialized_transaction =
        std::make_unique<std::tuple<std::filesystem::path, GoalJobSettings>>(transaction_path, settings);
}

void Goal::add_revert_transactions(
    const std::vector<libdnf5::transaction::Transaction> & transactions, const libdnf5::GoalJobSettings & settings) {
    libdnf_user_assert(!p_impl->revert_transactions, "Revert transactions cannot be set multiple times.");
    p_impl->revert_transactions =
        std::make_unique<std::tuple<std::vector<transaction::Transaction>, GoalJobSettings>>(transactions, settings);
}

void Goal::add_redo_transaction(
    const libdnf5::transaction::Transaction & transaction, const libdnf5::GoalJobSettings & settings) {
    libdnf_user_assert(!p_impl->redo_transaction, "Redo transactions cannot be set multiple times.");
    p_impl->redo_transaction =
        std::make_unique<std::tuple<transaction::Transaction, GoalJobSettings>>(transaction, settings);
}

void Goal::reset() {
    p_impl->module_specs.clear();
    p_impl->rpm_specs.clear();
    p_impl->rpm_reason_change_specs.clear();
    p_impl->rpm_ids.clear();
    p_impl->group_specs.clear();
    p_impl->rpm_filepaths.clear();
    p_impl->resolved_group_specs.clear();
    p_impl->resolved_environment_specs.clear();
    p_impl->rpm_goal = rpm::solv::GoalPrivate(p_impl->base);
    p_impl->serialized_transaction.reset();
    p_impl->revert_transactions.reset();
    p_impl->redo_transaction.reset();
}

BaseWeakPtr Goal::get_base() const {
    return p_impl->base->get_weak_ptr();
}

}  // namespace libdnf5