whogitit 1.0.0

Track AI-generated code at line-level granularity
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
use std::collections::HashSet;
use std::env;
use std::path::Path;

use anyhow::{Context, Result};
use git2::{Delta, DiffFindOptions, DiffOptions, Repository};
use serde::{Deserialize, Serialize};

use crate::capture::pending::{PendingBuffer, PendingStore, PromptRecord};
use crate::capture::threeway::ThreeWayAnalyzer;
use crate::core::attribution::{AIAttribution, PromptInfo, SessionMetadata};
use crate::privacy::{Redactor, RetentionConfig, WhogititConfig};
use crate::retention::apply_retention_policy;
use crate::storage::audit::AuditLog;
use crate::storage::notes::NotesStore;

/// Environment variable for session ID
const ENV_SESSION_ID: &str = "WHOGITIT_SESSION_ID";
/// Environment variable for model ID
const ENV_MODEL_ID: &str = "WHOGITIT_MODEL_ID";
/// Default model if not specified
const DEFAULT_MODEL: &str = "claude-opus-4-5-20251101";

/// Context from Claude Code transcript
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct HookContext {
    /// Whether the edit was made in plan mode
    #[serde(default)]
    pub plan_mode: bool,
    /// Whether this is from a subagent
    #[serde(default)]
    pub is_subagent: bool,
    /// Agent nesting depth (0=main, 1+=subagent)
    #[serde(default)]
    pub agent_depth: u8,
    /// Subagent ID if applicable
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subagent_id: Option<String>,
}

/// Input from Claude Code hook for file changes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HookInput {
    /// The tool being called (Edit, Write)
    pub tool: String,
    /// File path being modified
    pub file_path: String,
    /// The current user prompt/context
    pub prompt: String,
    /// Old file content (None for new files)
    pub old_content: Option<String>,
    /// Whether old_content was provided (distinguish empty from missing)
    #[serde(default)]
    pub old_content_present: bool,
    /// New file content
    pub new_content: String,
    /// Context from transcript (plan mode, subagent, etc.)
    #[serde(default)]
    pub context: Option<HookContext>,
}

/// Claude Code hook handler
pub struct CaptureHook {
    /// Repository root path
    repo_root: std::path::PathBuf,
    /// Privacy redactor
    redactor: Redactor,
    /// Whether audit logging is enabled
    audit_enabled: bool,
    /// Similarity threshold for AI-modified detection
    similarity_threshold: f64,
    /// Maximum pending buffer age in hours
    max_pending_age_hours: i64,
    /// Retention configuration
    retention_config: RetentionConfig,
}

impl CaptureHook {
    /// Create a new capture hook for a repository
    pub fn new(repo_path: &Path) -> Result<Self> {
        let repo_root = repo_path.to_path_buf();

        // Load config and build redactor
        let config = match WhogititConfig::load(&repo_root) {
            Ok(config) => config,
            Err(err) => {
                eprintln!(
                    "whogitit: Warning - failed to load config, using defaults: {}",
                    err
                );
                WhogititConfig::default()
            }
        };
        let redactor = config.privacy.build_redactor();
        let audit_enabled = config.privacy.audit_log;
        let similarity_threshold = config.analysis.similarity_threshold;
        let max_pending_age_hours = config.analysis.max_pending_age_hours as i64;
        let retention_config = config.retention.unwrap_or_default();

        Ok(Self {
            repo_root,
            redactor,
            audit_enabled,
            similarity_threshold,
            max_pending_age_hours,
            retention_config,
        })
    }

    /// Get or create session ID
    fn get_session_id() -> String {
        env::var(ENV_SESSION_ID).unwrap_or_else(|_| uuid::Uuid::new_v4().to_string())
    }

    /// Get model ID from environment
    fn get_model_id() -> String {
        env::var(ENV_MODEL_ID).unwrap_or_else(|_| DEFAULT_MODEL.to_string())
    }

