stagecrew 0.2.1

Disk usage management for shared or staging filesystems with automatic cleanup policies
Documentation
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
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
//! Widget rendering for the TUI.

use std::sync::OnceLock;

use ratatui::Frame;
use ratatui::layout::Margin;
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{
    Block, BorderType, Borders, Cell, Clear, HighlightSpacing, Padding, Paragraph, Row, Scrollbar,
    ScrollbarOrientation, ScrollbarState, Table, Tabs, Wrap,
};

use crate::config::Config;
use crate::removal::{DryRunResult, RemovalMethod};
use crate::scanner::calculate_expiration;

use super::TuiContext;

use super::{
    App, FocusPanel, PendingAuditExport, PendingQuotaTarget, QuotaTargetFocus, SortMode, View,
};

/// Semantic color palette for consistent styling across the TUI.
/// Using RGB values ensures colors look the same regardless of terminal theme,
/// and enables smooth gradient interpolation for the row fade effect.
mod palette {
    use ratatui::style::Color;

    /// Safe/healthy state - files with plenty of time remaining
    /// Uses Terminal.app's green for a mellower appearance than pure bright green
    pub const GREEN: Color = Color::Rgb(0, 166, 0);
    /// Warning state - files approaching expiration
    pub const YELLOW: Color = Color::Rgb(220, 200, 0);
    /// Danger/overdue state - files past expiration or pending removal
    pub const RED: Color = Color::Rgb(220, 80, 80);
    /// Deferred state - files with extended deadline
    pub const CYAN: Color = Color::Rgb(0, 200, 200);
    /// Shared modal surface background.
    ///
    /// Use terminal-default background so modal surfaces blend with existing pane
    /// backgrounds instead of creating a dark rectangular cutout.
    pub const MODAL_BG: Color = Color::Reset;
    /// Shared modal primary text color. Uses terminal default for light/dark adaptivity.
    pub const MODAL_FG: Color = Color::Reset;
    /// Shared modal secondary text color.
    pub const MODAL_MUTED: Color = Color::DarkGray;
}

/// Whether the terminal background is light. Detected once before the TUI
/// takes over the terminal, then used to choose fade direction.
static LIGHT_THEME: OnceLock<bool> = OnceLock::new();

/// Detect whether the terminal uses a light background.
/// Must be called before ratatui enters alternate screen mode.
pub(crate) fn detect_terminal_theme() {
    let is_light = std::time::Duration::from_millis(100);
    let light = matches!(termbg::theme(is_light), Ok(termbg::Theme::Light));
    let _ = LIGHT_THEME.set(light);
}

fn is_light_theme() -> bool {
    LIGHT_THEME.get().copied().unwrap_or(false)
}

/// Pre-computed gradient for row fading effect.
/// Contains 101 colors (indices 0-100) from full brightness to fully faded.
/// Index 0 = cursor row (full brightness), index 100 = maximally faded.
struct FadeGradient {
    text: [Color; 101],
    green: [Color; 101],
    yellow: [Color; 101],
    red: [Color; 101],
    gray: [Color; 101],
}

impl FadeGradient {
    /// Create a new gradient set for row fading.
    /// Fades toward a muted endpoint that recedes on the detected background.
    fn new() -> Self {
        // In dark mode, fade toward a dim gray (less visible against dark bg).
        // In light mode, fade toward a light gray (less visible against light bg).
        let fade_end = if is_light_theme() {
            (200, 200, 200)
        } else {
            (130, 130, 130)
        };

        // Text start color also adapts: dark text on light bg, light text on dark bg.
        let text_start = if is_light_theme() {
            (40, 40, 40)
        } else {
            (220, 220, 220)
        };

        // RGB values must match palette constants
        let green_rgb = (0, 166, 0);
        let yellow_rgb = (220, 200, 0);
        let red_rgb = (220, 80, 80);

        Self {
            text: Self::generate(text_start, fade_end),
            green: Self::generate(green_rgb, fade_end),
            yellow: Self::generate(yellow_rgb, fade_end),
            red: Self::generate(red_rgb, fade_end),
            gray: Self::generate((128, 128, 128), fade_end),
        }
    }

    /// Generate a 101-color gradient via linear RGB interpolation.
    fn generate(start: (u8, u8, u8), end: (u8, u8, u8)) -> [Color; 101] {
        let mut gradient = [Color::Reset; 101];
        for (i, slot) in gradient.iter_mut().enumerate() {
            let r = Self::lerp_channel(start.0, end.0, i);
            let g = Self::lerp_channel(start.1, end.1, i);
            let b = Self::lerp_channel(start.2, end.2, i);
            *slot = Color::Rgb(r, g, b);
        }
        gradient
    }

    /// Linear interpolation for a single color channel.
    /// `t` is the step index (0-100), representing t/100 of the way from start to end.
    #[allow(clippy::cast_possible_truncation)]
    // Allow: t is clamped to 0-100 before cast, and result is mathematically
    // bounded to 0-255 (interpolation between two u8 values).
    fn lerp_channel(start: u8, end: u8, t: usize) -> u8 {
        let start_16 = u16::from(start);
        let end_16 = u16::from(end);
        let t_16 = t.min(100) as u16;

        let result = if end_16 >= start_16 {
            start_16 + (end_16 - start_16) * t_16 / 100
        } else {
            start_16 - (start_16 - end_16) * t_16 / 100
        };

        result as u8
    }

    /// Get the fade percentage for a row based on distance from cursor.
    /// Returns 0 for cursor row, up to 100 for rows at the edge of visibility.
    fn fade_percent(row_idx: usize, cursor_idx: usize, visible_rows: usize) -> usize {
        if visible_rows == 0 {
            return 0;
        }
        let distance = row_idx.abs_diff(cursor_idx);
        (distance * 100 / visible_rows.max(1)).min(100)
    }
}

fn fade_gradient() -> &'static FadeGradient {
    static GRADIENT: OnceLock<FadeGradient> = OnceLock::new();
    GRADIENT.get_or_init(FadeGradient::new)
}

/// Render the current application state to the terminal.
///
/// This is the main rendering function that dispatches to view-specific
/// rendering based on the current `app.view` state.
pub(crate) fn render(app: &mut App, ctx: &TuiContext, frame: &mut Frame) {
    // Create the main layout with header, tabs, content, and footer.
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(5), // Header (logo + countdown dial)
            Constraint::Length(3), // View tabs block
            Constraint::Min(0),    // Main content area
            Constraint::Length(1), // Footer
        ])
        .split(frame.area());

    // Render header banner with logo and countdown.
    render_header(app, frame, chunks[0]);

    // Render tabs.
    render_view_tabs(app, frame, chunks[1]);

    // Render the current view in the main area.
    // All view data comes from cached App fields — no DB queries here.
    match app.view() {
        View::FileList => {
            let config = ctx.config(app);
            render_file_list_view(app, config, frame, chunks[2]);
        }
        View::AuditLog => render_audit_log(app, frame, chunks[2]),
        View::Help => render_help(app, frame, chunks[2]),
    }

    // Render footer
    render_footer(app, frame, chunks[3]);

    // Render entry deletion confirmation modal if pending entry delete
    if let Some(deletion) = app.pending_entry_delete() {
        let count = deletion.entries.len();
        if count == 1 {
            render_entry_delete_modal(
                frame,
                &deletion.entries[0].path.to_string_lossy(),
                deletion.entries[0].is_dir,
                deletion.method,
            );
        } else {
            render_entry_delete_modal_multi(frame, count, deletion.method);
        }
    }

    // Render entry deferral input modal if pending entry deferral
    if let Some(deferral) = app.pending_entry_deferral() {
        let count = deferral.entries.len();
        render_deferral_modal(
            frame,
            &deferral.entries[0].path.to_string_lossy(),
            &deferral.input,
            deferral.default_days,
            count,
        );
    }

    // Render entry ignore confirmation modal if pending entry ignore
    if let Some(entries) = app.pending_entry_ignore() {
        let count = entries.len();
        if count == 1 {
            render_ignore_modal(frame, &entries[0].path.to_string_lossy());
        } else {
            render_ignore_modal_multi(frame, count);
        }
    }

    // Render entry approval confirmation modal if pending entry approval
    if let Some(entries) = app.pending_entry_approval() {
        let count = entries.len();
        if count == 1 {
            render_confirmation_modal(frame, &entries[0].path.to_string_lossy());
        } else {
            render_confirmation_modal_multi(frame, count);
        }
    }

    // Render add path input modal if pending add path
    if let Some(input) = app.pending_add_path() {
        render_add_path_modal(frame, input);
    }

    // Render remove path confirmation modal if pending remove path
    if let Some(path) = app.pending_remove_path() {
        render_remove_path_modal(frame, &path.display().to_string());
    }

    // Render quota target input modal if pending
    if let Some(target) = app.pending_quota_target() {
        render_quota_target_modal(frame, target);
    }

    // Render audit export modal if pending
    if let Some(export) = app.pending_audit_export() {
        render_audit_export_modal(frame, export);
    }

    // Render dry run results modal if pending
    if let Some(result) = app.pending_dry_run() {
        render_dry_run_modal(frame, result);
    }
}

/// The stagecrew ASCII logo, trimmed to the 3 letter rows.
const LOGO: &str = "\
┏━┓╺┳╸┏━┓┏━╸┏━╸┏━╸┏━┓┏━╸╻ ╻
┗━┓ ┃ ┣━┫┃╺┓┣╸ ┃  ┣┳┛┣╸ ┃╻┃
┗━┛ ╹ ╹ ╹┗━┛┗━╸┗━╸╹┗╸┗━╸┗┻┛";

fn render_header(app: &App, frame: &mut Frame, area: Rect) {
    let block = Block::default()
        .borders(Borders::ALL)
        .padding(Padding::symmetric(1, 0));
    let inner = block.inner(area);
    frame.render_widget(block, area);

    let now = jiff::Timestamp::now().as_second();
    let has_countdown = app.nearest_expiration.is_some();
    let is_debug_build = cfg!(debug_assertions);
    let logo_style = if is_debug_build {
        Style::default().fg(palette::YELLOW)
    } else {
        Style::default()
    };

    if has_countdown {
        let dial_width: u16 = 16;
        let h_chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Length(30),         // Logo
                Constraint::Length(2),          // Gap
                Constraint::Min(10),            // Context text
                Constraint::Length(1),          // Gap before dial
                Constraint::Length(dial_width), // Timer dial
            ])
            .split(inner);

        let logo = Paragraph::new(LOGO).style(logo_style);
        frame.render_widget(logo, h_chunks[0]);

        let expiration_ts = app.nearest_expiration.unwrap_or(now);
        let remaining = expiration_ts - now;
        let (context_lines, time_top, time_bottom, color) =
            build_countdown_display(remaining, &app.cached_stats, is_debug_build);

        let context = Paragraph::new(context_lines).alignment(Alignment::Right);
        frame.render_widget(context, h_chunks[2]);

        render_timer_dial(frame, h_chunks[4], &time_top, &time_bottom, color);
    } else {
        let h_chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Length(30), // Logo
                Constraint::Length(2),  // Gap
                Constraint::Min(10),    // Status text
            ])
            .split(inner);

        let logo = Paragraph::new(LOGO).style(logo_style);
        frame.render_widget(logo, h_chunks[0]);

        let status_lines = build_idle_display(&app.cached_stats, is_debug_build);
        let status = Paragraph::new(status_lines).alignment(Alignment::Right);
        frame.render_widget(status, h_chunks[2]);
    }
}

fn build_countdown_display(
    remaining_seconds: i64,
    stats: &crate::db::Stats,
    is_debug_build: bool,
) -> (Vec<Line<'static>>, String, String, Color) {
    let (label, time_top, time_bottom, color) = if remaining_seconds <= 0 {
        let overdue = remaining_seconds.unsigned_abs();
        let days = overdue / 86400;
        let hours = (overdue % 86400) / 3600;
        let mins = (overdue % 3600) / 60;
        let secs = overdue % 60;

        let label = if days > 0 {
            format!(
                "overdue by {days} day{}, {hours} hour{}",
                if days == 1 { "" } else { "s" },
                if hours == 1 { "" } else { "s" }
            )
        } else {
            format!(
                "overdue by {hours} hour{}, {mins} min{}",
                if hours == 1 { "" } else { "s" },
                if mins == 1 { "" } else { "s" }
            )
        };

        (
            label,
            format!("-{days}d {hours:02}h"),
            format!("{mins:02}:{secs:02}"),
            palette::RED,
        )
    } else {
        let r = remaining_seconds.unsigned_abs();
        let days = r / 86400;
        let hours = (r % 86400) / 3600;
        let mins = (r % 3600) / 60;
        let secs = r % 60;

        let color = if days == 0 && hours < 6 {
            palette::RED
        } else if remaining_seconds <= 14 * 86400 {
            palette::YELLOW
        } else {
            palette::GREEN
        };

        let label = if days > 0 {
            format!(
                "next removal in {days} day{}, {hours} hour{}",
                if days == 1 { "" } else { "s" },
                if hours == 1 { "" } else { "s" }
            )
        } else {
            format!(
                "next removal in {hours} hour{}, {mins} min{}",
                if hours == 1 { "" } else { "s" },
                if mins == 1 { "" } else { "s" }
            )
        };

        (
            label,
            format!("{days}d {hours:02}h"),
            format!("{mins:02}:{secs:02}"),
            color,
        )
    };

    let is_overdue = remaining_seconds <= 0;
    let context = build_context_lines(&label, stats, is_overdue, is_debug_build);
    (context, time_top, time_bottom, color)
}

fn build_context_lines(
    label: &str,
    stats: &crate::db::Stats,
    is_overdue: bool,
    is_debug_build: bool,
) -> Vec<Line<'static>> {
    let mut lines = vec![if is_debug_build {
        Line::from(Span::styled(
            "DEBUG BUILD — reduced performance",
            Style::default().fg(palette::YELLOW),
        ))
    } else {
        Line::from(Span::styled(
            label.to_string(),
            Style::default().fg(Color::DarkGray),
        ))
    }];

    let overdue = stats.files_overdue;
    let warning = stats.files_within_warning;

    let mut summary_spans = Vec::new();
    if overdue > 0 || is_overdue {
        let count = overdue.max(1);
        summary_spans.push(Span::styled(
            format!("{count} file{} overdue", if count == 1 { "" } else { "s" }),
            Style::default().fg(palette::RED),
        ));
    }
    if warning > 0 {
        if !summary_spans.is_empty() {
            summary_spans.push(Span::styled(" · ", Style::default().fg(Color::DarkGray)));
        }
        summary_spans.push(Span::styled(
            format!(
                "{warning} file{} due soon",
                if warning == 1 { "" } else { "s" }
            ),
            Style::default().fg(palette::YELLOW),
        ));
    }
    if summary_spans.is_empty() {
        summary_spans.push(Span::styled(
            "all clear",
            Style::default().fg(palette::GREEN),
        ));
    }
    lines.push(Line::from(summary_spans));

    #[allow(clippy::cast_sign_loss)]
    let total_bytes = stats.total_size_bytes.max(0) as u64;
    lines.push(Line::from(Span::styled(
        format!(
            "tracking {} files ({})",
            stats.total_files,
            format_bytes(total_bytes)
        ),
        Style::default().fg(Color::DarkGray),
    )));

    lines
}

