fetchium-core 1.0.0

Fetchium core library — search, extract, rank, validate, cache
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
//! Subscription-based credential detection for AI providers.
//!
//! Reads OAuth tokens that have already been set up by first-party CLIs:
//! - **Gemini**: reads `~/.gemini/oauth_creds.json` (populated by `gemini auth login`)
//! - **Claude Code**: reads from macOS Keychain service `"Claude Code-credentials"`
//! - **OpenAI Codex**: reads `~/.codex/auth.json` (populated by `codex auth login`)
//! - **OpenCode Antigravity**: reads `~/.config/opencode/antigravity-accounts.json`
//!   (populated by the `opencode-antigravity-auth` plugin; grants access to Gemini 3
//!   and Claude Sonnet/Opus models via Google Cloud Code Assist for free)
//!
//! No browser flows are initiated here — this module only reads existing credentials.

use serde::{Deserialize, Serialize};

// ─── Gemini OAuth credentials ─────────────────────────────────────────────────

/// Google OAuth credentials stored by the Gemini CLI at `~/.gemini/oauth_creds.json`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeminiOAuthCreds {
    /// Current access token (short-lived, ~1 hour).
    pub access_token: String,
    /// Long-lived refresh token.
    pub refresh_token: String,
    /// Expiry as Unix timestamp in **milliseconds**.
    pub expiry_date: u64,
    /// Always `"Bearer"`.
    pub token_type: String,
}

impl GeminiOAuthCreds {
    /// True if the access token has not yet expired.
    pub fn is_valid(&self) -> bool {
        let now_ms = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_millis() as u64)
            .unwrap_or(0);
        // Allow 60-second grace period
        self.expiry_date > now_ms + 60_000
    }

    /// True if a non-empty refresh token is present (can attempt a token refresh).
    ///
    /// Returns `false` after [`invalidate_gemini_creds`] has been called, indicating
    /// the session is permanently dead and the user must re-authenticate.
    pub fn is_refreshable(&self) -> bool {
        !self.refresh_token.is_empty()
    }
}

/// Zero-out the Gemini OAuth credentials in `~/.gemini/oauth_creds.json`.
///
/// Called when a token-refresh attempt returns HTTP 401 (refresh token revoked).
/// After this, [`read_gemini_creds`] returns a struct where `is_refreshable()` is
/// `false`, so `check_provider` correctly reports Gemini as unavailable.
pub fn invalidate_gemini_creds() {
    if let Some(path) = dirs::home_dir().map(|h| h.join(".gemini").join("oauth_creds.json")) {
        let cleared = serde_json::json!({
            "access_token":  "",
            "refresh_token": "",
            "expiry_date":   0u64,
            "token_type":    "Bearer",
        });
        let _ = std::fs::write(
            &path,
            serde_json::to_string_pretty(&cleared).unwrap_or_default(),
        );
    }
}

/// Read Gemini CLI OAuth credentials from `~/.gemini/oauth_creds.json`.
///
/// Returns `None` if the file does not exist or cannot be parsed.
pub fn read_gemini_creds() -> Option<GeminiOAuthCreds> {
    let path = dirs::home_dir()?.join(".gemini").join("oauth_creds.json");
    let content = std::fs::read_to_string(path).ok()?;
    serde_json::from_str(&content).ok()
}

/// Return a valid access token if one is cached locally, or `None` if expired/absent.
///
/// Call [`refresh_gemini_token`] (async) when this returns `None` due to expiry.
pub fn get_gemini_access_token_if_valid() -> Option<String> {
    let creds = read_gemini_creds()?;
    if creds.is_valid() {
        Some(creds.access_token.clone())
    } else {
        tracing::debug!("Gemini OAuth token expired — refresh needed");
        None
    }
}

/// Google's public OAuth client ID baked into the Gemini CLI package.
/// This is a "native app" / "installed app" OAuth client — the client_secret
/// is **not secret** for this client type.
pub const GEMINI_OAUTH_CLIENT_ID: &str =
    "681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com";