    /// Handle a file change from Claude Code
    pub fn on_file_change(&self, input: HookInput) -> Result<()> {
        let store = PendingStore::new(&self.repo_root);

        // Load or create pending buffer
        let mut buffer = match store.load_with_max_age(self.max_pending_age_hours)? {
            Some(b) => {
                // Check if we should start a new session
                // (different session ID in env means new session)
                let current_session = Self::get_session_id();
                if b.session.session_id != current_session && env::var(ENV_SESSION_ID).is_ok() {
                    // New session ID explicitly set, start fresh
                    // But first, warn about uncommitted changes
                    if b.has_changes() {
                        eprintln!(
                            "whogitit: Warning - discarding {} uncommitted edits from previous session",
                            b.total_edits()
                        );
                    }
                    let mut buffer = PendingBuffer::new(&current_session, &Self::get_model_id());
                    buffer.audit_logging_enabled = self.audit_enabled;
                    buffer
                } else {
                    b
                }
            }
            None => {
                let mut buffer = PendingBuffer::new(&Self::get_session_id(), &Self::get_model_id());
                buffer.audit_logging_enabled = self.audit_enabled;
                buffer
            }
        };

        // Make path relative to repo root
        let relative_path = self.make_relative_path(&input.file_path)?;

        // Validate input
        if relative_path.is_empty() {
            anyhow::bail!("Empty file path");
        }

        let rel_path = std::path::Path::new(&relative_path);

        // Reject absolute paths (including Windows prefixes)
        if rel_path.is_absolute()
            || rel_path
                .components()
                .any(|c| matches!(c, std::path::Component::Prefix(_)))
        {
            anyhow::bail!(
                "Path '{}' is outside repository root '{}'. Use a repository-relative path.",
                relative_path,
                self.repo_root.display()
            );
        }

        // Check for path traversal attempts
        if rel_path
            .components()
            .any(|c| matches!(c, std::path::Component::ParentDir))
        {
            anyhow::bail!(
                "Path traversal detected in file path: '{}'. Paths containing '..' are not allowed.",
                relative_path
            );
        }

        if input.new_content.is_empty() && input.tool != "Delete" {
            eprintln!("whogitit: Warning - empty new_content for non-delete operation");
        }

        // Determine old content: use provided value, or fall back to git HEAD
        let old_content = if input.old_content_present {
            Some(input.old_content.unwrap_or_default())
        } else if let Some(content) = input.old_content.clone() {
            Some(content)
        } else {
            // Try to get content from git HEAD for existing files
            self.get_content_from_git_head(&relative_path)
        };

        // Build edit context from hook input
        let edit_context =
            input
                .context
                .as_ref()
                .map(|ctx| crate::capture::snapshot::EditContext {
                    plan_mode: ctx.plan_mode,
                    subagent_id: ctx.subagent_id.clone(),
                    agent_depth: ctx.agent_depth,
                    plan_step: None,
                });

        // Record the edit with full content snapshots
        buffer.record_edit_with_context(
            &relative_path,
            old_content.as_deref(),
            &input.new_content,
            &input.tool,
            &input.prompt,
            Some(&self.redactor),
            edit_context,
        );

        // Log redaction audit events (if enabled)
        if self.audit_enabled {
            if let Some(prompt) = buffer.session.prompts.last() {
                if !prompt.redaction_events.is_empty() {
                    let audit_log = AuditLog::new(&self.repo_root);
                    let mut counts: std::collections::HashMap<String, u32> =
                        std::collections::HashMap::new();
                    for event in &prompt.redaction_events {
                        *counts.entry(event.pattern_name.clone()).or_insert(0) += 1;
                    }
                    for (pattern, count) in counts {
                        if let Err(e) = audit_log.log_redaction(&pattern, count) {
                            eprintln!("whogitit: Warning - failed to log redaction: {}", e);
                        }
                    }
                }
            }
        }

        // Save buffer with atomic write
        store.save(&buffer)?;

        Ok(())
    }