fn build_idle_display(stats: &crate::db::Stats, is_debug_build: bool) -> Vec<Line<'static>> {
    if stats.total_files == 0 {
        vec![
            if is_debug_build {
                Line::from(Span::styled(
                    "DEBUG BUILD — reduced performance",
                    Style::default().fg(palette::YELLOW),
                ))
            } else {
                Line::from("")
            },
            Line::from(Span::styled(
                "no directories tracked",
                Style::default().fg(Color::DarkGray),
            )),
        ]
    } else {
        #[allow(clippy::cast_sign_loss)]
        let total_bytes = stats.total_size_bytes.max(0) as u64;
        vec![
            if is_debug_build {
                Line::from(Span::styled(
                    "DEBUG BUILD — reduced performance",
                    Style::default().fg(palette::YELLOW),
                ))
            } else {
                Line::from("")
            },
            Line::from(Span::styled(
                "all clear",
                Style::default().fg(palette::GREEN),
            )),
            Line::from(Span::styled(
                format!(
                    "tracking {} files ({})",
                    stats.total_files,
                    format_bytes(total_bytes)
                ),
                Style::default().fg(Color::DarkGray),
            )),
        ]
    }
}

fn render_timer_dial(
    frame: &mut Frame,
    area: Rect,
    time_top: &str,
    time_bottom: &str,
    color: Color,
) {
    if area.height < 3 || area.width < 12 {
        return;
    }

    let border_style = Style::default().fg(color);
    let time_style = Style::default().fg(color).add_modifier(Modifier::BOLD);

    let combined = format!("{time_top} {time_bottom}");
    let width = area
        .width
        .min(u16::try_from(combined.len() + 4).unwrap_or(20));
    let x = area.right().saturating_sub(width);
    let y = area.top();

    let buffer = frame.buffer_mut();

    // Top arc
    buffer[(x, y)].set_symbol("").set_style(border_style);
    for dx in 1..width.saturating_sub(1) {
        buffer[(x + dx, y)].set_symbol("").set_style(border_style);
    }
    buffer[(x + width - 1, y)]
        .set_symbol("")
        .set_style(border_style);

    // Middle row with combined time
    buffer[(x, y + 1)].set_symbol("").set_style(border_style);
    buffer[(x + width - 1, y + 1)]
        .set_symbol("")
        .set_style(border_style);
    let padded = format!(
        "{combined:^width$}",
        width = usize::from(width).saturating_sub(2)
    );
    write_dial_text(buffer, x, y + 1, width, &padded, time_style);

    // Bottom arc
    buffer[(x, y + 2)].set_symbol("").set_style(border_style);
    for dx in 1..width.saturating_sub(1) {
        buffer[(x + dx, y + 2)]
            .set_symbol("")
            .set_style(border_style);
    }
    buffer[(x + width - 1, y + 2)]
        .set_symbol("")
        .set_style(border_style);
}

fn write_dial_text(
    buffer: &mut ratatui::buffer::Buffer,
    x: u16,
    y: u16,
    width: u16,
    text: &str,
    style: Style,
) {
    for (i, ch) in text.chars().enumerate() {
        if let Ok(dx) = u16::try_from(i + 1)
            && dx < width - 1
        {
            buffer[(x + dx, y)]
                .set_symbol(&ch.to_string())
                .set_style(style);
        }
    }
}

fn render_view_tabs(app: &App, frame: &mut Frame, area: Rect) {
    let selected = match app.view() {
        View::FileList => Some(0),
        View::AuditLog => Some(1),
        View::Help => Some(2),
    };

    let tab_block = Block::default().borders(Borders::ALL);
    let tabs_inner = tab_block.inner(area);
    frame.render_widget(tab_block, area);

    let Some(selected) = selected else {
        frame.render_widget(Paragraph::new(""), tabs_inner);
        return;
    };

    let titles = vec![
        Line::from(vec![
            Span::styled("MAIN DASHBOARD", Style::default()),
            Span::raw(" [1]"),
        ]),
        Line::from(vec![
            Span::styled("AUDIT LOG", Style::default()),
            Span::raw(" [2]"),
        ]),
        Line::from(vec![
            Span::styled("HELP MENU", Style::default()),
            Span::raw(" [3]"),
        ]),
    ];

    let tabs = Tabs::new(titles)
        .select(selected)
        .style(Style::default().fg(Color::DarkGray))
        .highlight_style(Style::default().fg(Color::Reset))
        .divider(Span::styled("", Style::default().fg(Color::DarkGray)));

    frame.render_widget(tabs, tabs_inner);
}

/// Render the file list view with sidebar.
///
/// Displays tracked directories in left sidebar (20% width) and files from selected
/// directory in main panel (80% width). Shows header with stats for current view.
// Allow: This function orchestrates the two-panel layout and needs to handle
// sidebar, main panel, and header rendering. Breaking it up would make the layout
// coordination less clear.
#[allow(clippy::too_many_lines)]
fn render_file_list_view(
    app: &mut App,
    config: &Config,
    frame: &mut Frame,
    area: ratatui::layout::Rect,
) {
    // Split vertically: top widgets row | content area
    let v_chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(7), // Top row: lifecycle + timeline widgets
            Constraint::Min(0),    // Content area (sidebar + main panel)
        ])
        .split(area);

    // Split top row horizontally: lifecycle widget | expiration timeline
    let top_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
        .split(v_chunks[0]);

    // Render both top widgets
    render_lifecycle_widget(app, config, frame, top_chunks[0]);
    render_expiration_timeline(app, config, frame, top_chunks[1]);

    let content_area = v_chunks[1];

    if app.sidebar_visible() {
        // Split content area horizontally: sidebar | main panel
        let h_chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Percentage(20), // Sidebar for directories
                Constraint::Percentage(80), // Main panel for files
            ])
            .split(content_area);

        // Render sidebar with tracked directories
        render_sidebar(app, config, frame, h_chunks[0]);

        // Render main panel with entries from current path
        render_main_entry_panel(app, config, frame, h_chunks[1]);
    } else {
        // Sidebar hidden - main panel takes full width
        render_main_entry_panel(app, config, frame, content_area);
    }
}

/// A single lifecycle category's file count and byte total, converted to unsigned.
struct LifecycleTally {
    files: u64,
    bytes: u64,
}

/// Pre-computed unsigned view of `Stats` for the overview widget, with pending
/// and overdue merged into a single "overdue" bucket for display purposes.
struct LifecycleView {
    total_files: u64,
    total_bytes: u64,
    healthy: LifecycleTally,
    warning: LifecycleTally,
    overdue: LifecycleTally,
}

impl From<&crate::db::Stats> for LifecycleView {
    fn from(stats: &crate::db::Stats) -> Self {
        // Clamp to zero before casting — the schema guarantees non-negative values,
        // but defensive conversion avoids wrapping on corrupt data. The max(0) makes
        // the cast safe; clippy can't prove this statically.
        #[allow(clippy::cast_sign_loss)]
        let clamp = |v: i64| -> u64 { v.max(0) as u64 };

        Self {
            total_files: clamp(stats.total_files),
            total_bytes: clamp(stats.total_size_bytes),
            healthy: LifecycleTally {
                files: clamp(stats.files_healthy),
                bytes: clamp(stats.bytes_healthy),
            },
            warning: LifecycleTally {
                files: clamp(stats.files_within_warning),
                bytes: clamp(stats.bytes_within_warning),
            },
            overdue: LifecycleTally {
                files: clamp(stats.files_overdue) + clamp(stats.files_pending_approval),
                bytes: clamp(stats.bytes_overdue) + clamp(stats.bytes_pending_approval),
            },
        }
    }
}

impl LifecycleView {
    /// Compute lifecycle view from a list of entries.
    ///
    /// Categorizes entries by lifecycle status based on their expiration state:
    /// - Healthy: tracked entries with more than `warning_days` until expiration
    /// - Warning: tracked entries within the warning period
    /// - Overdue: entries past expiration
    ///
    /// Directories and ignored/removed entries are excluded from the tally.
    fn from_entries(entries: &[crate::db::Entry], config: &Config) -> Self {
        let mut healthy = LifecycleTally { files: 0, bytes: 0 };
        let mut warning = LifecycleTally { files: 0, bytes: 0 };
        let mut overdue = LifecycleTally { files: 0, bytes: 0 };

        for entry in entries
            .iter()
            .filter(|e| !e.is_dir && e.status != "removed" && e.status != "ignored")
        {
            // Clamp size to non-negative for safe casting
            #[allow(clippy::cast_sign_loss)]
            let size = entry.size_bytes.max(0) as u64;

            // Calculate days remaining from the active countdown timestamp.
            let days_remaining = if entry.status == "deferred" {
                entry.deferred_until.map(|until| {
                    let now = jiff::Timestamp::now().as_second();
                    (until - now) / 86400
                })
            } else {
                entry
                    .countdown_start
                    .map(|cs| calculate_expiration(cs, config.expiration_days))
                    .or_else(|| {
                        if entry.status == "pending" || entry.status == "approved" {
                            // Defensive fallback for historical rows missing countdown_start.
                            Some(0)
                        } else {
                            None
                        }
                    })
            };

            match days_remaining {
                Some(d) if d <= 0 => {
                    overdue.files += 1;
                    overdue.bytes += size;
                }
                Some(d) if d <= i64::from(config.warning_days) => {
                    warning.files += 1;
                    warning.bytes += size;
                }
                // None (no countdown_start) or Some with days > warning_days: assume healthy
                _ => {
                    healthy.files += 1;
                    healthy.bytes += size;
                }
            }
        }

        Self {
            total_files: healthy.files + warning.files + overdue.files,
            total_bytes: healthy.bytes + warning.bytes + overdue.bytes,
            healthy,
            warning,
            overdue,
        }
    }
}

/// Render the lifecycle widget showing stats for the selected root.
///
/// Layout (7 rows total = border + 5 content + border):
///   Row 0: Top border with "LIFECYCLE" title
///   Row 1: Summary — total in white, then colored lifecycle tallies
///   Row 2: Thin divider line
///   Row 3: Files lifecycle bar
///   Row 4: Thin divider line
///   Row 5: Bytes lifecycle bar
///   Row 6: Bottom border
///
/// If no root is selected, shows a placeholder message.
fn render_lifecycle_widget(
    app: &App,
    config: &Config,
    frame: &mut Frame,
    area: ratatui::layout::Rect,
) {
    let title = if app.loading.root_entries {
        "LIFECYCLE ..."
    } else {
        "LIFECYCLE"
    };
    let block = Block::default().borders(Borders::ALL).title(title);
    let inner = block.inner(area);
    frame.render_widget(block, area);

    if app.current_root_id.is_none() {
        let msg = Paragraph::new("Select a root from the sidebar")
            .style(Style::default().fg(Color::DarkGray));
        frame.render_widget(msg, inner);
        return;
    }

    let view = LifecycleView::from_entries(&app.root_entries, config);

    // Split inner area: summary + divider on top (2 rows), bars below (3 rows)
    let v_chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(2), Constraint::Length(3)])
        .split(inner);

    let summary_area = v_chunks[0];
    let bars_area = v_chunks[1];

    let summary_line = build_summary_line(&view, app.status_message.as_deref());

    let full_divider = Line::from(Span::styled(
        "".repeat(usize::from(summary_area.width)),
        Style::default().fg(Color::DarkGray),
    ));

    let summary_widget = Paragraph::new(vec![summary_line, full_divider]);
    frame.render_widget(summary_widget, summary_area);

    // Build lifecycle bars sized to full width. The "Files " / "Bytes " prefix is 6 chars.
    let prefix_width: u16 = 6;
    let bar_width = bars_area.width.saturating_sub(prefix_width);

    let files_segments = [
        BarSegment {
            value: view.healthy.files,
            label: view.healthy.files.to_string(),
            color: palette::GREEN,
        },
        BarSegment {
            value: view.warning.files,
            label: view.warning.files.to_string(),
            color: palette::YELLOW,
        },
        BarSegment {
            value: view.overdue.files,
            label: view.overdue.files.to_string(),
            color: palette::RED,
        },
    ];
    let bytes_segments = [
        BarSegment {
            value: view.healthy.bytes,
            label: format_bytes(view.healthy.bytes),
            color: palette::GREEN,
        },
        BarSegment {
            value: view.warning.bytes,
            label: format_bytes(view.warning.bytes),
            color: palette::YELLOW,
        },
        BarSegment {
            value: view.overdue.bytes,
            label: format_bytes(view.overdue.bytes),
            color: palette::RED,
        },
    ];

    let files_bar = build_lifecycle_bar(&files_segments, bar_width);
    let bytes_bar = build_lifecycle_bar(&bytes_segments, bar_width);

    let mut files_spans = vec![Span::styled(
        "Files ",
        Style::default().add_modifier(Modifier::DIM),
    )];
    files_spans.extend(files_bar);
    let files_line = Line::from(files_spans);

    let bar_divider = Line::from(Span::styled(
        "".repeat(usize::from(bars_area.width)),
        Style::default().fg(Color::DarkGray),
    ));

    let mut bytes_spans = vec![Span::styled(
        "Bytes ",
        Style::default().add_modifier(Modifier::DIM),
    )];
    bytes_spans.extend(bytes_bar);
    let bytes_line = Line::from(bytes_spans);

    let bars_widget = Paragraph::new(vec![files_line, bar_divider, bytes_line]);
    frame.render_widget(bars_widget, bars_area);
}