/// Public client secret for the Gemini CLI's installed-app OAuth client.
pub const GEMINI_OAUTH_CLIENT_SECRET: &str = "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";
/// Google OAuth token endpoint.
pub const GOOGLE_TOKEN_ENDPOINT: &str = "https://oauth2.googleapis.com/token";

// ─── Claude Code (Anthropic) OAuth credentials ───────────────────────────────

/// Claude Code OAuth credentials stored in the macOS Keychain.
#[derive(Debug, Clone)]
pub struct ClaudeOAuthCreds {
    /// OAuth access token for the Anthropic API (`sk-ant-oat01-...`).
    pub access_token: String,
    /// Refresh token (`sk-ant-ort01-...`).
    pub refresh_token: String,
    /// Token expiry as Unix timestamp in milliseconds.
    pub expires_at: u64,
    /// Subscription tier (e.g. `"max"`, `"pro"`).
    pub subscription_type: String,
}

impl ClaudeOAuthCreds {
    /// True if the access token has not yet expired.
    pub fn is_valid(&self) -> bool {
        let now_ms = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_millis() as u64)
            .unwrap_or(0);
        self.expires_at > now_ms + 60_000
    }
}

/// Read Claude Code OAuth credentials from the macOS Keychain.
///
/// Claude Code stores its session under service `"Claude Code-credentials"`.
/// Returns `None` on non-macOS systems or if credentials are not present.
pub fn read_claude_code_creds() -> Option<ClaudeOAuthCreds> {
    #[cfg(target_os = "macos")]
    {
        // Use the macOS `security` CLI to read from Keychain without extra deps.
        let output = std::process::Command::new("security")
            .args([
                "find-generic-password",
                "-s",
                "Claude Code-credentials",
                "-w",
            ])
            .output()
            .ok()?;

        if !output.status.success() {
            return None;
        }

        let raw = String::from_utf8(output.stdout).ok()?;
        let json: serde_json::Value = serde_json::from_str(raw.trim()).ok()?;
        let oauth = json.get("claudeAiOauth")?;

        let access_token = oauth.get("accessToken")?.as_str()?.to_string();
        let refresh_token = oauth.get("refreshToken")?.as_str()?.to_string();
        let expires_at = oauth.get("expiresAt")?.as_u64().unwrap_or(0);
        let subscription = oauth
            .get("subscriptionType")
            .and_then(|s| s.as_str())
            .unwrap_or("unknown")
            .to_string();

        Some(ClaudeOAuthCreds {
            access_token,
            refresh_token,
            expires_at,
            subscription_type: subscription,
        })
    }

    #[cfg(not(target_os = "macos"))]
    {
        // On Linux/Windows, check for a credentials file that users can create manually.
        let path = dirs::home_dir()?.join(".claude").join("credentials.json");
        let content = std::fs::read_to_string(path).ok()?;
        let json: serde_json::Value = serde_json::from_str(&content).ok()?;
        let oauth = json.get("claudeAiOauth")?;

        let access_token = oauth.get("accessToken")?.as_str()?.to_string();
        let refresh_token = oauth.get("refreshToken")?.as_str()?.to_string();
        let expires_at = oauth.get("expiresAt")?.as_u64().unwrap_or(0);
        let subscription = oauth
            .get("subscriptionType")
            .and_then(|s| s.as_str())
            .unwrap_or("unknown")
            .to_string();

        Some(ClaudeOAuthCreds {
            access_token,
            refresh_token,
            expires_at,
            subscription_type: subscription,
        })
    }
}

/// Get a usable Anthropic access token from Claude Code's session.
///
/// Returns `None` if Claude Code is not installed or the session has expired.
pub fn get_claude_code_token() -> Option<ClaudeOAuthCreds> {
    let creds = read_claude_code_creds()?;
    if creds.is_valid() {
        return Some(creds);
    }
    // Token expired — refreshing Anthropic OAuth requires hitting their
    // token endpoint which may change. For now, instruct user to re-run Claude Code.
    tracing::warn!(
        "Claude Code OAuth token has expired. Re-run `claude` once to refresh it, then retry."
    );
    None
}

// ─── Detection helpers ────────────────────────────────────────────────────────