    /// Get file content from git HEAD (the last committed version)
    ///
    /// Returns None for new files or if git operations fail.
    /// Logs warnings for unexpected failures to aid debugging.
    fn get_content_from_git_head(&self, path: &str) -> Option<String> {
        let repo = match Repository::open(&self.repo_root) {
            Ok(r) => r,
            Err(e) => {
                eprintln!(
                    "whogitit: Warning - failed to open repository at '{}': {}",
                    self.repo_root.display(),
                    e
                );
                return None;
            }
        };

        let head = match repo.head() {
            Ok(h) => h,
            Err(e) => {
                // HEAD not existing is normal for new repos with no commits
                if e.code() != git2::ErrorCode::UnbornBranch {
                    eprintln!("whogitit: Warning - failed to get HEAD: {}", e);
                }
                return None;
            }
        };

        let commit = match head.peel_to_commit() {
            Ok(c) => c,
            Err(e) => {
                eprintln!("whogitit: Warning - failed to peel HEAD to commit: {}", e);
                return None;
            }
        };

        let tree = match commit.tree() {
            Ok(t) => t,
            Err(e) => {
                eprintln!("whogitit: Warning - failed to get commit tree: {}", e);
                return None;
            }
        };

        // File not existing in HEAD is normal for new files - don't warn
        let entry = match tree.get_path(std::path::Path::new(path)) {
            Ok(e) => e,
            Err(_) => return None, // New file, not in HEAD
        };

        let blob = match repo.find_blob(entry.id()) {
            Ok(b) => b,
            Err(e) => {
                eprintln!(
                    "whogitit: Warning - failed to read blob for '{}': {}",
                    path, e
                );
                return None;
            }
        };

        // Non-UTF8 content is valid for binary files - don't treat as error
        match std::str::from_utf8(blob.content()) {
            Ok(content) => Some(content.to_string()),
            Err(_) => None, // Binary file
        }
    }

    /// Handle post-commit: perform three-way analysis, attach notes, and clean up
    pub fn on_post_commit(&self) -> Result<Option<AIAttribution>> {
        let store = PendingStore::new(&self.repo_root);

        // Load pending buffer
        let mut buffer = match store.load()? {
            Some(b) if b.has_changes() => b,
            _ => return Ok(None),
        };

        // Open repo and get HEAD commit
        let repo = Repository::open(&self.repo_root).context("Failed to open repository")?;
        let head = repo
            .head()
            .context("Failed to get HEAD")?
            .peel_to_commit()
            .context("Failed to get HEAD commit")?;

        let tree = head.tree()?;

        // Build rename map (old -> new) to preserve attribution across moves
        let rename_map = build_rename_map(&repo, &head)?;
        let changed_paths = build_changed_paths(&repo, &head)?;

        // Preserve all prompt records before we split processed vs remaining histories.
        let all_prompts = buffer.session.prompts.clone();

        let mut file_results = Vec::new();
        let mut remaining_histories = std::collections::HashMap::new();
        let mut processed_prompt_indices = HashSet::new();
        let mut remaining_prompt_indices = HashSet::new();
        let mut used_plan_mode = false;
        let mut subagent_count = 0u32;

        for (path, history) in buffer.file_histories.drain() {
            let Some(committed_path) = resolve_committed_path(&path, &changed_paths, &rename_map)
            else {
                for edit in &history.edits {
                    remaining_prompt_indices.insert(edit.prompt_index);
                }
                remaining_histories.insert(path, history);
                continue;
            };

            // Get the committed content for this file
            let committed_content = match tree.get_path(std::path::Path::new(&committed_path)) {
                Ok(entry) => {
                    let blob = repo.find_blob(entry.id())?;
                    String::from_utf8_lossy(blob.content()).to_string()
                }
                Err(_) => {
                    // File was part of commit metadata but does not exist in final tree
                    // (for example, deleted file). Consume it from pending state.
                    continue;
                }
            };

            // Perform three-way analysis
            let mut result = ThreeWayAnalyzer::analyze_with_diff_with_threshold(
                &history,
                &committed_content,
                self.similarity_threshold,
            );
            if committed_path != path {
                result.path = committed_path;
            }
            file_results.push(result);

            for edit in &history.edits {
                processed_prompt_indices.insert(edit.prompt_index);
                if edit.context.plan_mode {
                    used_plan_mode = true;
                }
                if edit.context.agent_depth > 0 {
                    subagent_count += 1;
                }
            }
        }

        // Nothing attributable for this commit; only update pending state.
        if file_results.is_empty() {
            if remaining_histories.is_empty() {
                store.delete()?;
            } else {
                buffer.file_histories = remaining_histories;
                buffer.session.prompts =
                    filter_prompt_records(&all_prompts, &remaining_prompt_indices);
                buffer.session.prompt_count = buffer.session.prompts.len() as u32;
                buffer.prompt_counter = next_prompt_index(&buffer.session.prompts);
                buffer.total_redactions = buffer
                    .session
                    .prompts
                    .iter()
                    .map(|p| p.redaction_events.len() as u32)
                    .sum();
                store.save(&buffer)?;
            }
            return Ok(None);
        }

        let attribution_prompts = filter_prompt_records(&all_prompts, &processed_prompt_indices);

        // Create attribution with full analysis
        let attribution = AIAttribution {
            version: 3,
            session: SessionMetadata {
                session_id: buffer.session.session_id.clone(),
                model: buffer.session.model.clone(),
                started_at: buffer.session.started_at.clone(),
                prompt_count: attribution_prompts.len() as u32,
                used_plan_mode,
                subagent_count,
            },
            prompts: attribution_prompts
                .iter()
                .map(|p| PromptInfo {
                    index: p.index,
                    text: p.text.clone(),
                    timestamp: p.timestamp.clone(),
                    affected_files: p.affected_files.clone(),
                })
                .collect(),
            files: file_results,
        };

        // Store as git note
        let notes_store = NotesStore::new(&repo)?;
        notes_store.store_attribution(head.id(), &attribution)?;

        if self.retention_config.auto_purge {
            if let Err(e) = apply_retention_policy(
                &repo,
                &self.retention_config,
                true,
                "Auto purge (post-commit)",
                self.audit_enabled,
            ) {
                eprintln!("whogitit: Warning - auto purge failed: {}", e);
            }
        }

        // Persist any remaining pending edits only after attribution note is safely stored.
        if remaining_histories.is_empty() {
            store.delete()?;
        } else {
            buffer.file_histories = remaining_histories;
            buffer.session.prompts = filter_prompt_records(&all_prompts, &remaining_prompt_indices);
            buffer.session.prompt_count = buffer.session.prompts.len() as u32;
            buffer.prompt_counter = next_prompt_index(&buffer.session.prompts);
            buffer.total_redactions = buffer
                .session
                .prompts
                .iter()
                .map(|p| p.redaction_events.len() as u32)
                .sum();
            store.save(&buffer)?;
        }

        // Log summary
        let total_ai = attribution
            .files
            .iter()
            .map(|f| f.summary.ai_lines + f.summary.ai_modified_lines)
            .sum::<usize>();
        let total_human = attribution
            .files
            .iter()
            .map(|f| f.summary.human_lines)
            .sum::<usize>();

        eprintln!(
            "whogitit: Attached attribution - {} AI lines, {} human lines across {} files",
            total_ai,
            total_human,
            attribution.files.len()
        );

        Ok(Some(attribution))
    }