/// Render the expiration timeline showing when files expire over the next 30 days.
///
/// The timeline is a horizontal strip with markers indicating when files will expire.
/// Files are bucketed by day, with the count shown below each marker.
// Allow: This function handles timeline rendering with multiple layout stages (axis labels,
// timeline, markers, summary) that form a cohesive visualization pipeline.
#[allow(clippy::too_many_lines)]
fn render_expiration_timeline(
    app: &App,
    config: &Config,
    frame: &mut Frame,
    area: ratatui::layout::Rect,
) {
    const TIMELINE_DAYS: usize = 30;

    let title = if app.loading.root_entries {
        "REMOVAL TIMELINE ..."
    } else {
        "REMOVAL TIMELINE"
    };
    let block = Block::default().borders(Borders::ALL).title(title);
    let inner = block.inner(area);
    frame.render_widget(block, area);
    let content = inner.inner(Margin {
        horizontal: 1,
        vertical: 0,
    });

    if app.current_root_id.is_none() {
        let msg = Paragraph::new("Select a root from the sidebar")
            .style(Style::default().fg(Color::DarkGray));
        frame.render_widget(msg, content);
        return;
    }

    // Separate overdue backlog from the upcoming 30-day distribution.
    let mut future_buckets: [u64; TIMELINE_DAYS + 1] = [0; TIMELINE_DAYS + 1];
    let mut overdue_count: u64 = 0;
    let mut overdue_bytes: i64 = 0;
    let mut future_count: u64 = 0;
    let mut future_bytes: i64 = 0;

    for entry in app
        .root_entries
        .iter()
        .filter(|e| !e.is_dir && e.status != "removed" && e.status != "ignored")
    {
        // Calculate days until expiration using the effective countdown timestamp.
        // Approved/pending entries still belong on their true due date in the timeline.
        let days_remaining = if entry.status == "deferred" {
            entry.deferred_until.map(|until| {
                let now = jiff::Timestamp::now().as_second();
                (until - now) / 86400
            })
        } else {
            entry
                .countdown_start
                .map(|cs| calculate_expiration(cs, config.expiration_days))
                .or_else(|| {
                    if entry.status == "pending" || entry.status == "approved" {
                        // Defensive fallback when historical records are missing countdown_start.
                        Some(0)
                    } else {
                        None
                    }
                })
        };

        if let Some(days) = days_remaining {
            if days < 0 {
                overdue_count += 1;
                overdue_bytes += entry.size_bytes;
            } else if days <= i64::try_from(TIMELINE_DAYS).unwrap_or(i64::MAX) {
                #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
                let bucket_idx = days as usize;
                future_buckets[bucket_idx] += 1;
                future_count += 1;
                future_bytes += entry.size_bytes;
            }
        }
    }

    // Debug: log non-zero buckets so we can verify deferred entries land correctly.
    if tracing::enabled!(tracing::Level::DEBUG) {
        let non_zero: Vec<_> = future_buckets
            .iter()
            .enumerate()
            .filter(|(_, count)| **count > 0)
            .map(|(day, count)| format!("d{day}={count}"))
            .collect();
        if !non_zero.is_empty() {
            tracing::debug!(
                target: "stagecrew::timeline",
                buckets = %non_zero.join(" "),
                overdue_count,
                future_count,
                "Timeline bucket distribution"
            );
        }
    }

    // If nothing is overdue or expiring soon, show a calm message.
    if overdue_count == 0 && future_count == 0 {
        let msg = Paragraph::new("No files expiring in the next 30 days")
            .style(Style::default().fg(Color::DarkGray));
        frame.render_widget(msg, content);
        return;
    }

    // Layout: labels, chart with axes, metric row, and summary.
    let v_chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Min(1),
        ])
        .split(content);

    let labels_area = v_chunks[0];
    let chart_area = v_chunks[1];
    let metrics_area = v_chunks[2];
    let summary_area = v_chunks[3];

    let overdue_width = 22;
    let y_label_width: u16 = 6; // "files│"
    let h_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(overdue_width),
            Constraint::Length(2),
            Constraint::Min(10),
        ])
        .split(chart_area);

    let overdue_area = h_chunks[0];
    let future_area = h_chunks[2];

    let label_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(overdue_width),
            Constraint::Length(2),
            Constraint::Length(y_label_width),
            Constraint::Min(5),
        ])
        .split(labels_area);

    frame.render_widget(
        Paragraph::new(Line::from(Span::styled(
            "overdue backlog",
            Style::default().fg(Color::DarkGray),
        ))),
        label_chunks[0],
    );
    frame.render_widget(
        Paragraph::new(build_future_axis_labels(label_chunks[3].width))
            .style(Style::default().fg(Color::DarkGray)),
        label_chunks[3],
    );

    render_overdue_block(
        overdue_area,
        frame,
        overdue_count,
        overdue_bytes,
        overdue_count + future_count,
    );

    // Split future area: "files│" label + y-axis line on left, sparkline on right.
    let future_h = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Length(y_label_width), Constraint::Min(5)])
        .split(future_area);

    frame.render_widget(
        Paragraph::new(Line::from(vec![
            Span::styled("files", Style::default().fg(Color::DarkGray)),
            Span::styled("", Style::default().fg(Color::DarkGray)),
        ]))
        .alignment(Alignment::Right),
        future_h[0],
    );

    let sparkline = build_future_sparkline(&future_buckets, future_h[1].width, config.warning_days);
    frame.render_widget(Paragraph::new(sparkline), future_h[1]);

    let metric_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(overdue_width),
            Constraint::Length(2),
            Constraint::Length(y_label_width),
            Constraint::Min(5),
        ])
        .split(metrics_area);

    // Baseline corner aligned with y-axis │ above it.
    frame.render_widget(
        Paragraph::new(Line::from(Span::styled(
            "",
            Style::default().fg(Color::DarkGray),
        )))
        .alignment(Alignment::Right),
        metric_chunks[2],
    );

    // Baseline line under sparkline: ───────
    let baseline_width = usize::from(metric_chunks[3].width);
    let baseline: String = "".repeat(baseline_width);
    frame.render_widget(
        Paragraph::new(Line::from(Span::styled(
            baseline,
            Style::default().fg(Color::DarkGray),
        ))),
        metric_chunks[3],
    );

    let overdue_metric = if overdue_count == 0 {
        String::new()
    } else {
        format!(
            "{} overdue",
            format_bytes(overdue_bytes.max(0).cast_unsigned())
        )
    };
    let future_metric = if future_count == 0 {
        "nothing due soon".to_string()
    } else {
        format!(
            "{} due soon",
            format_bytes(future_bytes.max(0).cast_unsigned())
        )
    };
    let combined_metric = if overdue_metric.is_empty() {
        future_metric
    } else if future_metric.is_empty() || future_metric == "nothing due soon" {
        overdue_metric
    } else {
        format!("{overdue_metric} · {future_metric}")
    };
    frame.render_widget(
        Paragraph::new(Line::from(Span::styled(
            combined_metric,
            Style::default().fg(Color::DarkGray),
        ))),
        metric_chunks[0],
    );

    let summary_text = match (overdue_count, future_count) {
        (0, upcoming) => format!(
            "{} due in next 30 days ({})",
            pluralize_files(upcoming),
            format_bytes(future_bytes.max(0).cast_unsigned())
        ),
        (overdue, 0) => format!(
            "{} overdue ({})",
            pluralize_files(overdue),
            format_bytes(overdue_bytes.max(0).cast_unsigned())
        ),
        (overdue, upcoming) => format!(
            "{} overdue | {} due in next 30 days",
            pluralize_files(overdue),
            pluralize_files(upcoming)
        ),
    };

    let summary_paragraph = Paragraph::new(Line::from(Span::styled(
        summary_text,
        Style::default().add_modifier(Modifier::BOLD),
    )))
    .alignment(Alignment::Center);
    let summary_rows = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Min(0), Constraint::Length(1)])
        .split(summary_area);
    frame.render_widget(summary_paragraph, summary_rows[1]);
}

fn build_future_axis_labels(width: u16) -> Line<'static> {
    let axis_width = usize::from(width);
    let mut axis_label = String::with_capacity(axis_width);
    axis_label.push_str("today");
    let mid_label = "+15d";
    let end_label = "+30d";

    let mid_pos = axis_width / 2;
    let end_pos = axis_width.saturating_sub(end_label.len());

    let padding_to_mid = mid_pos
        .saturating_sub(axis_label.len())
        .saturating_sub(mid_label.len() / 2);
    for _ in 0..padding_to_mid {
        axis_label.push(' ');
    }
    axis_label.push_str(mid_label);

    let padding_to_end = end_pos.saturating_sub(axis_label.len());
    for _ in 0..padding_to_end {
        axis_label.push(' ');
    }
    axis_label.push_str(end_label);

    Line::from(axis_label)
}

fn render_overdue_block(
    area: Rect,
    frame: &mut Frame,
    overdue_count: u64,
    _overdue_bytes: i64,
    total_count: u64,
) {
    let inner_width = area.width.saturating_sub(1);
    let blocks = u16::try_from(
        overdue_count
            .saturating_mul(u64::from(inner_width))
            .saturating_add(total_count.saturating_sub(1))
            / total_count.max(1),
    )
    .unwrap_or(inner_width)
    .max(if overdue_count > 0 { 4 } else { 0 })
    .min(inner_width);
    let bar = "".repeat(usize::from(blocks));
    if overdue_count == 0 {
        frame.render_widget(
            Paragraph::new(Line::from(Span::styled(
                "✓ all clear",
                Style::default().fg(palette::GREEN),
            ))),
            area,
        );
        return;
    }
    let count_text = pluralize_files(overdue_count);
    frame.render_widget(
        Paragraph::new(Line::from(vec![
            Span::styled(bar, Style::default().fg(palette::RED)),
            Span::raw(" "),
            Span::styled(count_text, Style::default().add_modifier(Modifier::BOLD)),
        ])),
        area,
    );
}

fn build_future_sparkline(buckets: &[u64], width: u16, warning_days: u32) -> Line<'static> {
    const BLOCKS: [char; 8] = ['', '', '', '', '', '', '', ''];

    if width == 0 || buckets.is_empty() {
        return Line::from(String::new());
    }

    let samples = sample_future_buckets(buckets, usize::from(width));
    let max_value = samples.iter().map(|(count, _)| *count).max().unwrap_or(0);
    if max_value == 0 {
        return Line::from(Span::styled(
            "·".repeat(usize::from(width)),
            Style::default().fg(Color::DarkGray),
        ));
    }

    let spans: Vec<Span<'static>> = samples
        .into_iter()
        .map(|(count, day)| {
            // Ensure non-zero buckets are always visible (at least ▂).
            let idx = if count == 0 {
                0
            } else {
                usize::try_from(
                    count
                        .saturating_mul(u64::try_from(BLOCKS.len()).unwrap_or(8))
                        .saturating_sub(1)
                        / max_value,
                )
                .unwrap_or(0)
                .min(BLOCKS.len().saturating_sub(1))
                .max(1)
            };
            if count == 0 {
                return Span::styled(" ", Style::default());
            }
            let color = if day == 0 {
                palette::RED
            } else if day <= usize::try_from(warning_days).unwrap_or(0) {
                palette::YELLOW
            } else {
                palette::GREEN
            };
            Span::styled(BLOCKS[idx].to_string(), Style::default().fg(color))
        })
        .collect();

    Line::from(spans)
}

fn sample_future_buckets(buckets: &[u64], width: usize) -> Vec<(u64, usize)> {
    (0..width)
        .map(|column| {
            let start = column.saturating_mul(buckets.len()) / width;
            let end = ((column + 1).saturating_mul(buckets.len()) / width).max(start + 1);
            let slice = &buckets[start..end.min(buckets.len())];
            let count = slice.iter().copied().sum::<u64>();
            let day = slice
                .iter()
                .enumerate()
                .find(|(_, count)| **count > 0)
                .map_or(start, |(offset, _)| start + offset);
            (count, day.min(buckets.len().saturating_sub(1)))
        })
        .collect()
}

fn pluralize_files(count: u64) -> String {
    format!("{count} file{}", if count == 1 { "" } else { "s" })
}

/// Render the quota target widget (pie chart with text above, or placeholder message).
// Allow: This function handles multiple early-return cases for different states (no root,
// error, no target) before the main rendering logic. Breaking it up would obscure the flow.
#[allow(clippy::too_many_lines)]
fn render_quota_widget(app: &App, config: &Config, frame: &mut Frame, area: ratatui::layout::Rect) {
    // Render a bordered block for the quota widget
    let title = if app.loading.root_entries {
        "QUOTA ..."
    } else {
        "QUOTA"
    };
    let block = Block::default().borders(Borders::ALL).title(title);
    let inner = block.inner(area);
    frame.render_widget(block, area);

    // Get the current root ID
    let Some(root_id) = app.current_root_id else {
        let msg = Paragraph::new("Select a root")
            .alignment(ratatui::layout::Alignment::Center)
            .style(Style::default().fg(Color::DarkGray));
        frame.render_widget(msg, inner);
        return;
    };

    let Some(root) = app.roots.iter().find(|r| r.id == root_id) else {
        let msg = Paragraph::new("Not found")
            .alignment(ratatui::layout::Alignment::Center)
            .style(Style::default().fg(palette::RED));
        frame.render_widget(msg, inner);
        return;
    };

    let Some(target_bytes) = root.target_bytes else {
        let msg = Paragraph::new("Press t to set")
            .alignment(ratatui::layout::Alignment::Center)
            .style(Style::default().fg(Color::DarkGray));
        frame.render_widget(msg, inner);
        return;
    };

    if app.loading.root_entries {
        let msg = Paragraph::new("Loading...")
            .alignment(ratatui::layout::Alignment::Center)
            .style(Style::default().fg(Color::DarkGray));
        frame.render_widget(msg, inner);
        return;
    }

    // Categorize bytes using exactly the same lifecycle logic as the top widget.
    // This keeps the quota pie and lifecycle bars semantically aligned.
    let view = LifecycleView::from_entries(&app.root_entries, config);
    let clamp_i64 = |v: u64| i64::try_from(v).unwrap_or(i64::MAX);
    let (healthy_bytes, warning_bytes, overdue_bytes) = (
        clamp_i64(view.healthy.bytes),
        clamp_i64(view.warning.bytes),
        clamp_i64(view.overdue.bytes),
    );

    let used_bytes = healthy_bytes + warning_bytes + overdue_bytes;

    // Calculate values for display. Precision loss is acceptable since we only
    // display integer percentages.
    #[allow(clippy::cast_precision_loss)]
    let target_f64 = target_bytes.max(1) as f64;
    #[allow(clippy::cast_precision_loss)]
    let used_f64 = used_bytes.max(0) as f64;

    // Split inner area: text on top (2 rows), pie chart below
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(2), Constraint::Min(1)])
        .split(inner);

    let text_area = chunks[0];
    let chart_area = chunks[1];

    // Build the text label above the chart
    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    let pct_display = (used_f64 / target_f64 * 100.0).round() as u32;

    #[allow(clippy::cast_sign_loss)]
    let used_display = crate::format_bytes(used_bytes.max(0) as u64);
    #[allow(clippy::cast_sign_loss)]
    let target_display = crate::format_bytes(target_bytes.max(0) as u64);

    // Text color: red if over quota, otherwise terminal default
    let text_color = if used_bytes > target_bytes {
        palette::RED
    } else {
        Color::Reset
    };

    let text_content = if used_bytes > target_bytes {
        #[allow(clippy::cast_sign_loss)]
        let overage = crate::format_bytes((used_bytes - target_bytes).max(0) as u64);
        vec![
            Line::from(Span::styled(
                format!("{pct_display}% ({overage} over)"),
                Style::default().fg(text_color),
            )),
            Line::from(format!("{used_display} / {target_display}")),
        ]
    } else {
        vec![
            Line::from(Span::styled(
                format!("{pct_display}%"),
                Style::default().fg(text_color),
            )),
            Line::from(format!("{used_display} / {target_display}")),
        ]
    };

    let text_widget = Paragraph::new(text_content)
        .alignment(ratatui::layout::Alignment::Center)
        .style(Style::default().fg(text_color));
    frame.render_widget(text_widget, text_area);

    // Constrain chart area to be square (use height as the limiting dimension,
    // accounting for braille's 2:1 aspect ratio where each cell is 2 wide x 4 tall)
    let chart_height = chart_area.height;
    // Each braille cell is roughly 2 chars wide for 4 dots tall, so multiply height by 2
    let ideal_width = chart_height.saturating_mul(2);
    let actual_width = chart_area.width.min(ideal_width);
    let x_offset = (chart_area.width.saturating_sub(actual_width)) / 2;
    let square_chart_area = ratatui::layout::Rect {
        x: chart_area.x + x_offset,
        y: chart_area.y,
        width: actual_width,
        height: chart_height,
    };

    // Create pie slices based on lifecycle status and quota headroom.
    let slices = if used_bytes > target_bytes {
        // Over quota: show solid red. We use 99.99% + 0.01% because tui-piechart
        // doesn't render single-slice charts correctly (shows as a thin line).
        vec![
            tui_piechart::PieSlice::new("", 99.99, palette::RED),
            tui_piechart::PieSlice::new("", 0.01, palette::RED),
        ]
    } else {
        // Under quota: show healthy (green), warning (yellow), overdue (red), remaining (gray)
        let remaining_bytes = (target_bytes - used_bytes).max(0);

        #[allow(clippy::cast_precision_loss)]
        let healthy_pct = healthy_bytes.max(0) as f64 / target_f64 * 100.0;
        #[allow(clippy::cast_precision_loss)]
        let warning_pct = warning_bytes.max(0) as f64 / target_f64 * 100.0;
        #[allow(clippy::cast_precision_loss)]
        let overdue_pct = overdue_bytes.max(0) as f64 / target_f64 * 100.0;
        #[allow(clippy::cast_precision_loss)]
        let remaining_pct = remaining_bytes as f64 / target_f64 * 100.0;

        let mut slices = Vec::new();
        if healthy_pct > 0.0 {
            slices.push(tui_piechart::PieSlice::new("", healthy_pct, palette::GREEN));
        }
        if warning_pct > 0.0 {
            slices.push(tui_piechart::PieSlice::new(
                "",
                warning_pct,
                palette::YELLOW,
            ));
        }
        if overdue_pct > 0.0 {
            slices.push(tui_piechart::PieSlice::new("", overdue_pct, palette::RED));
        }
        if remaining_pct > 0.0 {
            slices.push(tui_piechart::PieSlice::new(
                "",
                remaining_pct,
                Color::DarkGray,
            ));
        }

        // If no slices (empty root with target), show all remaining
        if slices.is_empty() {
            slices.push(tui_piechart::PieSlice::new("", 100.0, Color::DarkGray));
        }

        slices
    };

    let chart = tui_piechart::PieChart::new(slices)
        .show_legend(false)
        .high_resolution(true);

    frame.render_widget(chart, square_chart_area);
}