/// Returns `true` if Gemini CLI is installed and has OAuth credentials.
pub fn gemini_cli_auth_available() -> bool {
    // Check gemini binary exists
    let has_binary = std::process::Command::new("gemini")
        .arg("--version")
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false);

    has_binary && read_gemini_creds().is_some()
}

/// Returns `true` if Claude Code OAuth credentials are available.
pub fn claude_code_auth_available() -> bool {
    read_claude_code_creds().is_some()
}

// ─── OpenAI Codex CLI credentials ────────────────────────────────────────────

/// OpenAI Codex CLI OAuth credentials stored at `~/.codex/auth.json`.
///
/// Populated by `codex auth login` (ChatGPT sign-in flow).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodexOAuthCreds {
    /// Short-lived access token (JWT, used as `Authorization: Bearer`).
    pub access_token: String,
    /// Refresh token for obtaining a new access token.
    pub refresh_token: Option<String>,
    /// Expiry as ISO 8601 / RFC 3339 string (e.g. `"2025-04-01T10:30:00Z"`).
    pub expires_at: Option<String>,
}

impl CodexOAuthCreds {
    /// True if the access token has not yet expired (or expiry is unknown).
    pub fn is_valid(&self) -> bool {
        let Some(ref exp_str) = self.expires_at else {
            return true; // no expiry field → assume valid
        };
        // Parse as RFC 3339; if parsing fails also assume valid
        let Ok(exp) = chrono::DateTime::parse_from_rfc3339(exp_str) else {
            return true;
        };
        let now = chrono::Utc::now();
        exp.signed_duration_since(now).num_seconds() > 60
    }
}

/// Read OpenAI Codex CLI credentials from `~/.codex/auth.json`.
///
/// Returns `None` if the file does not exist, cannot be read, or cannot be parsed.
pub fn read_codex_creds() -> Option<CodexOAuthCreds> {
    let path = dirs::home_dir()?.join(".codex").join("auth.json");
    let content = std::fs::read_to_string(path).ok()?;
    serde_json::from_str(&content).ok()
}

/// Return a valid OpenAI Codex access token, or `None` if absent/expired.
pub fn get_codex_token_if_valid() -> Option<String> {
    let creds = read_codex_creds()?;
    if creds.is_valid() {
        Some(creds.access_token.clone())
    } else {
        tracing::debug!("OpenAI Codex token expired — re-run `codex auth login` to refresh");
        None
    }
}

/// Returns `true` if OpenAI Codex CLI credentials are present and valid.
pub fn codex_auth_available() -> bool {
    get_codex_token_if_valid().is_some()
}

// ─── OpenRouter ───────────────────────────────────────────────────────────────

/// Read OpenRouter API key from its config file at `~/.openrouter/config.json`.
///
/// OpenRouter stores credentials in a simple JSON file after `openrouter auth`.
pub fn read_openrouter_key() -> Option<String> {
    let path = dirs::home_dir()?.join(".openrouter").join("config.json");
    let content = std::fs::read_to_string(path).ok()?;
    let json: serde_json::Value = serde_json::from_str(&content).ok()?;
    json.get("api_key")
        .or_else(|| json.get("apiKey"))
        .or_else(|| json.get("key"))
        .and_then(|v| v.as_str())
        .map(|s| s.to_string())
}

// ─── OpenCode Antigravity OAuth credentials ───────────────────────────────────
//
// The `opencode-antigravity-auth` plugin stores Google OAuth credentials at
// `~/.config/opencode/antigravity-accounts.json`. These credentials grant access
// to Google Cloud Code Assist, which proxies requests to Gemini 3 and Claude models.
//
// OAuth application (public installed-app client — secret is not sensitive):
//   Client ID:     1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com
//   Client secret: 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
//
// API endpoints (primary → autopush → prod fallback order):
//   https://daily-cloudcode-pa.sandbox.googleapis.com/v1internal:generateContent
//   https://autopush-cloudcode-pa.sandbox.googleapis.com/v1internal:generateContent
//   https://cloudcode-pa.googleapis.com/v1internal:generateContent