    /// Make a path relative to the repo root
    fn make_relative_path(&self, path: &str) -> Result<String> {
        let input_path = Path::new(path);
        if !input_path.is_absolute() {
            return Ok(path.to_string());
        }

        // Fast path: exact prefix match against the repo root.
        if let Ok(relative) = input_path.strip_prefix(&self.repo_root) {
            return Ok(relative.to_string_lossy().to_string());
        }

        // Handle aliased absolute paths (e.g. /var vs /private/var on macOS)
        // by canonicalizing both paths before prefix comparison.
        let canonical_repo =
            canonicalize_for_prefix(&self.repo_root).unwrap_or_else(|| self.repo_root.clone());
        if let Some(canonical_input) = canonicalize_for_prefix(input_path) {
            if let Ok(relative) = canonical_input.strip_prefix(&canonical_repo) {
                return Ok(relative.to_string_lossy().to_string());
            }
        }

        anyhow::bail!(
            "Absolute path '{}' could not be mapped under repository root '{}'.",
            path,
            self.repo_root.display()
        )
    }

    /// Get current pending status
    pub fn status(&self) -> Result<PendingStatus> {
        let store = PendingStore::new(&self.repo_root);

        // Use quiet load to avoid spurious warnings during status check
        match store.load_quiet()? {
            Some(buffer) => {
                let session_id = buffer.session.session_id.clone();
                let file_count = buffer.file_count();
                let line_count = buffer.total_lines();
                let edit_count = buffer.total_edits();
                let prompt_count = buffer.session.prompt_count;
                let has_pending = buffer.has_changes();
                let is_stale = buffer.is_stale_hours(self.max_pending_age_hours);
                let age = buffer.age_string();
                Ok(PendingStatus {
                    has_pending,
                    session_id: Some(session_id),
                    file_count,
                    line_count,
                    edit_count,
                    prompt_count,
                    is_stale,
                    age,
                    max_pending_age_hours: self.max_pending_age_hours,
                })
            }
            None => Ok(PendingStatus {
                has_pending: false,
                session_id: None,
                file_count: 0,
                line_count: 0,
                edit_count: 0,
                prompt_count: 0,
                is_stale: false,
                age: String::new(),
                max_pending_age_hours: self.max_pending_age_hours,
            }),
        }
    }