/// Build the summary line showing total files and bytes, plus optional status message.
fn build_summary_line<'a>(view: &LifecycleView, status_message: Option<&str>) -> Line<'a> {
    let mut spans = vec![Span::styled(
        format!(
            "Total: {} files, {}",
            view.total_files,
            format_bytes(view.total_bytes)
        ),
        Style::default().add_modifier(Modifier::BOLD),
    )];

    if let Some(status) = status_message {
        spans.push(Span::raw("  "));
        spans.push(Span::styled(
            status.to_owned(),
            Style::default().add_modifier(Modifier::DIM),
        ));
    }

    Line::from(spans)
}

/// Build a proportional lifecycle bar as colored spans.
///
/// Each segment is rendered as a colored background band with the label centered
/// in dark text. When a segment is too narrow for its label, it shows as a solid
/// colored band. An empty bar (all zeros) renders as a solid dark gray band.
///
/// A single segment of a lifecycle bar: a proportional value, display label, and color.
struct BarSegment {
    value: u64,
    label: String,
    color: Color,
}

/// Build a proportional lifecycle bar as colored spans from a slice of segments.
///
/// Each segment is rendered as a colored background band with the label centered
/// in dark text. When a segment is too narrow for its label, it shows as a solid
/// colored band. An empty bar (all zeros) renders as a solid dark gray band.
fn build_lifecycle_bar(segments: &[BarSegment], width: u16) -> Vec<Span<'static>> {
    let w = usize::from(width);
    let total: u64 = segments.iter().map(|s| s.value).sum();

    if total == 0 || w == 0 {
        return vec![Span::styled(
            " ".repeat(w),
            Style::default().bg(Color::DarkGray),
        )];
    }

    let values: Vec<u64> = segments.iter().map(|s| s.value).collect();
    let widths = proportional_widths(&values, total, w);

    let mut spans = Vec::new();
    for (seg, &seg_width) in segments.iter().zip(&widths) {
        if seg_width > 0 {
            spans.push(build_bar_segment(seg_width, seg.color, &seg.label));
        }
    }

    spans
}

/// Build a single colored bar segment as a background-colored band with centered text.
///
/// The segment is spaces with a colored background. If the segment is wide enough
/// for the label, the label is centered within it in dark text. Otherwise the
/// segment is a solid colored band with no text.
fn build_bar_segment(width: usize, color: Color, label: &str) -> Span<'static> {
    let label_len = label.len();
    let content = if width >= label_len {
        // Center the label within spaces
        let total_padding = width - label_len;
        let left_pad = total_padding / 2;
        let right_pad = total_padding - left_pad;
        format!("{}{}{}", " ".repeat(left_pad), label, " ".repeat(right_pad),)
    } else {
        " ".repeat(width)
    };

    Span::styled(content, Style::default().bg(color).fg(Color::Black))
}

/// Distribute `width` characters proportionally across segments using integer math.
///
/// Non-zero values get at least 1 character. The returned widths sum to exactly
/// `width`. Uses u128 intermediates to avoid overflow on large byte totals.
fn proportional_widths(values: &[u64], total: u64, width: usize) -> Vec<usize> {
    let total_128 = u128::from(total);
    let width_128 = width as u128;

    // Initial proportional assignment via integer division with rounding
    let mut widths: Vec<usize> = values
        .iter()
        .map(|&v| {
            if total_128 == 0 {
                0
            } else {
                // Round to nearest: (v * width + total/2) / total.
                // Result is bounded by width (a usize), so truncation is safe.
                #[allow(clippy::cast_possible_truncation)]
                let w = (u128::from(v) * width_128 + total_128 / 2) / total_128;
                w as usize
            }
        })
        .collect();

    // Guarantee at least 1 char for non-zero values
    for (i, &v) in values.iter().enumerate() {
        if v > 0 && widths[i] == 0 {
            widths[i] = 1;
        }
    }

    // Adjust to sum to exactly width by modifying the largest segment
    let sum: usize = widths.iter().sum();
    if sum != width
        && let Some(max_idx) = widths
            .iter()
            .enumerate()
            .max_by_key(|&(_, &w)| w)
            .map(|(i, _)| i)
    {
        if sum > width {
            widths[max_idx] = widths[max_idx].saturating_sub(sum - width);
        } else {
            widths[max_idx] += width - sum;
        }
    }

    widths
}

/// Render the sidebar showing tracked roots and quota widget.
fn render_sidebar(app: &mut App, config: &Config, frame: &mut Frame, area: ratatui::layout::Rect) {
    // Split sidebar: roots list on top, quota widget on bottom (fixed 14 rows)
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Min(3), Constraint::Length(14)])
        .split(area);

    let roots_area = chunks[0];
    let quota_area = chunks[1];

    // Render roots list
    render_roots_list(app, frame, roots_area);

    // Render quota widget
    render_quota_widget(app, config, frame, quota_area);
}

/// Render the roots list in the sidebar.
fn render_roots_list(app: &App, frame: &mut Frame, area: ratatui::layout::Rect) {
    let roots = &app.roots;

    // Clamp selected index
    let selected_idx = if roots.is_empty() {
        0
    } else {
        app.sidebar_selected_index().min(roots.len() - 1)
    };

    // Build sidebar rows
    let rows: Vec<Row> = roots
        .iter()
        .enumerate()
        .map(|(idx, root)| {
            // Extract just the directory name from full path
            let path_str = root.path.to_string_lossy();
            let dir_name = root
                .path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or(&path_str);

            let cell = Cell::from(dir_name.to_owned());

            // Highlight selected row and show focus
            let style = if idx == selected_idx {
                if app.focus_panel() == FocusPanel::Sidebar {
                    Style::default()
                        .add_modifier(Modifier::REVERSED)
                        .fg(palette::CYAN)
                } else {
                    Style::default().add_modifier(Modifier::REVERSED)
                }
            } else {
                Style::default()
            };

            Row::new(vec![cell]).style(style)
        })
        .collect();

    // Empty state (or loading)
    if rows.is_empty() {
        let msg = if app.loading.roots {
            "Loading..."
        } else {
            "No tracked paths.\n\nRun 'stagecrew add PATH'"
        };
        let empty_text = Paragraph::new(msg)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title("TRACKED DIRECTORIES"),
            )
            .style(Style::default().fg(Color::DarkGray));
        frame.render_widget(empty_text, area);
        return;
    }

    let table = Table::new(rows, [Constraint::Percentage(100)]).block(
        Block::default()
            .title("TRACKED DIRECTORIES")
            .borders(Borders::ALL)
            .border_style(if app.focus_panel() == FocusPanel::Sidebar {
                Style::default().fg(palette::CYAN)
            } else {
                Style::default()
            }),
    );

    frame.render_widget(table, area);
}