/// OAuth client ID for the OpenCode Antigravity plugin (installed-app, public).
pub const ANTIGRAVITY_CLIENT_ID: &str =
    "1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com";
/// Public client secret for the Antigravity installed-app OAuth client.
pub const ANTIGRAVITY_CLIENT_SECRET: &str = "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";

/// Primary Cloud Code Assist API endpoint (daily sandbox, mirrors CLIProxy behaviour).
pub const ANTIGRAVITY_ENDPOINT: &str = "https://daily-cloudcode-pa.sandbox.googleapis.com";
/// Fallback autopush endpoint.
pub const ANTIGRAVITY_ENDPOINT_AUTOPUSH: &str =
    "https://autopush-cloudcode-pa.sandbox.googleapis.com";
/// Fallback production endpoint.
pub const ANTIGRAVITY_ENDPOINT_PROD: &str = "https://cloudcode-pa.googleapis.com";

/// Ordered list of Cloud Code Assist endpoints to try (first success wins).
pub const ANTIGRAVITY_ENDPOINTS: &[&str] = &[
    ANTIGRAVITY_ENDPOINT,
    ANTIGRAVITY_ENDPOINT_AUTOPUSH,
    ANTIGRAVITY_ENDPOINT_PROD,
];

/// Default fallback project ID when the account has none.
pub const ANTIGRAVITY_DEFAULT_PROJECT_ID: &str = "rising-fact-p41fc";

/// Antigravity plugin version string (must stay in sync with the installed plugin).
pub const ANTIGRAVITY_VERSION: &str = "1.15.8";

/// A single account entry stored in `antigravity-accounts.json`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AntigravityAccount {
    /// Google account email address.
    pub email: String,
    /// Google OAuth refresh token (long-lived).
    pub refresh_token: String,
    /// Google Cloud project ID for quota routing.
    #[serde(default)]
    pub project_id: Option<String>,
    /// Timestamp when this account was added (string ISO 8601 or integer epoch ms).
    #[serde(default)]
    pub added_at: Option<serde_json::Value>,
    /// Timestamp when this account was last used (string ISO 8601 or integer epoch ms).
    #[serde(default)]
    pub last_used: Option<serde_json::Value>,
    /// Device fingerprint headers injected into requests.
    #[serde(default)]
    pub fingerprint: Option<serde_json::Value>,
}

impl AntigravityAccount {
    /// Returns the effective Google Cloud project ID, falling back to the default.
    pub fn effective_project_id(&self) -> &str {
        self.project_id
            .as_deref()
            .filter(|s| !s.is_empty())
            .unwrap_or(ANTIGRAVITY_DEFAULT_PROJECT_ID)
    }
}

/// The root structure of `antigravity-accounts.json`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AntigravityAccountStore {
    #[serde(default)]
    pub version: u32,
    #[serde(default)]
    pub accounts: Vec<AntigravityAccount>,
}

/// Read OpenCode Antigravity accounts from `~/.config/opencode/antigravity-accounts.json`.
///
/// Returns `None` if the file is absent, unreadable, or malformed.
pub fn read_antigravity_accounts() -> Option<AntigravityAccountStore> {
    let path = dirs::home_dir()?
        .join(".config")
        .join("opencode")
        .join("antigravity-accounts.json");
    let content = std::fs::read_to_string(path).ok()?;
    serde_json::from_str(&content).ok()
}

/// Return the first valid Antigravity account, or `None` if none exist.
pub fn get_primary_antigravity_account() -> Option<AntigravityAccount> {
    let store = read_antigravity_accounts()?;
    store.accounts.into_iter().next()
}

/// Returns `true` if at least one Antigravity account is stored locally.
pub fn antigravity_auth_available() -> bool {
    read_antigravity_accounts()
        .map(|s| !s.accounts.is_empty())
        .unwrap_or(false)
}