    /// Clear pending changes without committing
    pub fn clear_pending(&self) -> Result<()> {
        let store = PendingStore::new(&self.repo_root);
        store.delete()
    }
}

/// Canonicalize a path for prefix comparison.
///
/// If the full path doesn't exist yet, this resolves the deepest existing ancestor
/// and re-appends the missing suffix so new files can still be matched reliably.
fn canonicalize_for_prefix(path: &Path) -> Option<std::path::PathBuf> {
    if let Ok(canonical) = std::fs::canonicalize(path) {
        return Some(canonical);
    }

    let mut current = path;
    let mut missing_components = Vec::new();

    while !current.exists() {
        let file_name = current.file_name()?;
        missing_components.push(file_name.to_os_string());
        current = current.parent()?;
    }

    let mut canonical_base = std::fs::canonicalize(current).ok()?;
    for component in missing_components.iter().rev() {
        canonical_base.push(component);
    }

    Some(canonical_base)
}

fn build_rename_map(
    repo: &Repository,
    head: &git2::Commit,
) -> Result<std::collections::HashMap<String, String>> {
    let mut map = std::collections::HashMap::new();

    let new_tree = head.tree()?;

    for parent_idx in 0..head.parent_count() {
        let parent = match head.parent(parent_idx) {
            Ok(p) => p,
            Err(_) => continue,
        };

        let old_tree = parent.tree()?;

        let mut opts = DiffOptions::new();
        let mut diff = repo.diff_tree_to_tree(Some(&old_tree), Some(&new_tree), Some(&mut opts))?;

        let mut find_opts = DiffFindOptions::new();
        find_opts.renames_from_rewrites(true);
        diff.find_similar(Some(&mut find_opts))?;

        for delta in diff.deltas() {
            if delta.status() == Delta::Renamed {
                let old_path = delta
                    .old_file()
                    .path()
                    .map(|p| p.to_string_lossy().to_string());
                let new_path = delta
                    .new_file()
                    .path()
                    .map(|p| p.to_string_lossy().to_string());
                if let (Some(old_path), Some(new_path)) = (old_path, new_path) {
                    map.entry(old_path).or_insert(new_path);
                }
            }
        }
    }

    Ok(map)
}

fn build_changed_paths(repo: &Repository, head: &git2::Commit) -> Result<HashSet<String>> {
    let mut changed = HashSet::new();
    let new_tree = head.tree()?;

    if head.parent_count() == 0 {
        collect_changed_paths(repo, None, &new_tree, &mut changed)?;
        return Ok(changed);
    }

    for parent_idx in 0..head.parent_count() {
        let parent = match head.parent(parent_idx) {
            Ok(p) => p,
            Err(_) => continue,
        };
        let old_tree = parent.tree()?;
        collect_changed_paths(repo, Some(&old_tree), &new_tree, &mut changed)?;
    }

    Ok(changed)
}

fn collect_changed_paths(
    repo: &Repository,
    old_tree: Option<&git2::Tree<'_>>,
    new_tree: &git2::Tree<'_>,
    changed: &mut HashSet<String>,
) -> Result<()> {
    let mut opts = DiffOptions::new();
    let diff = repo.diff_tree_to_tree(old_tree, Some(new_tree), Some(&mut opts))?;

    for delta in diff.deltas() {
        if let Some(path) = delta.old_file().path() {
            changed.insert(path.to_string_lossy().to_string());
        }
        if let Some(path) = delta.new_file().path() {
            changed.insert(path.to_string_lossy().to_string());
        }
    }

    Ok(())
}

fn resolve_committed_path(
    path: &str,
    changed_paths: &HashSet<String>,
    rename_map: &std::collections::HashMap<String, String>,
) -> Option<String> {
    if let Some(new_path) = rename_map.get(path) {
        if changed_paths.contains(path) || changed_paths.contains(new_path) {
            return Some(new_path.clone());
        }
    }

    if changed_paths.contains(path) {
        return Some(path.to_string());
    }

    None
}

fn filter_prompt_records(
    prompts: &[PromptRecord],
    prompt_indices: &HashSet<u32>,
) -> Vec<PromptRecord> {
    prompts
        .iter()
        .filter(|p| prompt_indices.contains(&p.index))
        .cloned()
        .collect()
}