/// Render the main panel showing entries from the current path.
// Allow: This function handles entry loading, sorting, and table rendering which are
// sequential operations that form a cohesive rendering pipeline.
#[allow(clippy::too_many_lines)]
fn render_main_entry_panel(
    app: &mut App,
    config: &Config,
    frame: &mut Frame,
    area: ratatui::layout::Rect,
) {
    // Get the current path for browsing
    let current_path = app.current_path();

    // If current_path is empty, show a message to select a root (or loading)
    if current_path.as_os_str().is_empty() {
        let msg = if app.loading.roots {
            "Loading..."
        } else {
            "Select a root from the sidebar\n\n(Use j/k to navigate, Tab to switch panels)"
        };
        let message = Paragraph::new(msg)
            .block(Block::default().borders(Borders::ALL).title("ENTRIES"))
            .style(Style::default().fg(Color::DarkGray));
        frame.render_widget(message, area);
        return;
    }

    // Empty state (or loading)
    if app.dir_entries.is_empty() {
        let msg = if app.loading.dir_entries {
            "Loading..."
        } else {
            "No entries in this directory"
        };
        let empty_text = Paragraph::new(msg)
            .block(Block::default().borders(Borders::ALL).title("ENTRIES"))
            .style(Style::default().fg(Color::DarkGray));
        frame.render_widget(empty_text, area);
        return;
    }

    // Update entry list length for navigation
    app.entry_list_len = app.dir_entries.len();

    // Clamp selected index
    let selected_idx = if app.dir_entries.is_empty() {
        0
    } else {
        app.entry_selected_index().min(app.dir_entries.len() - 1)
    };

    // Compute search matches for highlighting
    let search_match_set: std::collections::HashSet<usize> = app
        .search_query
        .as_ref()
        .map(|q| {
            super::input::find_search_matches(&app.dir_entries, q)
                .into_iter()
                .collect()
        })
        .unwrap_or_default();

    let entry_rows = &app.dir_entries;

    // Calculate viewport height for gradient fade effect
    // This determines how aggressively rows fade based on distance from cursor
    let viewport_height = area.height.saturating_sub(4) as usize; // borders + header + margin
    let gradient = fade_gradient();

    // Build entry table rows
    let rows: Vec<Row> = entry_rows
        .iter()
        .enumerate()
        .map(|(idx, (entry, days_remaining))| {
            // Calculate fade percentage based on distance from cursor
            // Cursor row = 0% fade (full brightness), edge rows = up to 100% fade
            let fade_pct = FadeGradient::fade_percent(idx, selected_idx, viewport_height);

            // Countdown indicator + workflow marker (pending/approved)
            let (indicator_symbol, indicator_color) = expiration_indicator_entry(
                &entry.status,
                *days_remaining,
                config.warning_days,
                entry,
            );
            let (workflow_symbol, workflow_color) = workflow_indicator(&entry.status);
            let indicator_cell = Cell::from(Line::from(vec![
                Span::styled(
                    indicator_symbol.to_string(),
                    Style::default().fg(indicator_color),
                ),
                Span::styled(
                    workflow_symbol.to_string(),
                    Style::default().fg(workflow_color),
                ),
            ]));

            // Extract filename from path with directory indicator
            let path_str = entry.path.to_string_lossy();
            let filename = entry
                .path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or(&path_str);
            let display_name = if entry.is_dir {
                format!("{filename}/")
            } else {
                filename.to_string()
            };
            // Format size for both files and directories.
            // Directory sizes are recursively aggregated during scan persistence.
            // Allow: size_bytes is guaranteed non-negative by schema, but stored as i64 for SQLite compatibility.
            #[allow(clippy::cast_sign_loss)]
            let size_str = format_bytes(entry.size_bytes as u64);

            // Calculate effective days for the Due column
            // For deferred entries, use the deferral end date instead of mtime-based expiration
            let effective_days = if entry.status == "deferred" {
                if let Some(deferred_until) = entry.deferred_until {
                    let now = jiff::Timestamp::now().as_second();
                    (deferred_until - now) / 86400
                } else {
                    *days_remaining
                }
            } else {
                *days_remaining
            };

            // Format the Due column value
            // Ignored entries show "—" since they're exempt from removal
            let due_str = if entry.status == "ignored" {
                "".to_string()
            } else if effective_days >= 0 {
                format!("{effective_days} days")
            } else {
                format!("{} days ago", -effective_days)
            };

            // Check if this entry is selected (multi-select)
            let is_selected = app.selected_entries().contains(&entry.id);
            let is_search_match = search_match_set.contains(&idx);

            // Determine if this is the cursor row - affects styling strategy
            // When REVERSED modifier is used, cell foreground colors become backgrounds,
            // creating visual artifacts. So cursor rows use plain text (no cell colors).
            let is_cursor = idx == selected_idx && app.focus_panel() == FocusPanel::MainPanel;
            let uses_reversed = is_cursor; // REVERSED swaps fg/bg, breaking cell colors

            // Build cells with gradient-faded colors based on distance from cursor
            // Cursor row and selected rows don't fade; other rows fade progressively
            let should_fade = !is_cursor && !is_selected;

            let filename_cell = if uses_reversed {
                Cell::from(display_name)
            } else if entry.status == "ignored" {
                let color = if should_fade {
                    gradient.gray[fade_pct]
                } else {
                    Color::DarkGray
                };
                Cell::from(display_name).style(Style::default().fg(color))
            } else {
                let color = if should_fade {
                    gradient.text[fade_pct]
                } else {
                    Color::Reset // Default terminal color for cursor/selected
                };
                Cell::from(display_name).style(Style::default().fg(color))
            };

            let size_cell = if uses_reversed {
                Cell::from(size_str)
            } else {
                let color = if should_fade {
                    gradient.gray[fade_pct]
                } else {
                    Color::DarkGray
                };
                Cell::from(size_str).style(Style::default().fg(color))
            };

            // Due column is countdown-driven.
            // Ignored entries are always gray.
            // The gradient fades each color toward dark gray.
            let due_color = if uses_reversed {
                Color::Reset
            } else {
                let base_gradient = match entry.status.as_str() {
                    "ignored" => &gradient.gray,
                    _ => {
                        // For tracked/pending/approved/deferred entries, color by urgency.
                        if effective_days <= 0 {
                            &gradient.red // Overdue
                        } else if effective_days <= i64::from(config.warning_days) {
                            &gradient.yellow // Warning period
                        } else {
                            &gradient.green // Safe
                        }
                    }
                };
                if should_fade {
                    base_gradient[fade_pct]
                } else {
                    base_gradient[0] // Full brightness for cursor/selected
                }
            };
            let due_cell = Cell::from(due_str).style(Style::default().fg(due_color));

            // Row-level styling for selection and cursor
            let mut row_style = if is_selected && is_cursor {
                Style::default()
                    .bg(Color::DarkGray)
                    .add_modifier(Modifier::BOLD | Modifier::REVERSED)
            } else if is_selected {
                Style::default()
                    .bg(Color::DarkGray)
                    .add_modifier(Modifier::BOLD)
            } else if is_cursor {
                // Currently focused entry
                Style::default().add_modifier(Modifier::REVERSED)
            } else {
                Style::default()
            };

            // Underline search matches so they stand out
            if is_search_match {
                row_style = row_style.add_modifier(Modifier::UNDERLINED);
            }

            Row::new(vec![indicator_cell, filename_cell, size_cell, due_cell]).style(row_style)
        })
        .collect();

    // Build table
    let widths = [
        Constraint::Length(3),      // Countdown + workflow indicator (e.g. "⚠✓")
        Constraint::Percentage(54), // Filename
        Constraint::Percentage(15), // Size
        Constraint::Percentage(28), // Due
    ];

    let sort_indicator = match app.sort_mode() {
        SortMode::Expiration => " (by due)",
        SortMode::Size => " (by size)",
        SortMode::Name => " (by name)",
        SortMode::Modified => " (by modified)",
    };

    let selection_info = if app.selected_entries().is_empty() {
        String::new()
    } else {
        format!(" | {} selected", app.selected_entries().len())
    };

    let search_info = if let Some(query) = &app.search_query {
        if query.is_empty() {
            String::new()
        } else {
            let match_count = search_match_set.len();
            format!(
                " | /{query} ({match_count} match{plural})",
                plural = if match_count == 1 { "" } else { "es" }
            )
        }
    } else {
        String::new()
    };

    let table = Table::new(rows, widths)
        .block(
            Block::default()
                .title(format!(
                    "ENTRIES{sort_indicator}{selection_info}{search_info}"
                ))
                .borders(Borders::ALL)
                .border_style(if app.focus_panel() == FocusPanel::MainPanel {
                    Style::default().fg(palette::CYAN)
                } else {
                    Style::default()
                }),
        )
        .header(
            Row::new(entry_table_header_cells(app.sort_mode()))
                .style(Style::default().add_modifier(Modifier::BOLD))
                .bottom_margin(1),
        )
        .highlight_spacing(HighlightSpacing::Never);

    // Don't set selection on TableState - we handle row highlighting manually.
    // This prevents ratatui from auto-scrolling to the selected row,
    // allowing mouse scroll to control the viewport independently.
    // We only use TableState for scroll offset management.

    // Only scroll to follow cursor when keyboard navigation requested it.
    // This allows mouse scrolling to move the viewport independently.
    if app.ensure_cursor_visible {
        let viewport_height = area.height.saturating_sub(4) as usize; // borders + header + margin
        let current_offset = app.entry_table_state.offset();
        if selected_idx < current_offset {
            // Selection is above viewport - scroll up to show it
            *app.entry_table_state.offset_mut() = selected_idx;
        } else if selected_idx >= current_offset + viewport_height && viewport_height > 0 {
            // Selection is below viewport - scroll down to show it
            *app.entry_table_state.offset_mut() = selected_idx.saturating_sub(viewport_height) + 1;
        }
        app.ensure_cursor_visible = false;
    }

    // Store area for mouse hit-testing
    app.entry_table_area = area;

    // Render the table
    frame.render_stateful_widget(table, area, &mut app.entry_table_state);

    // Render the scrollbar (inside the border, on the right edge)
    // Only render if there's content to scroll
    let viewport_height = area.height.saturating_sub(4) as usize; // borders + header + margin
    if entry_rows.len() > viewport_height {
        // content_length is the scrollable range: max_offset + 1 so position 0..=max_offset maps correctly
        let max_offset = entry_rows.len().saturating_sub(viewport_height);
        app.entry_scrollbar_state =
            ScrollbarState::new(max_offset + 1).position(app.entry_table_state.offset());

        let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
            .begin_symbol(Some(""))
            .end_symbol(Some(""))
            .track_symbol(Some(""));

        frame.render_stateful_widget(
            scrollbar,
            area.inner(Margin {
                vertical: 1,
                horizontal: 0,
            }),
            &mut app.entry_scrollbar_state,
        );
    }
}

/// Sort entry rows according to the specified sort mode.
pub(super) fn sort_entry_rows(rows: &mut [(crate::db::Entry, i64)], sort_mode: SortMode) {
    match sort_mode {
        SortMode::Expiration => {
            // Ascending (most urgent first), but:
            // - Ignored entries sort to the end (they're not expiring)
            // - Deferred entries sort by their deferred_until date
            // - Directories sort by their oldest child's mtime
            rows.sort_by(|a, b| {
                let key_a = expiration_sort_key_entry(&a.0, a.1);
                let key_b = expiration_sort_key_entry(&b.0, b.1);
                key_a.cmp(&key_b)
            });
        }
        SortMode::Size => {
            // Descending (largest first), files and directories interspersed by size.
            rows.sort_by(|a, b| {
                b.0.size_bytes
                    .cmp(&a.0.size_bytes)
                    .then_with(|| a.0.path.cmp(&b.0.path))
            });
        }
        SortMode::Name => {
            // Alphabetical ascending by filename, directories first
            rows.sort_by(|a, b| {
                // Directories first
                match (a.0.is_dir, b.0.is_dir) {
                    (true, false) => std::cmp::Ordering::Less,
                    (false, true) => std::cmp::Ordering::Greater,
                    _ => {
                        let str_a = a.0.path.to_string_lossy();
                        let name_a =
                            a.0.path
                                .file_name()
                                .and_then(|n| n.to_str())
                                .unwrap_or(&str_a);
                        let str_b = b.0.path.to_string_lossy();
                        let name_b =
                            b.0.path
                                .file_name()
                                .and_then(|n| n.to_str())
                                .unwrap_or(&str_b);
                        name_a.cmp(name_b)
                    }
                }
            });
        }
        SortMode::Modified => {
            // Descending (most recent first = highest mtime first), directories to end
            rows.sort_by(|a, b| {
                // Directories go to end (no mtime)
                match (a.0.mtime, b.0.mtime) {
                    (Some(mtime_a), Some(mtime_b)) => mtime_b.cmp(&mtime_a),
                    (Some(_), None) => std::cmp::Ordering::Less,
                    (None, Some(_)) => std::cmp::Ordering::Greater,
                    (None, None) => std::cmp::Ordering::Equal,
                }
            });
        }
    }
}

/// Compute a sort key for expiration-based sorting.
///
/// Returns an `i64` where lower values sort first (more urgent):
/// - Directories return `i64::MAX - 1` (sort near end, before ignored)
/// - Ignored entries return `i64::MAX` (sort to end)
/// - Deferred entries return days until deferral expires
/// - Other entries return their `days_remaining`
fn expiration_sort_key_entry(entry: &crate::db::Entry, days_remaining: i64) -> i64 {
    match entry.status.as_str() {
        "ignored" => i64::MAX, // Sort to end
        "deferred" => {
            // Sort by days until deferral expires
            if let Some(deferred_until) = entry.deferred_until {
                let now = jiff::Timestamp::now().as_second();
                let seconds_remaining = deferred_until - now;
                seconds_remaining / 86400 // Convert to days
            } else {
                // Deferred but no deferred_until set (shouldn't happen, but handle gracefully)
                days_remaining
            }
        }
        _ => days_remaining,
    }
}

/// Generate a visual indicator (symbol + color) for attention status (for entries).
///
/// Returns a tuple of (symbol, color) that signals when attention is needed:
/// - `●` RED: Overdue (expired, requires immediate attention)
/// - `⚠` YELLOW: Warning period (approaching expiration)
/// - `—` GRAY: Ignored (won't expire, no action needed)
/// - `📁` BLUE: Directory (no expiration)
/// - ` ` (space): Safe, no attention needed
///
/// For deferred entries, the indicator is based on the deferral end date.
fn expiration_indicator_entry(
    status: &str,
    days_remaining: i64,
    warning_days: u32,
    entry: &crate::db::Entry,
) -> (&'static str, Color) {
    // Ignored entries show a dash — they won't expire
    if status == "ignored" {
        return ("", Color::DarkGray);
    }

    // Deferred entries: calculate days until deferral expires
    let effective_days = if status == "deferred" {
        if let Some(deferred_until) = entry.deferred_until {
            let now = jiff::Timestamp::now().as_second();
            (deferred_until - now) / 86400
        } else {
            days_remaining
        }
    } else {
        days_remaining
    };

    if effective_days <= 0 {
        ("", palette::RED) // Overdue - filled circle
    } else if effective_days <= i64::from(warning_days) {
        ("", palette::YELLOW) // Warning - warning triangle
    } else {
        (" ", Color::Reset) // Safe - no indicator needed
    }
}

/// Workflow-state marker shown alongside the countdown indicator.
///
/// Keeps approval workflow state visible without overloading countdown colors.
fn workflow_indicator(status: &str) -> (&'static str, Color) {
    match status {
        "pending" => ("!", palette::YELLOW),
        "approved" => ("", Color::Reset),
        _ => (" ", Color::Reset),
    }
}

/// Generate header cells for the entry table with sort indicators.
///
/// The currently sorted column gets a triangle indicator:
/// - `▲` for ascending sort (Name, Due)
/// - `▼` for descending sort (Size, Modified)
fn entry_table_header_cells(sort_mode: SortMode) -> Vec<Cell<'static>> {
    let indicator_asc = "";
    let indicator_desc = "";

    let filename_header = match sort_mode {
        SortMode::Name => format!("Filename{indicator_asc}"),
        _ => "Filename".to_string(),
    };

    let size_header = match sort_mode {
        SortMode::Size => format!("Size{indicator_desc}"),
        _ => "Size".to_string(),
    };

    // Due column shows sort indicator for both Expiration and Modified sorts
    // (Modified sorts by mtime, which determines the due date)
    let due_header = match sort_mode {
        SortMode::Expiration => format!("Due{indicator_asc}"),
        SortMode::Modified => format!("Due (mtime){indicator_desc}"),
        _ => "Due".to_string(),
    };

    vec![
        Cell::from(""), // No header for indicator column
        Cell::from(filename_header),
        Cell::from(size_header),
        Cell::from(due_header),
    ]
}

/// Format bytes as human-readable string (e.g., "1.2 KB", "523 MB").
fn format_bytes(bytes: u64) -> String {
    const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB", "PB"];
    const THRESHOLD: f64 = 1000.0;

    if bytes == 0 {
        return "0 B".to_string();
    }

    // Allow: Casting u64 to f64 for display purposes. Precision loss above 2^53 is acceptable
    // for human-readable sizes (that's ~9 PB, well beyond typical file sizes).
    #[allow(clippy::cast_precision_loss)]
    let bytes_f = bytes as f64;

    // Allow: Log-based calculation always produces non-negative results for positive inputs,
    // and floor() ensures the result fits in usize range [0..5] which is then clamped.
    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    let unit_idx = (bytes_f.log10() / THRESHOLD.log10()).floor() as usize;
    let unit_idx = unit_idx.min(UNITS.len() - 1);

    // Allow: unit_idx is clamped to [0..5], which always fits in i32.
    #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
    let value = bytes_f / THRESHOLD.powi(unit_idx as i32);

    if unit_idx == 0 {
        format!("{bytes} B")
    } else {
        format!("{value:.1} {}", UNITS[unit_idx])
    }
}