/// Refresh an Antigravity Google OAuth access token using the stored refresh token.
///
/// Uses the Antigravity installed-app OAuth client (client ID/secret are public).
/// Returns `(access_token, project_id)` on success, or `None` on failure.
pub fn refresh_antigravity_token(account: &AntigravityAccount) -> Option<(String, String)> {
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(15))
        .build()
        .ok()?;

    let params = [
        ("client_id", ANTIGRAVITY_CLIENT_ID),
        ("client_secret", ANTIGRAVITY_CLIENT_SECRET),
        ("refresh_token", account.refresh_token.as_str()),
        ("grant_type", "refresh_token"),
    ];

    let resp = client
        .post(GOOGLE_TOKEN_ENDPOINT)
        .form(&params)
        .send()
        .ok()?;

    if !resp.status().is_success() {
        tracing::warn!(
            "Antigravity token refresh failed (HTTP {}): email={}",
            resp.status(),
            account.email
        );
        return None;
    }

    let json: serde_json::Value = resp.json().ok()?;
    let access_token = json["access_token"].as_str()?.to_string();
    let project_id = account.effective_project_id().to_string();

    tracing::debug!(
        email = %account.email,
        project = %project_id,
        "Antigravity OAuth token refreshed"
    );
    Some((access_token, project_id))
}

/// Get a usable Antigravity access token (always refreshes — access tokens are short-lived).
///
/// Returns `(access_token, project_id)` or `None` if no account is configured
/// or the refresh request fails.
pub fn get_antigravity_token() -> Option<(String, String)> {
    let account = get_primary_antigravity_account()?;
    refresh_antigravity_token(&account)
}

// ─── Combined detection ───────────────────────────────────────────────────────

/// Describe which subscription auth methods are available on this machine.
pub fn detect_subscription_auth() -> Vec<SubscriptionAuth> {
    let mut found = Vec::new();

    if let Some(creds) = read_claude_code_creds() {
        found.push(SubscriptionAuth::ClaudeCode {
            subscription_type: creds.subscription_type.clone(),
            valid: creds.is_valid(),
        });
    }

    if let Some(creds) = read_gemini_creds() {
        // Try to get the active account email
        let account = std::fs::read_to_string(
            dirs::home_dir()
                .unwrap_or_default()
                .join(".gemini")
                .join("google_accounts.json"),
        )
        .ok()
        .and_then(|s| {
            let json: serde_json::Value = serde_json::from_str(&s).ok()?;
            json["active"].as_str().map(|s| s.to_string())
        });

        found.push(SubscriptionAuth::GeminiOAuth {
            account,
            valid: creds.is_valid(),
        });
    }

    if let Some(creds) = read_codex_creds() {
        found.push(SubscriptionAuth::CodexOAuth {
            valid: creds.is_valid(),
        });
    }

    if let Some(key) = read_openrouter_key() {
        let masked = if key.len() > 4 {
            format!("…{}", &key[key.len() - 4..])
        } else {
            "****".into()
        };
        found.push(SubscriptionAuth::OpenRouterKey { masked });
    }

    if let Some(account) = get_primary_antigravity_account() {
        found.push(SubscriptionAuth::AntigravityOAuth {
            email: account.email.clone(),
            project_id: account.effective_project_id().to_string(),
        });
    }

    found
}

/// A detected subscription-based authentication method.
#[derive(Debug, Clone)]
pub enum SubscriptionAuth {
    /// Claude Code OAuth session (Anthropic).
    ClaudeCode {
        subscription_type: String,
        valid: bool,
    },
    /// Google OAuth session (Gemini CLI).
    GeminiOAuth {
        account: Option<String>,
        valid: bool,
    },
    /// OpenAI Codex CLI OAuth session (ChatGPT subscription).
    CodexOAuth { valid: bool },
    /// OpenRouter API key stored in `~/.openrouter/config.json`.
    OpenRouterKey {
        /// Masked key (last 4 chars visible).
        masked: String,
    },
    /// OpenCode Antigravity OAuth session (Gemini 3 + Claude via Cloud Code Assist).
    AntigravityOAuth {
        /// Google account email.
        email: String,
        /// Google Cloud project ID used for quota routing.
        project_id: String,
    },
}