fn next_prompt_index(prompts: &[PromptRecord]) -> u32 {
    prompts
        .iter()
        .map(|p| p.index)
        .max()
        .map(|idx| idx.saturating_add(1))
        .unwrap_or(0)
}

/// Status of pending changes
#[derive(Debug)]
pub struct PendingStatus {
    pub has_pending: bool,
    pub session_id: Option<String>,
    pub file_count: usize,
    pub line_count: u32,
    pub edit_count: usize,
    pub prompt_count: u32,
    /// Whether the pending buffer is stale (older than configured hours)
    pub is_stale: bool,
    /// Human-readable age of the pending buffer
    pub age: String,
    /// Configured maximum pending buffer age in hours
    pub max_pending_age_hours: i64,
}

/// Hook entry point for Claude Code integration
pub fn run_capture_hook() -> Result<()> {
    // Read input from stdin
    let input: HookInput = serde_json::from_reader(std::io::stdin())
        .context("Failed to read hook input from stdin")?;

    // Find repo root
    let repo_root = find_repo_root()?;

    // Only capture in repos that have been initialized with `whogitit init`
    if !is_repo_initialized(&repo_root) {
        return Ok(());
    }

    // Process the change
    let hook = CaptureHook::new(&repo_root)?;
    hook.on_file_change(input)?;

    Ok(())
}

/// Find the git repository root from current directory
fn find_repo_root() -> Result<std::path::PathBuf> {
    let current = env::current_dir()?;
    let repo = Repository::discover(&current).context("Not in a git repository")?;

    repo.workdir()
        .map(|p| p.to_path_buf())
        .context("Repository has no working directory")
}

/// Check if the repository has been initialized with `whogitit init`
/// by looking for the whogitit marker in the post-commit hook
fn is_repo_initialized(repo_root: &std::path::Path) -> bool {
    let post_commit = repo_root.join(".git/hooks/post-commit");
    if let Ok(content) = std::fs::read_to_string(&post_commit) {
        content.contains("whogitit")
    } else {
        false
    }
}