/// Render the audit log view.
///
/// Displays recent audit entries showing timestamp, user, action, and path.
/// The view is scrollable and shows the most recent entries first.
// Allow: This view coordinates data loading, row styling, scroll state, and
// scrollbar rendering in one place for readability, similar to the main table
// renderer pattern.
#[allow(clippy::too_many_lines)]
fn render_audit_log(app: &mut App, frame: &mut Frame, area: ratatui::layout::Rect) {
    // Update list length for navigation
    app.sidebar_len = app.audit_entries.len();

    // Clamp selected index to valid range
    let selected_idx = if app.audit_entries.is_empty() {
        0
    } else {
        app.sidebar_selected_index()
            .min(app.audit_entries.len() - 1)
    };

    // Handle empty state (or loading)
    if app.audit_entries.is_empty() {
        let msg = if app.loading.audit_entries {
            "Loading..."
        } else {
            "No audit entries found.\n\nPress 'q' or Esc to go back"
        };
        let empty_text = Paragraph::new(msg)
            .block(Block::default().borders(Borders::ALL).title("AUDIT LOG"))
            .style(Style::default());
        frame.render_widget(empty_text, area);
        return;
    }

    let entries = &app.audit_entries;

    // Build table rows with distance-based fade similar to the main panel.
    let gradient = fade_gradient();
    let max_dist = entries.len().saturating_sub(1).max(1);
    let rows: Vec<Row> = entries
        .iter()
        .enumerate()
        .map(|(idx, entry)| {
            let is_selected = idx == selected_idx;
            let distance = idx.abs_diff(selected_idx);
            let fade_pct = ((distance * 100) / max_dist).min(100);
            let should_fade = !is_selected;

            // Format timestamp as human-readable in local timezone
            let timestamp_str = format_timestamp(entry.timestamp);
            let timestamp_cell = if is_selected {
                Cell::from(timestamp_str)
            } else {
                let color = if should_fade {
                    gradient.text[fade_pct]
                } else {
                    Color::DarkGray
                };
                Cell::from(timestamp_str).style(Style::default().fg(color))
            };

            let user_cell = if is_selected {
                Cell::from(entry.user.as_str())
            } else {
                let color = if should_fade {
                    gradient.text[fade_pct]
                } else {
                    Color::Gray
                };
                Cell::from(entry.user.as_str()).style(Style::default().fg(color))
            };

            let action_style = if is_selected {
                Style::default().add_modifier(Modifier::BOLD)
            } else {
                let base = match entry.action.as_str() {
                    "remove" => &gradient.red,
                    "defer" => &gradient.green,
                    "ignore" => &gradient.yellow,
                    "approve" | "unapprove" | "unignore" | "undo" => &gradient.text,
                    _ => &gradient.gray,
                };
                let color = if should_fade { base[fade_pct] } else { base[0] };
                Style::default().fg(color).add_modifier(Modifier::BOLD)
            };
            let action_cell = Cell::from(entry.action.as_str()).style(action_style);

            let path_str = entry.target_path.as_deref().unwrap_or("<system-wide>");
            let path_cell = if is_selected {
                Cell::from(path_str)
            } else {
                let color = if should_fade {
                    gradient.text[fade_pct]
                } else {
                    Color::Gray
                };
                Cell::from(path_str).style(Style::default().fg(color))
            };

            // Highlight selected row
            let style = if is_selected {
                Style::default().add_modifier(Modifier::BOLD | Modifier::REVERSED)
            } else {
                Style::default()
            };

            Row::new(vec![timestamp_cell, user_cell, action_cell, path_cell]).style(style)
        })
        .collect();

    // Build table
    let widths = [
        Constraint::Percentage(20), // Timestamp
        Constraint::Percentage(15), // User
        Constraint::Percentage(15), // Action
        Constraint::Percentage(50), // Path
    ];

    let table = Table::new(rows, widths)
        .block(
            Block::default()
                .title(format!(
                    "AUDIT LOG (Most Recent First | {} entries | E export)",
                    entries.len()
                ))
                .borders(Borders::ALL),
        )
        .header(
            Row::new(vec![
                Cell::from("Timestamp"),
                Cell::from("User"),
                Cell::from("Action"),
                Cell::from("Path"),
            ])
            .style(Style::default().add_modifier(Modifier::BOLD))
            .bottom_margin(1),
        )
        .highlight_spacing(HighlightSpacing::Never);

    // Don't set selection on TableState - we handle row highlighting manually.
    // This prevents ratatui from auto-scrolling to the selected row,
    // allowing mouse scroll to control the viewport independently.

    // Only scroll to follow cursor when keyboard navigation requested it.
    if app.ensure_cursor_visible {
        let viewport_height = area.height.saturating_sub(4) as usize; // borders + header + margin
        let current_offset = app.audit_table_state.offset();
        if selected_idx < current_offset {
            // Selection is above viewport - scroll up to show it
            *app.audit_table_state.offset_mut() = selected_idx;
        } else if selected_idx >= current_offset + viewport_height && viewport_height > 0 {
            // Selection is below viewport - scroll down to show it
            *app.audit_table_state.offset_mut() = selected_idx.saturating_sub(viewport_height) + 1;
        }
        // Note: flag is cleared by entry panel render, which runs first in FileList view.
        // For AuditLog view, we clear it here.
        if app.view() == super::View::AuditLog {
            app.ensure_cursor_visible = false;
        }
    }

    // Store area for mouse hit-testing
    app.audit_table_area = area;

    // Render the table
    frame.render_stateful_widget(table, area, &mut app.audit_table_state);

    // Render the scrollbar (inside the border, on the right edge)
    // Only render if there's content to scroll
    let viewport_height = area.height.saturating_sub(4) as usize; // borders + header + margin
    if entries.len() > viewport_height {
        // content_length is the scrollable range: max_offset + 1 so position 0..=max_offset maps correctly
        let max_offset = entries.len().saturating_sub(viewport_height);
        app.audit_scrollbar_state =
            ScrollbarState::new(max_offset + 1).position(app.audit_table_state.offset());

        let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
            .begin_symbol(Some(""))
            .end_symbol(Some(""))
            .track_symbol(Some(""));

        frame.render_stateful_widget(
            scrollbar,
            area.inner(Margin {
                vertical: 1,
                horizontal: 0,
            }),
            &mut app.audit_scrollbar_state,
        );
    }
}

/// Format a Unix timestamp as a human-readable string in local timezone.
///
/// Formats as "YYYY-MM-DD HH:MM:SS" in local time.
fn format_timestamp(timestamp: i64) -> String {
    // Convert Unix timestamp to jiff::Timestamp
    let ts = jiff::Timestamp::from_second(timestamp).unwrap_or(jiff::Timestamp::UNIX_EPOCH);

    // Format in local timezone as "YYYY-MM-DD HH:MM:SS"
    ts.to_zoned(jiff::tz::TimeZone::system())
        .strftime("%Y-%m-%d %H:%M:%S")
        .to_string()
}

/// Render the help view with keybinding reference.
///
/// Displays all available keybindings organized by category (Navigation, Views,
/// Actions, Other). Any key press dismisses this view and returns to the directory list.
/// Render the help view with keybinding reference.
///
/// Displays all available keybindings organized by sections: File-Centric Workflow,
/// Navigation, Selection, Actions, Views, Sorting, and Other. Any key press dismisses
/// this view and returns to the file list.
const HELP_LEFT_TEXT: &str = r"File-Centric Workflow:
  The main panel shows files from the currently selected directory.
  The left sidebar shows tracked directories for filtering.
  Navigate the sidebar with j/k to change which directory's files are shown.

Navigation:
  j / ↓       Move selection down in focused panel
  k / ↑       Move selection up in focused panel
  g           Jump to top of focused panel
  G           Jump to bottom of focused panel
  Tab         Switch focus between sidebar and main panel
  h           Switch focus to sidebar (shows sidebar if hidden)
  l           Switch focus to main panel / enter directory
  B           Toggle sidebar visibility
  /           Search entries (Enter to confirm, Esc to cancel)
  n / N       Jump to next / previous search match

Selection (main panel only):
  Space       Toggle selection on current file and advance cursor
  v           Enter/exit visual mode (range select from anchor)
  a           Select all entries in current directory
  Esc         Exit visual mode / clear search / clear selection

Actions (on focused file or all selected files):
  d           Delete file(s) with confirmation
  r           Defer file(s) expiration (reset clock, prompt for days)
  i           Permanently ignore file(s)
  x           Approve file(s) for daemon removal
  I           Unignore file(s) (restore from ignored)
  u           Undo last reversible action
  e           Open in $VISUAL/$EDITOR (suspends TUI)
  o           Open with system viewer (fire-and-forget)";

const HELP_RIGHT_TEXT: &str = r"Root Management:
  A           Add a new tracked path
  X           Remove selected root (sidebar only)
  t           Set quota target for current root

Views:
  1           Main dashboard (file list)
  2           Audit log
  3 / ?       Show this help screen

Sorting:
  s           Cycle sort mode (Due → Size → Name → Modified)

Other:
  E           Export audit log (from Audit Log view)
  F           Execute approved removals for current root
  R           Refresh tracked paths (rescan filesystem)
  T           Reset countdown timer for current root
  Y           Dry run: check if approved entries can be removed
  q           Quit application (or return from audit log)
  Ctrl+C      Quit application";

fn styled_help_lines(text: &str) -> Vec<Line<'static>> {
    text.lines()
        .map(|line| {
            let is_header = line.ends_with(':') && !line.starts_with("  ");
            if is_header {
                Line::from(vec![Span::styled(
                    line.to_string(),
                    Style::default()
                        .fg(palette::CYAN)
                        .add_modifier(Modifier::BOLD),
                )])
            } else {
                Line::from(line.to_string())
            }
        })
        .collect()
}

fn help_legend_lines() -> Vec<Line<'static>> {
    vec![
        Line::from(""),
        Line::from(vec![Span::styled(
            "Legend:",
            Style::default()
                .fg(palette::CYAN)
                .add_modifier(Modifier::BOLD),
        )]),
        Line::from(vec![
            Span::styled("", Style::default().fg(palette::GREEN)),
            Span::raw(" Healthy (beyond warning window)"),
        ]),
        Line::from(vec![
            Span::styled("", Style::default().fg(palette::YELLOW)),
            Span::raw(" Warning (within warning window)"),
        ]),
        Line::from(vec![
            Span::styled("", Style::default().fg(palette::RED)),
            Span::raw(" Overdue (due now or in the past)"),
        ]),
        Line::from(vec![
            Span::styled("  ⚠ / ●", Style::default().fg(palette::YELLOW)),
            Span::raw(" Countdown glyphs (warning / overdue)"),
        ]),
        Line::from(vec![
            Span::styled("  !", Style::default().fg(palette::YELLOW)),
            Span::raw(" Pending workflow state"),
        ]),
        Line::from(vec![
            Span::styled("", Style::default().fg(Color::Reset)),
            Span::raw(" Approved workflow state"),
        ]),
    ]
}

fn render_help(_app: &App, frame: &mut Frame, area: ratatui::layout::Rect) {
    let block = Block::default()
        .title("KEYBIND REFERENCE")
        .borders(Borders::ALL)
        .style(Style::default());

    let left_lines = styled_help_lines(HELP_LEFT_TEXT);
    let mut right_lines = styled_help_lines(HELP_RIGHT_TEXT);
    right_lines.extend(help_legend_lines());
    right_lines.push(Line::from(""));
    right_lines.push(Line::from("Press any key to close this help screen"));

    frame.render_widget(block, area);
    let inner = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Fill(1),
            Constraint::Length(2),
            Constraint::Fill(1),
        ])
        .split(area.inner(Margin {
            horizontal: 1,
            vertical: 1,
        }));

    let left = Paragraph::new(left_lines).wrap(Wrap { trim: true });
    let right = Paragraph::new(right_lines).wrap(Wrap { trim: true });

    frame.render_widget(left, inner[0]);
    frame.render_widget(right, inner[2]);
}

/// Build a `Line` of hint spans that fits within `max_width`.
///
/// Left-side hints are rendered in priority order until space runs out.
/// Right-side (pinned) hints are always shown if they fit, separated from
/// the left group by flexible padding. Each hint is rendered as `[key] Label`
/// with a single space between hints.
fn build_hint_line(left: &[(&str, &str)], right: &[(&str, &str)], max_width: u16) -> Line<'static> {
    let style = Style::default().add_modifier(Modifier::REVERSED);
    let width = usize::from(max_width);

    // Format a single hint as "[key] Label"
    let fmt = |key: &str, label: &str| -> String { format!("[{key}] {label}") };

    // Measure the pinned right-side hints (with separating spaces)
    let right_parts: Vec<String> = right.iter().map(|(k, l)| fmt(k, l)).collect();
    let right_total: usize =
        right_parts.iter().map(String::len).sum::<usize>() + right_parts.len().saturating_sub(1); // spaces between

    // If even the pinned hints don't fit, just render what we can
    if right_total >= width {
        let joined = right_parts.join(" ");
        return Line::from(Span::styled(
            joined[..width.min(joined.len())].to_string(),
            style,
        ));
    }

    // Budget for left-side hints: total width minus right hints minus a gap
    let min_gap: usize = 2;
    let left_budget = width.saturating_sub(right_total).saturating_sub(min_gap);

    // Greedily add left-side hints in priority order
    let mut left_rendered: Vec<String> = Vec::new();
    let mut left_used: usize = 0;
    for (key, label) in left {
        let part = fmt(key, label);
        let needed = if left_rendered.is_empty() {
            part.len()
        } else {
            part.len() + 1 // space separator
        };
        if left_used + needed > left_budget {
            break;
        }
        left_used += needed;
        left_rendered.push(part);
    }

    let left_str = left_rendered.join(" ");
    let gap = width
        .saturating_sub(left_str.len())
        .saturating_sub(right_total);
    let right_str = right_parts.join(" ");

    Line::from(Span::styled(
        format!("{left_str}{:>gap$}{right_str}", "", gap = gap),
        style,
    ))
}

/// A keybind hint: `(key_label, description)`, rendered as `[key] description`.
type Hint = (&'static str, &'static str);

/// Return priority-ordered hint pairs for the current non-modal context.
///
/// Hints are ordered by importance: the most essential bindings come first
/// so they survive truncation on narrow terminals. The returned tuple is
/// `(left_hints, right_pinned_hints)`.
fn context_hints(app: &App) -> (Vec<Hint>, Vec<Hint>) {
    let right = vec![("?", "Help"), ("q", "Quit")];

    let left = match app.view() {
        View::FileList => {
            let selection_count = app.selected_entries().len();

            if app.search_query.is_some() {
                vec![
                    ("n", "Next match"),
                    ("N", "Prev match"),
                    ("/", "New search"),
                    ("Esc", "Clear search"),
                ]
            } else if app.is_visual_mode() {
                vec![
                    ("j/k", "Extend"),
                    ("d/r/i/x", "Act on selection"),
                    ("Esc", "Keep & exit"),
                    ("g/G", "Top/Bottom"),
                ]
            } else if selection_count > 0 {
                vec![
                    ("d", "Delete"),
                    ("r", "Defer"),
                    ("i", "Ignore"),
                    ("x", "Approve"),
                    ("Esc", "Clear"),
                ]
            } else {
                match app.focus_panel() {
                    FocusPanel::Sidebar => vec![
                        ("j/k", "Navigate"),
                        ("h/l", "Switch panel"),
                        ("d/r/i/x", "Actions"),
                        ("Space", "Select"),
                        ("g/G", "Top/Bottom"),
                        ("s", "Sort"),
                    ],
                    FocusPanel::MainPanel => vec![
                        ("j/k", "Navigate"),
                        ("h/l", "Switch panel"),
                        ("d", "Delete"),
                        ("r", "Defer"),
                        ("i", "Ignore"),
                        ("x", "Approve"),
                        ("I", "Unignore"),
                        ("u", "Undo"),
                        ("F", "Remove approved"),
                        ("T", "Reset timer"),
                        ("Y", "Dry run"),
                        ("Space", "Select"),
                        ("v", "Visual"),
                        ("s", "Sort"),
                        ("a", "All"),
                    ],
                }
            }
        }
        View::AuditLog => vec![
            ("j/k", "Navigate"),
            ("g/G", "Top/Bottom"),
            ("E", "Export"),
            ("Esc", "Back"),
        ],
        View::Help => {
            // Help view is simple enough to not need pinned right hints
            return (vec![("Any key", "Close")], vec![]);
        }
    };

    (left, right)
}