impl SubscriptionAuth {
    /// Human-readable description.
    pub fn description(&self) -> String {
        match self {
            Self::ClaudeCode {
                subscription_type,
                valid,
            } => format!(
                "Claude Code session ({subscription_type}) — {}",
                if *valid { "valid" } else { "expired" }
            ),
            Self::GeminiOAuth { account, valid } => format!(
                "Gemini OAuth ({}) — {}",
                account.as_deref().unwrap_or("unknown account"),
                if *valid {
                    "valid"
                } else {
                    "expired (needs refresh)"
                }
            ),
            Self::CodexOAuth { valid } => format!(
                "OpenAI Codex CLI session — {}",
                if *valid {
                    "valid"
                } else {
                    "expired (run `codex auth login`)"
                }
            ),
            Self::OpenRouterKey { masked } => {
                format!("OpenRouter API key ({masked}) — stored in ~/.openrouter/config.json")
            }
            Self::AntigravityOAuth { email, project_id } => format!(
                "OpenCode Antigravity OAuth ({email}, project: {project_id}) — Gemini 3 + Claude"
            ),
        }
    }
}

// ─── Unified Fetchium auth store (~/.fetchium/auth.json) ─────────────────────
//
// Modelled after OpenCode's auth.json. Stores API keys and OAuth tokens for all
// providers in a single JSON file with 0o600 permissions (owner read/write only).
//
// Format:
// {
//   "gemini":     { "type": "api", "key": "AIza..." }
//   "anthropic":  { "type": "oauth", "access": "...", "refresh": "...", "expires": 1234567890000 }
// }
//
// Path:
// ~/.fetchium/auth.json

/// Credential stored in `~/.fetchium/auth.json`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum FetchiumAuth {
    /// Single API key credential.
    Api { key: String },
    /// Multiple API keys — random selection with 429/rate-limit failover.
    ///
    /// Store additional keys with `fetchium provider set <name> --add-key KEY`.
    /// Keys are tried in random order; on HTTP 429 the next key is tried.
    ApiPool { keys: Vec<String> },
    /// OAuth token credential (access + refresh).
    Oauth {
        access: String,
        refresh: String,
        /// Expiry as Unix timestamp in milliseconds.
        expires: u64,
    },
}

impl FetchiumAuth {
    /// True if this credential contains at least one API key.
    pub fn is_api_key(&self) -> bool {
        matches!(self, Self::Api { .. } | Self::ApiPool { .. })
    }

    /// Return the first API key (single `Api` or first of `ApiPool`).
    ///
    /// Prefer [`api_keys`] when you need all keys for pool-based rotation.
    pub fn api_key(&self) -> Option<&str> {
        match self {
            Self::Api { key } => Some(key.as_str()),
            Self::ApiPool { keys } => keys.first().map(|k| k.as_str()),
            _ => None,
        }
    }

    /// Return all stored API keys.
    ///
    /// Returns a single-element slice for `Api`, all keys for `ApiPool`,
    /// and an empty vec for `Oauth`.
    pub fn api_keys(&self) -> Vec<&str> {
        match self {
            Self::Api { key } => vec![key.as_str()],
            Self::ApiPool { keys } => keys.iter().map(|k| k.as_str()).collect(),
            Self::Oauth { .. } => vec![],
        }
    }

    /// Number of API keys stored (0 for OAuth credentials).
    pub fn key_count(&self) -> usize {
        self.api_keys().len()
    }

    /// Return the access token if this is a valid, non-expired `Oauth` credential.
    pub fn valid_access_token(&self) -> Option<&str> {
        match self {
            Self::Oauth {
                access, expires, ..
            } => {
                let now_ms = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .map(|d| d.as_millis() as u64)
                    .unwrap_or(0);
                if *expires > now_ms + 60_000 {
                    Some(access.as_str())
                } else {
                    None
                }
            }
            _ => None,
        }
    }
}

/// Path to the unified auth store file.
///
/// Returns `~/.fetchium/auth.json`.
pub fn fetchium_auth_path() -> std::path::PathBuf {
    let home = dirs::home_dir().unwrap_or_default();
    home.join(".fetchium").join("auth.json")
}