/// Git post-commit hook entry point
pub fn run_post_commit_hook() -> Result<()> {
    let repo_root = find_repo_root()?;
    let hook = CaptureHook::new(&repo_root)?;

    hook.on_post_commit()?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use git2::Signature;
    use tempfile::TempDir;

    fn create_test_repo() -> (TempDir, Repository) {
        let dir = TempDir::new().unwrap();
        let repo = Repository::init(dir.path()).unwrap();

        // Create initial commit
        {
            let sig = Signature::now("Test", "test@test.com").unwrap();
            let tree_id = repo.index().unwrap().write_tree().unwrap();
            let tree = repo.find_tree(tree_id).unwrap();
            repo.commit(Some("HEAD"), &sig, &sig, "Initial", &tree, &[])
                .unwrap();
        }

        (dir, repo)
    }

    #[test]
    fn test_capture_hook_on_file_change() {
        let (dir, _repo) = create_test_repo();
        let hook = CaptureHook::new(dir.path()).unwrap();

        let input = HookInput {
            tool: "Write".to_string(),
            file_path: "test.rs".to_string(),
            prompt: "Create a test file".to_string(),
            old_content: None,
            old_content_present: false,
            new_content: "fn test() {}\n".to_string(),
            context: None,
        };

        hook.on_file_change(input).unwrap();

        let status = hook.status().unwrap();
        assert!(status.has_pending);
        assert_eq!(status.file_count, 1);
        assert_eq!(status.edit_count, 1);
        assert_eq!(status.prompt_count, 1);
    }

    #[test]
    fn test_capture_hook_multiple_edits() {
        let (dir, _repo) = create_test_repo();
        let hook = CaptureHook::new(dir.path()).unwrap();

        // First edit
        hook.on_file_change(HookInput {
            tool: "Write".to_string(),
            file_path: "test.rs".to_string(),
            prompt: "Create file".to_string(),
            old_content: None,
            old_content_present: false,
            new_content: "line1\n".to_string(),
            context: None,
        })
        .unwrap();

        // Second edit to same file
        hook.on_file_change(HookInput {
            tool: "Edit".to_string(),
            file_path: "test.rs".to_string(),
            prompt: "Add line".to_string(),
            old_content: Some("line1\n".to_string()),
            old_content_present: true,
            new_content: "line1\nline2\n".to_string(),
            context: None,
        })
        .unwrap();

        let status = hook.status().unwrap();
        assert_eq!(status.file_count, 1);
        assert_eq!(status.edit_count, 2);
        assert_eq!(status.prompt_count, 2);
    }

    #[test]
    fn test_capture_hook_status_empty() {
        let (dir, _repo) = create_test_repo();
        let hook = CaptureHook::new(dir.path()).unwrap();

        let status = hook.status().unwrap();
        assert!(!status.has_pending);
        assert_eq!(status.file_count, 0);
    }

    #[test]
    fn test_capture_hook_clear() {
        let (dir, _repo) = create_test_repo();
        let hook = CaptureHook::new(dir.path()).unwrap();

        // Add a change
        hook.on_file_change(HookInput {
            tool: "Write".to_string(),
            file_path: "test.rs".to_string(),
            prompt: "test".to_string(),
            old_content: None,
            old_content_present: false,
            new_content: "content\n".to_string(),
            context: None,
        })
        .unwrap();

        assert!(hook.status().unwrap().has_pending);

        // Clear
        hook.clear_pending().unwrap();
        assert!(!hook.status().unwrap().has_pending);
    }

    #[test]
    fn test_post_commit_rename_preserves_attribution_path() {
        let (dir, repo) = create_test_repo();
        let repo_root = dir.path();

        // Create and commit initial file
        let old_path = repo_root.join("old.rs");
        std::fs::write(&old_path, "line1\n").unwrap();

        {
            let mut index = repo.index().unwrap();
            index.add_path(std::path::Path::new("old.rs")).unwrap();
            index.write().unwrap();
            let tree_id = index.write_tree().unwrap();
            let tree = repo.find_tree(tree_id).unwrap();
            let sig = Signature::now("Test", "test@test.com").unwrap();
            let head = repo.head().unwrap().peel_to_commit().unwrap();
            repo.commit(Some("HEAD"), &sig, &sig, "Add old.rs", &tree, &[&head])
                .unwrap();
        }

        let hook = CaptureHook::new(repo_root).unwrap();
        hook.on_file_change(HookInput {
            tool: "Edit".to_string(),
            file_path: "old.rs".to_string(),
            prompt: "Add line".to_string(),
            old_content: Some("line1\n".to_string()),
            old_content_present: true,
            new_content: "line1\nline2\n".to_string(),
            context: None,
        })
        .unwrap();

        // Rename file and commit
        let new_path = repo_root.join("new.rs");
        std::fs::rename(&old_path, &new_path).unwrap();

        {
            let mut index = repo.index().unwrap();
            index.remove_path(std::path::Path::new("old.rs")).unwrap();
            index.add_path(std::path::Path::new("new.rs")).unwrap();
            index.write().unwrap();
            let tree_id = index.write_tree().unwrap();
            let tree = repo.find_tree(tree_id).unwrap();
            let sig = Signature::now("Test", "test@test.com").unwrap();
            let head = repo.head().unwrap().peel_to_commit().unwrap();
            repo.commit(
                Some("HEAD"),
                &sig,
                &sig,
                "Rename old.rs to new.rs",
                &tree,
                &[&head],
            )
            .unwrap();
        }

        let attribution = hook.on_post_commit().unwrap().unwrap();
        assert_eq!(attribution.files.len(), 1);
        assert_eq!(attribution.files[0].path, "new.rs");
    }

    #[test]
    fn test_post_commit_preserves_pending_for_uncommitted_files() {
        let (dir, repo) = create_test_repo();
        let repo_root = dir.path();

        // Add baseline files and commit them.
        std::fs::write(repo_root.join("a.rs"), "a0\n").unwrap();
        std::fs::write(repo_root.join("b.rs"), "b0\n").unwrap();
        {
            let mut index = repo.index().unwrap();
            index.add_path(std::path::Path::new("a.rs")).unwrap();
            index.add_path(std::path::Path::new("b.rs")).unwrap();
            index.write().unwrap();
            let tree_id = index.write_tree().unwrap();
            let tree = repo.find_tree(tree_id).unwrap();
            let sig = Signature::now("Test", "test@test.com").unwrap();
            let head = repo.head().unwrap().peel_to_commit().unwrap();
            repo.commit(
                Some("HEAD"),
                &sig,
                &sig,
                "Add baseline files",
                &tree,
                &[&head],
            )
            .unwrap();
        }

        let hook = CaptureHook::new(repo_root).unwrap();

        // Capture edits for both files.
        hook.on_file_change(HookInput {
            tool: "Edit".to_string(),
            file_path: "a.rs".to_string(),
            prompt: "Update a".to_string(),
            old_content: Some("a0\n".to_string()),
            old_content_present: true,
            new_content: "a1\n".to_string(),
            context: None,
        })
        .unwrap();

        hook.on_file_change(HookInput {
            tool: "Edit".to_string(),
            file_path: "b.rs".to_string(),
            prompt: "Update b".to_string(),
            old_content: Some("b0\n".to_string()),
            old_content_present: true,
            new_content: "b1\n".to_string(),
            context: None,
        })
        .unwrap();

        std::fs::write(repo_root.join("a.rs"), "a1\n").unwrap();
        std::fs::write(repo_root.join("b.rs"), "b1\n").unwrap();

        // Commit only a.rs.
        {
            let mut index = repo.index().unwrap();
            index.add_path(std::path::Path::new("a.rs")).unwrap();
            index.write().unwrap();
            let tree_id = index.write_tree().unwrap();
            let tree = repo.find_tree(tree_id).unwrap();
            let sig = Signature::now("Test", "test@test.com").unwrap();
            let head = repo.head().unwrap().peel_to_commit().unwrap();
            repo.commit(
                Some("HEAD"),
                &sig,
                &sig,
                "Commit only a.rs",
                &tree,
                &[&head],
            )
            .unwrap();
        }

        let attribution = hook.on_post_commit().unwrap().unwrap();
        assert_eq!(attribution.files.len(), 1);
        assert_eq!(attribution.files[0].path, "a.rs");

        // b.rs should remain pending for a later commit.
        let store = PendingStore::new(repo_root);
        let remaining = store.load_quiet().unwrap().unwrap();
        assert!(remaining.get_file_history("a.rs").is_none());
        assert!(remaining.get_file_history("b.rs").is_some());

        let status = hook.status().unwrap();
        assert!(status.has_pending);
        assert_eq!(status.file_count, 1);
    }

    #[cfg(unix)]
    #[test]
    fn test_make_relative_path_accepts_symlinked_absolute_path() {
        let (dir, _repo) = create_test_repo();
        let repo_root = dir.path();
        let hook = CaptureHook::new(repo_root).unwrap();

        let alias_parent = TempDir::new().unwrap();
        let alias_root = alias_parent.path().join("repo-alias");
        std::os::unix::fs::symlink(repo_root, &alias_root).unwrap();

        let file_via_alias = alias_root.join("src").join("main.rs");
        std::fs::create_dir_all(file_via_alias.parent().unwrap()).unwrap();
        std::fs::write(&file_via_alias, "fn main() {}\n").unwrap();

        let relative = hook
            .make_relative_path(file_via_alias.to_str().unwrap())
            .unwrap();
        assert_eq!(relative, "src/main.rs");
    }

    #[cfg(unix)]
    #[test]
    fn test_make_relative_path_accepts_nonexistent_file_under_symlinked_root() {
        let (dir, _repo) = create_test_repo();
        let repo_root = dir.path();
        let hook = CaptureHook::new(repo_root).unwrap();

        let alias_parent = TempDir::new().unwrap();
        let alias_root = alias_parent.path().join("repo-alias");
        std::os::unix::fs::symlink(repo_root, &alias_root).unwrap();

        let nested_dir = alias_root.join("newdir");
        std::fs::create_dir_all(&nested_dir).unwrap();
        let missing_file_via_alias = nested_dir.join("created_later.rs");

        let relative = hook
            .make_relative_path(missing_file_via_alias.to_str().unwrap())
            .unwrap();
        assert_eq!(relative, "newdir/created_later.rs");
    }

    #[test]
    fn test_is_repo_initialized() {
        let dir = TempDir::new().unwrap();
        let hooks_dir = dir.path().join(".git/hooks");
        std::fs::create_dir_all(&hooks_dir).unwrap();

        // Not initialized - no hook file
        assert!(!is_repo_initialized(dir.path()));

        // Not initialized - hook exists but no whogitit marker
        std::fs::write(hooks_dir.join("post-commit"), "#!/bin/bash\necho hello").unwrap();
        assert!(!is_repo_initialized(dir.path()));

        // Initialized - hook contains whogitit
        std::fs::write(
            hooks_dir.join("post-commit"),
            "#!/bin/bash\nwhogitit commit",
        )
        .unwrap();
        assert!(is_repo_initialized(dir.path()));
    }
}