/// Render the footer with mode badge and context-sensitive keybinding hints.
fn render_footer(app: &App, frame: &mut Frame, area: ratatui::layout::Rect) {
    // Determine current mode for the badge
    let (mode_label, mode_style) = if app.search_input_active || app.search_query.is_some() {
        (
            " SEARCH ",
            Style::default()
                .fg(Color::Black)
                .bg(Color::Blue)
                .add_modifier(Modifier::BOLD),
        )
    } else if app.is_visual_mode() {
        (
            " VISUAL ",
            Style::default()
                .fg(Color::Black)
                .bg(palette::GREEN)
                .add_modifier(Modifier::BOLD),
        )
    } else {
        (
            " NORMAL ",
            Style::default().add_modifier(Modifier::BOLD | Modifier::REVERSED),
        )
    };

    // Split footer: fixed-width badge on left, hints fill the rest
    let badge_width: u16 = 9; // " VISUAL " + 1 space separator
    let chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([Constraint::Length(badge_width), Constraint::Min(0)])
        .split(area);

    // Render mode badge
    let badge = Paragraph::new(Span::styled(mode_label, mode_style));
    frame.render_widget(badge, chunks[0]);

    // Search input bar replaces hints when typing
    if app.search_input_active {
        let query = app.search_query.as_deref().unwrap_or("");
        let search_bar = Paragraph::new(Line::from(vec![Span::styled(
            format!("/{query}"),
            Style::default().add_modifier(Modifier::REVERSED),
        )]));
        frame.render_widget(search_bar, chunks[1]);
        return;
    }

    // Check if any modal is open (takes precedence over normal view hints).
    // Modal hints are short enough to never overflow, so they bypass truncation.
    let modal_open = app.pending_entry_delete().is_some()
        || app.pending_entry_deferral().is_some()
        || app.pending_entry_ignore().is_some()
        || app.pending_entry_approval().is_some()
        || app.pending_add_path().is_some()
        || app.pending_remove_path().is_some()
        || app.pending_audit_export().is_some()
        || app.pending_dry_run().is_some();

    if modal_open {
        let hints = if app.pending_dry_run().is_some() {
            "[Any key] Close"
        } else if app.pending_entry_deferral().is_some() {
            "[0-9] Enter days [Backspace] Delete [Enter] Confirm [Esc] Cancel"
        } else if app.pending_add_path().is_some() {
            "[Type path] (supports ~) [Backspace] Delete [Enter] Add [Esc] Cancel"
        } else if app.pending_audit_export().is_some() {
            "[Type path] [Tab] Format [Backspace] Delete [Enter] Export [Esc] Cancel"
        } else {
            "[y] Yes [n] No [Esc] Cancel"
        };
        let footer = Paragraph::new(hints).style(Style::default().add_modifier(Modifier::REVERSED));
        frame.render_widget(footer, chunks[1]);
        return;
    }

    let (left, right) = context_hints(app);
    let line = build_hint_line(&left, &right, chunks[1].width);
    let footer = Paragraph::new(line);
    frame.render_widget(footer, chunks[1]);
}

fn render_modal_shell(
    frame: &mut Frame,
    title: &str,
    border_color: Color,
    desired_width: u16,
    desired_height: u16,
) -> Rect {
    let area = frame.area();

    // Dim existing UI so modal content has clear visual priority.
    let buffer = frame.buffer_mut();
    for y in area.top()..area.bottom() {
        for x in area.left()..area.right() {
            let cell = &mut buffer[(x, y)];
            cell.set_style(cell.style().add_modifier(Modifier::DIM));
        }
    }

    let width = desired_width.clamp(44, area.width.saturating_sub(4));
    let height = desired_height.clamp(8, area.height.saturating_sub(2));
    let modal_area = Rect {
        x: area.left() + area.width.saturating_sub(width) / 2,
        y: area.top() + area.height.saturating_sub(height) / 2,
        width,
        height,
    };

    let block = Block::default()
        .title(title)
        .title_style(
            Style::default()
                .fg(border_color)
                .add_modifier(Modifier::BOLD),
        )
        .title_alignment(Alignment::Center)
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(Style::default().fg(border_color))
        .padding(Padding::symmetric(2, 1))
        .style(Style::default().bg(palette::MODAL_BG).fg(palette::MODAL_FG));
    let inner = block.inner(modal_area);

    // Clear modal cells so dim modifier and old symbols do not bleed through.
    frame.render_widget(Clear, modal_area);
    frame.render_widget(block, modal_area);

    inner
}

fn render_modal_body(frame: &mut Frame, area: Rect, lines: Vec<Line<'_>>) {
    let body = Paragraph::new(lines)
        .alignment(Alignment::Left)
        .wrap(Wrap { trim: true })
        .style(Style::default().fg(palette::MODAL_FG).bg(palette::MODAL_BG));
    frame.render_widget(body, area);
}

const MODAL_WIDTH_CONFIRM: u16 = 64;
const MODAL_WIDTH_INPUT: u16 = 74;
const MODAL_WIDTH_FORM: u16 = 82;