/// Read all entries from the auth store.
pub fn fetchium_auth_all() -> std::collections::HashMap<String, FetchiumAuth> {
    let path = fetchium_auth_path();
    let content = match std::fs::read_to_string(&path) {
        Ok(c) => c,
        Err(_) => return std::collections::HashMap::new(),
    };
    let raw: std::collections::HashMap<String, serde_json::Value> =
        serde_json::from_str(&content).unwrap_or_default();
    raw.into_iter()
        .filter_map(|(k, v)| serde_json::from_value(v).ok().map(|auth| (k, auth)))
        .collect()
}

/// Read one entry from the auth store.
pub fn fetchium_auth_get(provider: &str) -> Option<FetchiumAuth> {
    fetchium_auth_all().remove(provider)
}

/// Write one entry to the auth store (creates the file if absent, 0o600 permissions).
///
/// Always writes to `~/.fetchium/auth.json`.
pub fn fetchium_auth_set(
    provider: &str,
    auth: FetchiumAuth,
) -> Result<(), Box<dyn std::error::Error>> {
    let home = dirs::home_dir().unwrap_or_default();
    let path = home.join(".fetchium").join("auth.json");

    let mut map = fetchium_auth_all();
    map.insert(provider.to_string(), auth);

    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    let content = serde_json::to_string_pretty(
        &map.into_iter()
            .map(|(k, v)| (k, serde_json::to_value(v).unwrap()))
            .collect::<std::collections::HashMap<_, _>>(),
    )?;

    // Write with restrictive permissions (0o600 — owner only)
    std::fs::write(&path, &content)?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600))?;
    }

    Ok(())
}

/// Add an API key to the provider's key pool in the auth store.
///
/// - If no entry exists, creates a new `ApiPool` with the single key.
/// - If a single `Api` key exists, converts it to an `ApiPool`.
/// - Deduplicates: if the key is already in the pool, this is a no-op.
///
/// Use `fetchium_auth_set` with `FetchiumAuth::ApiPool` to **replace** the entire pool.
pub fn fetchium_auth_add_api_key(
    provider: &str,
    new_key: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut existing: Vec<String> = fetchium_auth_get(provider)
        .map(|a| a.api_keys().into_iter().map(|s| s.to_string()).collect())
        .unwrap_or_default();

    let key_str = new_key.to_string();
    if !existing.contains(&key_str) {
        existing.push(key_str);
    }

    fetchium_auth_set(provider, FetchiumAuth::ApiPool { keys: existing })
}

/// Remove one entry from the auth store.
///
/// Always writes to `~/.fetchium/auth.json` (new canonical location).
pub fn fetchium_auth_remove(provider: &str) -> Result<(), Box<dyn std::error::Error>> {
    let home = dirs::home_dir().unwrap_or_default();
    let path = home.join(".fetchium").join("auth.json");

    let mut map = fetchium_auth_all();
    map.remove(provider);

    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let content = serde_json::to_string_pretty(
        &map.into_iter()
            .map(|(k, v)| (k, serde_json::to_value(v).unwrap()))
            .collect::<std::collections::HashMap<_, _>>(),
    )?;
    std::fs::write(&path, content)?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn gemini_creds_validity_future() {
        let now_ms = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_millis() as u64)
            .unwrap_or(0);
        let creds = GeminiOAuthCreds {
            access_token: "test".into(),
            refresh_token: "rt".into(),
            expiry_date: now_ms + 3_600_000, // 1 hour from now
            token_type: "Bearer".into(),
        };
        assert!(creds.is_valid());
    }

    #[test]
    fn gemini_creds_validity_expired() {
        let creds = GeminiOAuthCreds {
            access_token: "test".into(),
            refresh_token: "rt".into(),
            expiry_date: 1_000_000, // way in the past
            token_type: "Bearer".into(),
        };
        assert!(!creds.is_valid());
    }

    #[test]
    fn claude_oauth_validity_future() {
        let now_ms = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_millis() as u64)
            .unwrap_or(0);
        let creds = ClaudeOAuthCreds {
            access_token: "sk-ant-oat01-test".into(),
            refresh_token: "rt".into(),
            expires_at: now_ms + 7_200_000, // 2 hours
            subscription_type: "max".into(),
        };
        assert!(creds.is_valid());
    }
}