/// Render a confirmation modal for approval actions.
///
/// Displays a centered modal asking the user to confirm removal approval.
fn render_confirmation_modal(frame: &mut Frame, path: &str) {
    let inner = render_modal_shell(
        frame,
        "Approve Removal",
        palette::YELLOW,
        MODAL_WIDTH_CONFIRM,
        10,
    );
    render_modal_body(
        frame,
        inner,
        vec![
            Line::from(vec![Span::styled(
                "Action",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
            Line::from("Approve removal for:"),
            Line::from(path.to_string()),
            Line::from(""),
            Line::from(vec![Span::styled(
                "[y] confirm   [n] cancel",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
        ],
    );
}

/// Render a deferral input modal for entering days to defer.
///
/// Displays a centered modal prompting the user to enter the number of days
/// to defer expiration. Shows the current input and the default value.
/// The count parameter indicates how many files will be deferred.
fn render_deferral_modal(
    frame: &mut Frame,
    path: &str,
    input: &str,
    default_days: u32,
    count: usize,
) {
    let title = if count > 1 {
        format!("Defer Expiration ({count} files)")
    } else {
        "Defer Expiration".to_string()
    };
    let display_input = if input.is_empty() {
        format!("[{default_days}]")
    } else {
        input.to_string()
    };
    let path_display = if count > 1 {
        format!("{count} selected files")
    } else {
        path.to_string()
    };

    let inner = render_modal_shell(frame, &title, palette::CYAN, MODAL_WIDTH_INPUT, 12);
    render_modal_body(
        frame,
        inner,
        vec![
            Line::from(vec![Span::styled(
                "Target",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
            Line::from(path_display),
            Line::from(""),
            Line::from(vec![
                Span::styled("Days to defer: ", Style::default().fg(palette::MODAL_MUTED)),
                Span::raw(display_input),
            ]),
            Line::from(""),
            Line::from(vec![Span::styled(
                "[Enter] confirm   [Esc] cancel",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
        ],
    );
}

/// Render an entry deletion confirmation modal.
///
/// Displays a centered modal prompting the user to confirm deletion
/// of the selected entry (file or directory). The modal text varies
/// based on the removal method (trash vs permanent delete).
fn render_entry_delete_modal(frame: &mut Frame, path: &str, is_dir: bool, method: RemovalMethod) {
    let type_name = if is_dir { "directory" } else { "file" };
    let (title, action_verb, border_color) = match method {
        RemovalMethod::Trash => (
            if is_dir {
                "Move Directory to Trash"
            } else {
                "Move File to Trash"
            },
            "Move to trash",
            palette::YELLOW,
        ),
        RemovalMethod::PermanentDelete => (
            if is_dir {
                "Permanently Delete Directory"
            } else {
                "Permanently Delete File"
            },
            "Permanently delete",
            palette::RED,
        ),
    };
    let inner = render_modal_shell(frame, title, border_color, MODAL_WIDTH_CONFIRM, 10);
    render_modal_body(
        frame,
        inner,
        vec![
            Line::from(vec![Span::styled(
                "Action",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
            Line::from(format!("{action_verb} {type_name}:")),
            Line::from(path.to_string()),
            Line::from(""),
            Line::from(vec![Span::styled(
                "[y] confirm   [n] cancel",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
        ],
    );
}

/// Render a multi-entry deletion confirmation modal.
///
/// The modal text varies based on the removal method (trash vs permanent delete).
fn render_entry_delete_modal_multi(frame: &mut Frame, count: usize, method: RemovalMethod) {
    let (title, message, border_color) = match method {
        RemovalMethod::Trash => (
            format!("Move {count} Entries to Trash"),
            format!("Move {count} entries to trash?"),
            palette::YELLOW,
        ),
        RemovalMethod::PermanentDelete => (
            format!("Permanently Delete {count} Entries"),
            format!("Permanently delete {count} entries?"),
            palette::RED,
        ),
    };

    let inner = render_modal_shell(frame, &title, border_color, 58, 9);
    render_modal_body(
        frame,
        inner,
        vec![
            Line::from(message),
            Line::from(""),
            Line::from(vec![Span::styled(
                "[y] confirm   [n] cancel",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
        ],
    );
}

/// Render an ignore confirmation modal for permanently exempting a path.
///
/// Displays a centered modal prompting the user to confirm permanent exemption
/// of the selected directory from auto-removal.
fn render_ignore_modal(frame: &mut Frame, path: &str) {
    let inner = render_modal_shell(
        frame,
        "Ignore Path Permanently",
        palette::CYAN,
        MODAL_WIDTH_CONFIRM,
        10,
    );
    render_modal_body(
        frame,
        inner,
        vec![
            Line::from("Permanently ignore:"),
            Line::from(path.to_string()),
            Line::from(""),
            Line::from(vec![Span::styled(
                "[y] confirm   [n] cancel",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
        ],
    );
}

/// Render a multi-file ignore confirmation modal.
fn render_ignore_modal_multi(frame: &mut Frame, count: usize) {
    let title = format!("Ignore {count} Files Permanently");
    let inner = render_modal_shell(frame, &title, palette::CYAN, 58, 9);
    render_modal_body(
        frame,
        inner,
        vec![
            Line::from(format!("Permanently ignore {count} files?")),
            Line::from(""),
            Line::from(vec![Span::styled(
                "[y] confirm   [n] cancel",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
        ],
    );
}

/// Render a multi-file approval confirmation modal.
fn render_confirmation_modal_multi(frame: &mut Frame, count: usize) {
    let title = format!("Approve {count} Files for Removal");
    let inner = render_modal_shell(frame, &title, palette::YELLOW, 60, 9);
    render_modal_body(
        frame,
        inner,
        vec![
            Line::from(format!("Approve {count} files for removal?")),
            Line::from(""),
            Line::from(vec![Span::styled(
                "[y] confirm   [n] cancel",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
        ],
    );
}

/// Render add path text input modal.
///
/// Displays a centered modal prompting the user to enter a path to add to `tracked_paths`.
/// Supports tilde expansion (~).
fn render_add_path_modal(frame: &mut Frame, input: &str) {
    let display_input = if input.is_empty() { "_" } else { input };

    let inner = render_modal_shell(
        frame,
        "Add Tracked Path",
        palette::GREEN,
        MODAL_WIDTH_INPUT,
        11,
    );
    render_modal_body(
        frame,
        inner,
        vec![
            Line::from(vec![Span::styled(
                "Path",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
            Line::from(display_input.to_string()),
            Line::from(""),
            Line::from(vec![Span::styled(
                "Supports ~ expansion",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
            Line::from(vec![Span::styled(
                "[Enter] add   [Esc] cancel",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
        ],
    );
}

/// Render remove path confirmation modal.
///
/// Displays a centered modal prompting the user to confirm removal of a tracked path.
fn render_remove_path_modal(frame: &mut Frame, path: &str) {
    let inner = render_modal_shell(
        frame,
        "Remove Tracked Path",
        palette::RED,
        MODAL_WIDTH_CONFIRM,
        10,
    );
    render_modal_body(
        frame,
        inner,
        vec![
            Line::from("Remove tracked path:"),
            Line::from(path.to_string()),
            Line::from(""),
            Line::from(vec![Span::styled(
                "[y] confirm   [n] cancel",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
        ],
    );
}

/// Render audit export modal.
fn render_audit_export_modal(frame: &mut Frame, export: &PendingAuditExport) {
    let inner = render_modal_shell(frame, "Export Audit Log", palette::CYAN, 86, 12);
    let display_input = if export.path_input.is_empty() {
        "_".to_string()
    } else {
        export.path_input.clone()
    };

    render_modal_body(
        frame,
        inner,
        vec![
            Line::from(vec![Span::styled(
                "Output path",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
            Line::from(display_input),
            Line::from(""),
            Line::from(vec![
                Span::styled("Format: ", Style::default().fg(palette::MODAL_MUTED)),
                Span::styled(
                    export.format.label(),
                    Style::default()
                        .fg(palette::CYAN)
                        .add_modifier(Modifier::BOLD),
                ),
                Span::styled(
                    "  (Tab to switch)",
                    Style::default().fg(palette::MODAL_MUTED),
                ),
            ]),
            Line::from(""),
            Line::from(vec![Span::styled(
                "[Enter] export   [Esc] cancel",
                Style::default().fg(palette::MODAL_MUTED),
            )]),
        ],
    );
}

/// Render the quota target input modal.
///
/// Displays a centered modal with two fields: a numeric input for the size value
/// and a unit selector (MB/GB/TB). Tab switches focus between fields.
fn render_quota_target_modal(frame: &mut Frame, target: &PendingQuotaTarget) {
    let inner = render_modal_shell(
        frame,
        "Set Quota Target",
        palette::CYAN,
        MODAL_WIDTH_FORM,
        13,
    );

    // Format the current target for display
    let current_display = match target.current_target {
        // Allow: bytes is guaranteed non-negative by the guard
        #[allow(clippy::cast_sign_loss)]
        Some(bytes) if bytes >= 0 => crate::format_bytes(bytes as u64),
        Some(_) => "invalid".to_string(),
        None => "not set".to_string(),
    };

    // Format the size input with cursor indicator
    let size_display = if target.input.is_empty() {
        "_".to_string()
    } else {
        target.input.clone()
    };

    // Format the unit selector with highlight on selected
    let unit_display = format!(
        "{}  {}  {}",
        if target.unit == super::ByteUnit::MB {
            "[MB]"
        } else {
            " MB "
        },
        if target.unit == super::ByteUnit::GB {
            "[GB]"
        } else {
            " GB "
        },
        if target.unit == super::ByteUnit::TB {
            "[TB]"
        } else {
            " TB "
        },
    );

    // Highlight the focused field
    let (size_style, unit_style) = match target.focus {
        QuotaTargetFocus::Size => (
            Style::default()
                .fg(palette::CYAN)
                .add_modifier(Modifier::BOLD),
            Style::default().fg(palette::MODAL_FG),
        ),
        QuotaTargetFocus::Unit => (
            Style::default().fg(palette::MODAL_FG),
            Style::default()
                .fg(palette::CYAN)
                .add_modifier(Modifier::BOLD),
        ),
    };

    // Build the content with styled spans
    let path_display = target.root_path.display().to_string();
    let content = vec![
        Line::from(vec![
            Span::styled("Root: ", Style::default().fg(palette::MODAL_MUTED)),
            Span::raw(path_display),
        ]),
        Line::from(vec![
            Span::styled("Current: ", Style::default().fg(palette::MODAL_MUTED)),
            Span::raw(current_display),
        ]),
        Line::from(""),
        Line::from(vec![
            Span::styled("Size: ", Style::default().fg(palette::MODAL_MUTED)),
            Span::styled(size_display, size_style),
            Span::styled("    Unit: ", Style::default().fg(palette::MODAL_MUTED)),
            Span::styled(unit_display, unit_style),
        ]),
        Line::from(""),
        Line::from(vec![Span::styled(
            "[Tab] switch fields   [Enter] confirm",
            Style::default().fg(palette::MODAL_MUTED),
        )]),
        Line::from(vec![Span::styled(
            "Empty or 0 clears target   [Esc] cancel",
            Style::default().fg(palette::MODAL_MUTED),
        )]),
    ];

    render_modal_body(frame, inner, content);
}

/// Render the dry run results modal showing which approved entries would fail removal.
fn render_dry_run_modal(frame: &mut Frame, result: &DryRunResult) {
    let title = format!(
        "Dry Run: {} of {} removable",
        result.removable_count, result.total_count
    );

    let failure_count = result.failures.len();
    let modal_height = u16::try_from(failure_count)
        .unwrap_or(u16::MAX)
        .saturating_add(6)
        .min(24);

    let inner = render_modal_shell(frame, &title, palette::YELLOW, 78, modal_height);

    let mut lines = vec![
        Line::from(vec![Span::styled(
            format!(
                "{} entr{} would fail removal:",
                failure_count,
                if failure_count == 1 { "y" } else { "ies" }
            ),
            Style::default().fg(palette::RED),
        )]),
        Line::from(""),
    ];

    for failure in &result.failures {
        let path_display = failure.path.to_string_lossy();
        lines.push(Line::from(vec![
            Span::styled("  ", Style::default()),
            Span::styled(
                path_display.into_owned(),
                Style::default().fg(palette::MODAL_FG),
            ),
        ]));
        lines.push(Line::from(vec![Span::styled(
            format!("    {}", failure.reason),
            Style::default().fg(palette::MODAL_MUTED),
        )]));
    }

    lines.push(Line::from(""));
    lines.push(Line::from(vec![Span::styled(
        "[Any key] close",
        Style::default().fg(palette::MODAL_MUTED),
    )]));

    render_modal_body(frame, inner, lines);
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use super::*;

    // Helper to create a minimal Entry for testing (file)
    fn test_entry(path: &str, size_bytes: i64, mtime: Option<i64>) -> crate::db::Entry {
        let now = jiff::Timestamp::now().as_second();
        crate::db::Entry {
            id: 0,
            root_id: 1,
            path: PathBuf::from(path),
            parent_path: PathBuf::from("/"),
            is_dir: false,
            size_bytes,
            mtime,
            tracked_since: Some(now),
            countdown_start: Some(now),
            status: "tracked".to_string(),
            deferred_until: None,
            created_at: now,
            updated_at: now,
        }
    }

    // Helper to create a directory Entry for testing
    fn test_entry_dir(path: &str) -> crate::db::Entry {
        let now = jiff::Timestamp::now().as_second();
        crate::db::Entry {
            id: 0,
            root_id: 1,
            path: PathBuf::from(path),
            parent_path: PathBuf::from("/"),
            is_dir: true,
            size_bytes: 0,
            mtime: None,
            tracked_since: Some(now),
            countdown_start: None,
            status: "tracked".to_string(),
            deferred_until: None,
            created_at: now,
            updated_at: now,
        }
    }

    // Tests for sort_entry_rows

    #[test]
    fn sort_entries_by_expiration_most_urgent_first() {
        let mut rows = vec![
            (test_entry("/c.txt", 100, Some(1000)), 30), // 30 days remaining
            (test_entry("/a.txt", 200, Some(5000)), 5),  // 5 days remaining (most urgent)
            (test_entry("/b.txt", 150, Some(3000)), 15), // 15 days remaining
        ];

        sort_entry_rows(&mut rows, SortMode::Expiration);

        assert_eq!(
            rows[0].0.path,
            PathBuf::from("/a.txt"),
            "Most urgent (5 days) should be first"
        );
        assert_eq!(
            rows[1].0.path,
            PathBuf::from("/b.txt"),
            "Middle urgency (15 days) should be second"
        );
        assert_eq!(
            rows[2].0.path,
            PathBuf::from("/c.txt"),
            "Least urgent (30 days) should be last"
        );
    }

    #[test]
    fn sort_entries_directories_sort_by_expiration_like_files() {
        let mut rows = vec![
            (test_entry("/a.txt", 100, Some(1000)), 10),
            (test_entry_dir("/subdir"), 3),
            (test_entry("/b.txt", 100, Some(1000)), 20),
        ];

        sort_entry_rows(&mut rows, SortMode::Expiration);

        assert_eq!(
            rows[0].0.path,
            PathBuf::from("/subdir"),
            "Most urgent (dir with 3 days) first"
        );
        assert_eq!(
            rows[1].0.path,
            PathBuf::from("/a.txt"),
            "File with 10 days second"
        );
        assert_eq!(
            rows[2].0.path,
            PathBuf::from("/b.txt"),
            "Least urgent (20 days) last"
        );
    }

    #[test]
    fn sort_entries_by_size_largest_first() {
        let mut rows = vec![
            (test_entry("/a.txt", 100, Some(1000)), 10),
            (test_entry("/b.txt", 500, Some(1000)), 10),
            (test_entry("/c.txt", 250, Some(1000)), 10),
        ];

        sort_entry_rows(&mut rows, SortMode::Size);

        assert_eq!(
            rows[0].0.path,
            PathBuf::from("/b.txt"),
            "Largest (500) should be first"
        );
        assert_eq!(
            rows[1].0.path,
            PathBuf::from("/c.txt"),
            "Middle (250) should be second"
        );
        assert_eq!(
            rows[2].0.path,
            PathBuf::from("/a.txt"),
            "Smallest (100) should be last"
        );
    }

    #[test]
    fn sort_entries_by_name_alphabetical_dirs_first() {
        let mut rows = vec![
            (test_entry("/zebra.txt", 100, Some(1000)), 10),
            (test_entry_dir("/alpha_dir"), 15),
            (test_entry("/mango.txt", 100, Some(1000)), 10),
        ];

        sort_entry_rows(&mut rows, SortMode::Name);

        assert_eq!(
            rows[0].0.path,
            PathBuf::from("/alpha_dir"),
            "Directory should come first"
        );
        assert_eq!(
            rows[1].0.path,
            PathBuf::from("/mango.txt"),
            "Mango should be second"
        );
        assert_eq!(
            rows[2].0.path,
            PathBuf::from("/zebra.txt"),
            "Zebra should be last"
        );
    }

    #[test]
    fn sort_entries_empty_list_does_not_panic() {
        let mut rows: Vec<(crate::db::Entry, i64)> = vec![];

        // Should not panic for any sort mode
        sort_entry_rows(&mut rows, SortMode::Expiration);
        sort_entry_rows(&mut rows, SortMode::Size);
        sort_entry_rows(&mut rows, SortMode::Name);
        sort_entry_rows(&mut rows, SortMode::Modified);

        assert_eq!(rows.len(), 0, "Empty list should remain empty");
    }

    #[test]
    fn sort_entries_by_modified_most_recent_first() {
        let mut rows = vec![
            (test_entry("/a.txt", 100, Some(1000)), 10), // Oldest
            (test_entry("/b.txt", 100, Some(5000)), 10), // Most recent
            (test_entry("/c.txt", 100, Some(3000)), 10), // Middle
        ];

        sort_entry_rows(&mut rows, SortMode::Modified);

        assert_eq!(
            rows[0].0.path,
            PathBuf::from("/b.txt"),
            "Most recent (5000) should be first"
        );
        assert_eq!(
            rows[1].0.path,
            PathBuf::from("/c.txt"),
            "Middle (3000) should be second"
        );
        assert_eq!(
            rows[2].0.path,
            PathBuf::from("/a.txt"),
            "Oldest (1000) should be last"
        );
    }

    // Tests for format_timestamp

    #[test]
    fn format_timestamp_formats_correctly() {
        // 2024-01-15 14:32:45 UTC
        let ts = 1_705_329_165;
        let result = format_timestamp(ts);

        // Verify it contains expected date components
        // (exact time depends on local timezone, so we check for date)
        assert!(
            result.contains("2024"),
            "Should contain year 2024, got: {result}"
        );
        assert!(
            result.contains("01") || result.contains("1-"),
            "Should contain month 01, got: {result}"
        );
        assert!(
            result.contains("15") || result.contains("14") || result.contains("16"),
            "Should contain day 14-16 (timezone variance), got: {result}"
        );
    }

    #[test]
    fn format_timestamp_handles_unix_epoch() {
        let result = format_timestamp(0);
        // Unix epoch is 1970-01-01 00:00:00 UTC
        // Local timezone may shift this slightly, but should contain 1969 or 1970
        assert!(
            result.contains("1970") || result.contains("1969"),
            "Should contain year 1970 or 1969, got: {result}"
        );
    }

    #[test]
    fn format_timestamp_handles_invalid_timestamp_gracefully() {
        // Timestamp that would fail conversion - should fall back to UNIX_EPOCH
        let result = format_timestamp(i64::MIN);
        // Should not panic and should return something reasonable
        assert!(
            !result.is_empty(),
            "Should return non-empty string even for invalid timestamp"
        );
    }

    #[test]
    fn format_timestamp_includes_time_components() {
        // 2024-06-15 09:30:45 UTC
        let ts = 1_718_443_845;
        let result = format_timestamp(ts);

        // Verify format includes time separator ':'
        assert!(
            result.contains(':'),
            "Should include time separator ':', got: {result}"
        );

        // Verify format roughly matches "YYYY-MM-DD HH:MM:SS"
        // We check for two colons (HH:MM:SS)
        assert_eq!(
            result.matches(':').count(),
            2,
            "Should have exactly 2 colons for HH:MM:SS format, got: {result}"
        );
    }

    // Tests for expiration_indicator_entry

    fn make_test_entry(status: &str, deferred_until: Option<i64>) -> crate::db::Entry {
        crate::db::Entry {
            id: 1,
            root_id: 1,
            path: PathBuf::from("/test/file.txt"),
            parent_path: PathBuf::from("/test"),
            is_dir: false,
            size_bytes: 100,
            mtime: Some(0),
            tracked_since: None,
            countdown_start: Some(0),
            status: status.to_string(),
            deferred_until,
            created_at: 0,
            updated_at: 0,
        }
    }

    #[test]
    fn expiration_indicator_entry_overdue_is_red_circle() {
        let entry = make_test_entry("tracked", None);
        let (symbol, color) = expiration_indicator_entry("tracked", 0, 14, &entry);
        assert_eq!(symbol, "", "Overdue (0 days) should show filled circle");
        assert_eq!(color, palette::RED, "Overdue should be red");

        let (symbol, color) = expiration_indicator_entry("tracked", -5, 14, &entry);
        assert_eq!(symbol, "", "Negative days should show filled circle");
        assert_eq!(color, palette::RED, "Overdue should be red");
    }

    #[test]
    fn expiration_indicator_entry_within_warning_is_yellow_triangle() {
        let entry = make_test_entry("tracked", None);
        let (symbol, color) = expiration_indicator_entry("tracked", 1, 14, &entry);
        assert_eq!(symbol, "", "1 day remaining should show warning triangle");
        assert_eq!(color, palette::YELLOW, "Warning should be yellow");

        let (symbol, color) = expiration_indicator_entry("tracked", 14, 14, &entry);
        assert_eq!(symbol, "", "At warning threshold should show warning");
        assert_eq!(color, palette::YELLOW, "Warning should be yellow");
    }

    #[test]
    fn expiration_indicator_entry_safe_is_empty() {
        let entry = make_test_entry("tracked", None);
        let (symbol, color) = expiration_indicator_entry("tracked", 15, 14, &entry);
        assert_eq!(symbol, " ", "Safe entries should show no indicator");
        assert_eq!(color, Color::Reset, "Safe should use reset color");

        let (symbol, _) = expiration_indicator_entry("tracked", 90, 14, &entry);
        assert_eq!(symbol, " ", "Very safe entries should show no indicator");
    }

    #[test]
    fn expiration_indicator_entry_ignored_is_gray_dash() {
        let entry = make_test_entry("ignored", None);
        let (symbol, color) = expiration_indicator_entry("ignored", -30, 14, &entry);
        assert_eq!(symbol, "", "Ignored entries should show dash");
        assert_eq!(color, Color::DarkGray, "Ignored should be gray");

        // Even if "overdue", ignored entries show dash
        let (symbol, _) = expiration_indicator_entry("ignored", 0, 14, &entry);
        assert_eq!(
            symbol, "",
            "Ignored overdue entries should still show dash"
        );
    }

    #[test]
    fn expiration_indicator_entry_deferred_uses_deferred_until() {
        // Deferred entry with plenty of time on deferral
        let future = jiff::Timestamp::now().as_second() + (30 * 86400); // 30 days from now
        let entry = make_test_entry("deferred", Some(future));
        let (symbol, _) = expiration_indicator_entry("deferred", -100, 14, &entry);
        assert_eq!(
            symbol, " ",
            "Deferred with time remaining should show no indicator"
        );

        // Deferred entry with deferral expiring soon
        let soon = jiff::Timestamp::now().as_second() + (5 * 86400); // 5 days from now
        let entry = make_test_entry("deferred", Some(soon));
        let (symbol, color) = expiration_indicator_entry("deferred", -100, 14, &entry);
        assert_eq!(symbol, "", "Deferred expiring soon should show warning");
        assert_eq!(color, palette::YELLOW);
    }

    #[test]
    fn workflow_indicator_pending_and_approved_have_markers() {
        let (pending_symbol, pending_color) = workflow_indicator("pending");
        assert_eq!(pending_symbol, "!");
        assert_eq!(pending_color, palette::YELLOW);

        let (approved_symbol, approved_color) = workflow_indicator("approved");
        assert_eq!(approved_symbol, "");
        assert_eq!(approved_color, Color::Reset);
    }

    #[test]
    fn workflow_indicator_other_states_are_blank() {
        let (tracked_symbol, tracked_color) = workflow_indicator("tracked");
        assert_eq!(tracked_symbol, " ");
        assert_eq!(tracked_color, Color::Reset);

        let (deferred_symbol, deferred_color) = workflow_indicator("deferred");
        assert_eq!(deferred_symbol, " ");
        assert_eq!(deferred_color, Color::Reset);
    }
}