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
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
"""
凭证管理路由模块 - 处理 /creds/* 相关的HTTP请求(仅 Antigravity 模式)
凭证以 email 为唯一键;导入/导出使用规范 JSON 格式:
  { "refresh_token": "...", "access_token": "...", "expiry": "...", "email": "...", "tier": "...", "project_id": "..." }
"""

import asyncio
import io
import json
import time
import zipfile
from typing import Any, Dict, List, Optional, Tuple

from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, Response
from fastapi.responses import JSONResponse

from log import log
from src.credential_manager import (
    add_credential,
    remove_credential,
    set_cred_disabled,
)
from src.models import (
    CredFileActionRequest,
    CredFileBatchActionRequest
)
from src.storage.database import get_database
from src.utils import (
    verify_panel_token,
    ANTIGRAVITY_USER_AGENT,
    ANTIGRAVITY_CLIENT_ID,
    ANTIGRAVITY_CLIENT_SECRET,
)
from src.api.antigravity import (
    fetch_quota_info,
    build_antigravity_headers,
    fetch_credential_models,
    refresh_credential_models,
)
from src.httpx_client import post_async
from src.google_oauth_api import (
    Credentials,
    fetch_project_id_and_tier,
    get_user_email,
)
from config import get_antigravity_api_url


# 创建路由器
router = APIRouter(prefix="/creds", tags=["credentials"])


# =============================================================================
# 凭证导入核心
# =============================================================================

async def _import_single_credential(data: Dict[str, Any]) -> Dict[str, Any]:
    """
    导入单个凭证(规范格式)。
    必须字段: refresh_token
    可选字段: email / tier / project_id / access_token / expiry
    (access_token 未过期时直接复用;否则用 refresh_token 刷新获取,其余缺失字段自动获取)

    Returns:
        {"status": "success"|"error", "email": ..., "message": ...}
    """
    try:
        refresh_token = (data.get("refresh_token") or "").strip()
        if not refresh_token:
            return {"status": "error", "message": "缺少 refresh_token"}

        email = (data.get("email") or "").strip().lower() or None
        tier = (data.get("tier") or "").strip().lower() or None
        project_id = (data.get("project_id") or "").strip() or None
        access_token = (str(data.get("access_token") or "")).strip()
        expiry = (str(data.get("expiry") or "")).strip()

        # 提供了未过期的 access_token 时直接复用(跳过刷新,避免批量导入触发 OAuth 限流)
        creds = None
        if access_token:
            candidate = Credentials.from_dict({
                "access_token": access_token,
                "refresh_token": refresh_token,
                "client_id": ANTIGRAVITY_CLIENT_ID,
                "client_secret": ANTIGRAVITY_CLIENT_SECRET,
                "expiry": expiry,
            })
            if not candidate.is_expired():
                creds = candidate

        # 无可用 access_token 时,用 refresh_token 换 access_token,同时验证凭证有效性
        if creds is None:
            creds = Credentials(
                access_token="",
                refresh_token=refresh_token,
                client_id=ANTIGRAVITY_CLIENT_ID,
                client_secret=ANTIGRAVITY_CLIENT_SECRET,
            )
            try:
                await creds.refresh()
            except Exception as e:
                return {"status": "error", "message": f"refresh_token 无效: {str(e)[:200]}"}

        if not creds.access_token:
            return {"status": "error", "message": "refresh_token 无效: 未获取到 access_token"}

        # 自动获取缺失的 email
        if not email:
            try:
                email = await get_user_email(creds)
            except Exception as e:
                log.warning(f"自动获取邮箱失败: {e}")
        if not email:
            return {"status": "error", "message": "无法获取邮箱(JSON 中未提供且自动获取失败)"}

        # 自动获取缺失的 project_id / tier
        if not project_id or not tier:
            try:
                api_base_url = await get_antigravity_api_url()
                fetched_project, fetched_tier = await fetch_project_id_and_tier(
                    access_token=creds.access_token,
                    user_agent=ANTIGRAVITY_USER_AGENT,
                    api_base_url=api_base_url,
                )
                if not project_id:
                    project_id = fetched_project
                if not tier:
                    tier = fetched_tier
            except Exception as e:
                log.warning(f"自动获取 project_id/tier 失败: {e}")

        # project_id 获取失败时回退为 aicode-consumers,避免空值入库
        if not project_id:
            log.warning(f"导入凭证 {email}: project_id 获取失败,使用回退值 aicode-consumers")
            project_id = "aicode-consumers"

        # 获取凭证支持的模型列表(若导入JSON中自带有效列表则优先复用,否则请求API获取)
        supported_models = data.get("supported_models")
        if not isinstance(supported_models, list) or not supported_models:
            supported_models = []
            try:
                fetched_models = await fetch_credential_models(creds.access_token)
                if fetched_models is not None:
                    supported_models = fetched_models
            except Exception as e:
                log.warning(f"导入凭证 {email}: 获取模型列表失败: {e}")

        credential_data = {
            "access_token": creds.access_token,
            "refresh_token": refresh_token,
            "client_id": ANTIGRAVITY_CLIENT_ID,
            "client_secret": ANTIGRAVITY_CLIENT_SECRET,
            "project_id": project_id,
            "expiry": creds.expires_at.isoformat() if creds.expires_at else "",
            "tier": tier,
            "supported_models": supported_models,
        }

        await add_credential(email, credential_data)
        return {
            "status": "success",
            "email": email,
            "tier": tier,
            "supported_models": supported_models,
            "message": "导入成功",
        }

    except Exception as e:
        return {"status": "error", "message": f"处理失败: {str(e)[:200]}"}


# =============================================================================
# 工具函数
# =============================================================================


async def extract_json_files_from_zip(zip_file: UploadFile) -> List[dict]:
    """从ZIP文件中提取JSON文件"""
    try:
        zip_content = await zip_file.read()
        files_data = []

        with zipfile.ZipFile(io.BytesIO(zip_content), "r") as zip_ref:
            file_list = zip_ref.namelist()
            json_files = [
                f for f in file_list if f.endswith(".json") and not f.startswith("__MACOSX/")
            ]

            if not json_files:
                raise HTTPException(status_code=400, detail="ZIP文件中没有找到JSON文件")

            log.info(f"从ZIP文件 {zip_file.filename} 中找到 {len(json_files)} 个JSON文件")

            for json_filename in json_files:
                try:
                    with zip_ref.open(json_filename) as json_file:
                        content = json_file.read()

                        try:
                            content_str = content.decode("utf-8")
                        except UnicodeDecodeError:
                            log.warning(f"跳过编码错误的文件: {json_filename}")
                            continue

                        filename = json_filename.rsplit("/", 1)[-1]
                        files_data.append({"filename": filename, "content": content_str})

                except Exception as e:
                    log.warning(f"处理ZIP中的文件 {json_filename} 时出错: {e}")
                    continue

        log.info(f"成功从ZIP文件中提取 {len(files_data)} 个有效的JSON文件")
        return files_data

    except zipfile.BadZipFile:
        raise HTTPException(status_code=400, detail="无效的ZIP文件格式")
    except Exception as e:
        if isinstance(e, HTTPException):
            raise
        log.error(f"处理ZIP文件失败: {e}")
        raise HTTPException(status_code=500, detail=f"处理ZIP文件失败: {str(e)}")


async def upload_credentials_common( files: List[UploadFile] ) -> JSONResponse:
    """批量导入凭证文件(规范 JSON:refresh_token 必填,access_token/expiry/email/tier/project_id 可选)"""

    if not files:
        raise HTTPException(status_code=400, detail="请选择要上传的文件")

    if len(files) > 100:
        raise HTTPException(
            status_code=400, detail=f"文件数量过多,最多支持100个文件,当前:{len(files)}个"
        )

    files_data = []
    for file in files:
        if file.filename.endswith(".zip"):
            zip_files_data = await extract_json_files_from_zip(file)
            files_data.extend(zip_files_data)
            log.info(f"从ZIP文件 {file.filename} 中提取了 {len(zip_files_data)} 个JSON文件")

        elif file.filename.endswith(".json"):
            content_chunks = []
            while True:
                chunk = await file.read(8192)
                if not chunk:
                    break
                content_chunks.append(chunk)

            content = b"".join(content_chunks)
            try:
                content_str = content.decode("utf-8")
            except UnicodeDecodeError:
                raise HTTPException(
                    status_code=400, detail=f"文件 {file.filename} 编码格式不支持"
                )

            files_data.append({"filename": file.filename, "content": content_str})
        else:
            raise HTTPException(
                status_code=400, detail=f"文件 {file.filename} 格式不支持,只支持JSON和ZIP文件"
            )

    batch_size = 100
    all_results = []
    total_success = 0

    for i in range(0, len(files_data), batch_size):
        batch_files = files_data[i : i + batch_size]

        async def process_single_file(file_data):
            try:
                data = json.loads(file_data["content"])
                if not isinstance(data, dict):
                    return {
                        "filename": file_data["filename"],
                        "status": "error",
                        "message": "JSON格式错误: 顶层必须是对象",
                    }
                result = await _import_single_credential(data)
                result["filename"] = file_data["filename"]
                return result
            except json.JSONDecodeError as e:
                return {
                    "filename": file_data["filename"],
                    "status": "error",
                    "message": f"JSON格式错误: {str(e)}",
                }
            except Exception as e:
                return {
                    "filename": file_data["filename"],
                    "status": "error",
                    "message": f"处理失败: {str(e)[:200]}",
                }

        log.info(f"开始并发处理 {len(batch_files)} 个凭证...")
        concurrent_tasks = [process_single_file(file_data) for file_data in batch_files]
        batch_results = await asyncio.gather(*concurrent_tasks, return_exceptions=True)

        batch_uploaded_count = 0
        for result in batch_results:
            if isinstance(result, Exception):
                all_results.append(
                    {
                        "filename": "unknown",
                        "status": "error",
                        "message": f"处理异常: {str(result)[:200]}",
                    }
                )
            else:
                all_results.append(result)
                if result["status"] == "success":
                    batch_uploaded_count += 1

        total_success += batch_uploaded_count

        batch_num = (i // batch_size) + 1
        total_batches = (len(files_data) + batch_size - 1) // batch_size
        log.info(
            f"批次 {batch_num}/{total_batches} 完成: 成功 "
            f"{batch_uploaded_count}/{len(batch_files)} 个凭证"
        )

    return JSONResponse(
        content={
            "uploaded_count": total_success,
            "total_count": len(files_data),
            "results": all_results,
            "message": f"批量导入完成: 成功 {total_success}/{len(files_data)} 个凭证",
        }
    )


async def get_creds_status_common(
    offset: int, limit: int, status_filter: str,
    error_code_filter: str = None, cooldown_filter: str = None, tier_filter: str = None
) -> JSONResponse:
    """获取凭证状态"""
    if offset < 0:
        raise HTTPException(status_code=400, detail="offset 必须大于等于 0")
    if limit not in [20, 50, 100, 200, 500, 1000]:
        raise HTTPException(status_code=400, detail="limit 只能是 20、50、100、200、500 或 1000")
    if status_filter not in ["all", "enabled", "disabled"]:
        raise HTTPException(status_code=400, detail="status_filter 只能是 all、enabled 或 disabled")
    if cooldown_filter and cooldown_filter not in ["all", "in_cooldown", "no_cooldown"]:
        raise HTTPException(status_code=400, detail="cooldown_filter 只能是 all、in_cooldown 或 no_cooldown")
    if tier_filter and tier_filter not in ["all", "free", "pro", "ultra"]:
        raise HTTPException(status_code=400, detail="tier_filter 只能是 all、free、pro 或 ultra")

    database = await get_database()
    result = await database.get_credentials_summary(
        offset=offset,
        limit=limit,
        status_filter=status_filter,
        error_code_filter=error_code_filter if error_code_filter and error_code_filter != "all" else None,
        cooldown_filter=cooldown_filter if cooldown_filter and cooldown_filter != "all" else None,
        tier_filter=tier_filter if tier_filter and tier_filter != "all" else None
    )

    return JSONResponse(content={
        "items": result["items"],
        "total": result["total"],
        "offset": offset,
        "limit": limit,
        "has_more": (offset + limit) < result["total"],
        "stats": result.get("stats", {"total": 0, "normal": 0, "disabled": 0}),
    })


async def download_all_creds_common() -> Response:
    """打包导出所有凭证(规范格式,文件名=邮箱.json)"""
    zip_filename = "antigravity_credentials.zip"

    database = await get_database()
    emails = await database.list_credentials()

    if not emails:
        raise HTTPException(status_code=404, detail="没有找到凭证")

    log.info(f"开始打包 {len(emails)} 个凭证...")

    zip_buffer = io.BytesIO()

    with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
        success_count = 0
        for email in emails:
            try:
                state = await database.get_credential_state(email)
                # 从凭证数据中取 refresh_token / access_token / expiry
                cred = await database.get_credential(email)
                if not cred:
                    continue

                export_data = {
                    "refresh_token": cred.get("refresh_token", ""),
                    "access_token": cred.get("access_token", ""),
                    "expiry": cred.get("expiry", ""),
                    "email": email,
                    "tier": state.get("tier", "pro"),
                    "project_id": state.get("project_id", ""),
                    "supported_models": state.get("supported_models", []),
                }

                content = json.dumps(export_data, ensure_ascii=False, indent=2)
                zip_file.writestr(f"{email}.json", content)
                success_count += 1
            except Exception as e:
                log.warning(f"导出凭证 {email} 时出错: {e}")
                continue

    log.info(f"打包完成: 成功 {success_count}/{len(emails)} 个凭证")

    zip_buffer.seek(0)
    return Response(
        content=zip_buffer.getvalue(),
        media_type="application/zip",
        headers={"Content-Disposition": f"attachment; filename={zip_filename}"},
    )


async def verify_credential_project_common(email: str) -> JSONResponse:
    """验证并重新获取凭证的 project id / tier"""

    database = await get_database()

    credential_data = await database.get_credential(email)
    if not credential_data:
        raise HTTPException(status_code=404, detail="凭证不存在")

    credentials = Credentials.from_dict(credential_data)

    token_refreshed = await credentials.refresh_if_needed()

    if token_refreshed:
        log.info(f"Token已自动刷新: {email}")
        credential_data = credentials.to_dict()
        await database.store_credential(email, credential_data)

    # 重新获取 project id / tier
    api_base_url = await get_antigravity_api_url()
    project_id, subscription_tier = await fetch_project_id_and_tier(
        access_token=credentials.access_token,
        user_agent=ANTIGRAVITY_USER_AGENT,
        api_base_url=api_base_url,
    )

    # project_id 获取失败时回退,避免空值入库
    if not project_id:
        log.warning(f"校验凭证 {email}: project_id 获取失败,使用回退值 aicode-consumers")
        project_id = "aicode-consumers"

    # tier 获取失败时保留数据库原值,不覆盖为 None
    old_state = await database.get_credential_state(email)
    if not subscription_tier:
        subscription_tier = old_state.get("tier") or "pro"
        log.warning(f"校验凭证 {email}: tier 获取失败,保留原值 {subscription_tier}")

    # 同步刷新该凭证支持的模型列表
    supported_models = old_state.get("supported_models", [])
    try:
        fetched_models = await fetch_credential_models(credentials.access_token)
        if fetched_models is not None:
            supported_models = fetched_models
    except Exception as e:
        log.warning(f"校验凭证 {email}: 获取模型列表失败,保留原值: {e}")

    credential_data["project_id"] = project_id
    credential_data["supported_models"] = supported_models

    await database.store_credential(email, credential_data)

    state_update = {
        "disabled": False,
        "error_codes": [],
        "tier": subscription_tier,
        "project_id": project_id,
        "supported_models": supported_models,
    }

    await database.update_credential_state(email, state_update)

    log.info(
        f"检验凭证成功: {email} - Project ID: {project_id}, Tier: {subscription_tier}, "
        f"Models: {len(supported_models)}"
    )

    return JSONResponse(content={
        "success": True,
        "email": email,
        "project_id": project_id,
        "subscription_tier": subscription_tier,
        "supported_models": supported_models,
        "message": "检验成功!已解除禁用状态、清除错误码并同步模型列表",
    })


# =============================================================================
# 路由处理函数
# =============================================================================


@router.post("/upload")
async def upload_credentials( files: List[UploadFile] = File(...),
    token: str = Depends(verify_panel_token),
):
    """批量导入凭证文件(规范 JSON / ZIP)"""
    try:
        return await upload_credentials_common(files)
    except HTTPException:
        raise
    except Exception as e:
        log.error(f"批量导入失败: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/status")
async def get_creds_status(
    token: str = Depends(verify_panel_token),
    offset: int = 0,
    limit: int = 50,
    status_filter: str = "all",
    error_code_filter: str = "all",
    cooldown_filter: str = "all",
    tier_filter: str = "all"):
    """获取凭证状态(轻量级摘要,支持分页和状态筛选)"""
    try:
        return await get_creds_status_common(
            offset, limit, status_filter,
            error_code_filter=error_code_filter,
            cooldown_filter=cooldown_filter,
            tier_filter=tier_filter
        )
    except HTTPException:
        raise
    except Exception as e:
        log.error(f"获取凭证状态失败: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/action")
async def creds_action(
    request: CredFileActionRequest,
    token: str = Depends(verify_panel_token)):
    """对凭证执行操作(启用/禁用/删除)"""
    try:

        email = request.email
        action = request.action

        log.info(f"Performing action '{action}' on credential: {email}")

        database = await get_database()

        if action == "enable":
            result = await set_cred_disabled(email, False)
            if result:
                return JSONResponse(content={"message": f"已启用凭证 {email}"})
            raise HTTPException(status_code=500, detail="启用凭证失败,可能凭证不存在")

        elif action == "disable":
            result = await set_cred_disabled(email, True)
            if result:
                return JSONResponse(content={"message": f"已禁用凭证 {email}"})
            raise HTTPException(status_code=500, detail="禁用凭证失败,可能凭证不存在")

        elif action == "delete":
            success = await remove_credential(email)
            if success:
                return JSONResponse(content={"message": f"已删除凭证 {email}"})
            raise HTTPException(status_code=500, detail="删除凭证失败")

        else:
            raise HTTPException(status_code=400, detail="无效的操作类型")

    except HTTPException:
        raise
    except Exception as e:
        log.error(f"凭证操作失败: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/batch-action")
async def creds_batch_action(
    request: CredFileBatchActionRequest,
    token: str = Depends(verify_panel_token)):
    """批量对凭证执行操作(启用/禁用/删除)"""
    try:

        action = request.action
        emails = request.emails

        if not emails:
            raise HTTPException(status_code=400, detail="凭证列表不能为空")

        log.info(f"对 {len(emails)} 个凭证执行批量操作 '{action}'")

        success_count = 0
        errors = []

        for email in emails:
            try:
                if action == "enable":
                    await set_cred_disabled(email, False)
                    success_count += 1
                elif action == "disable":
                    await set_cred_disabled(email, True)
                    success_count += 1
                elif action == "delete":
                    if await remove_credential(email):
                        success_count += 1
                    else:
                        errors.append(f"{email}: 删除失败")
                        continue
                elif action == "refresh_models":
                    models = await refresh_credential_models(email)
                    if models is not None:
                        success_count += 1
                    else:
                        errors.append(f"{email}: 刷新模型列表失败")
                        continue
                else:
                    errors.append(f"{email}: 无效的操作类型")
                    continue
            except Exception as e:
                log.error(f"处理 {email} 时出错: {e}")
                errors.append(f"{email}: 处理失败 - {str(e)}")
                continue

        result_message = f"批量操作完成:成功处理 {success_count}/{len(emails)} 个凭证"
        if errors:
            result_message += "\n错误详情:\n" + "\n".join(errors)

        return JSONResponse(content={
            "success_count": success_count,
            "total_count": len(emails),
            "errors": errors,
            "message": result_message,
        })

    except HTTPException:
        raise
    except Exception as e:
        log.error(f"批量凭证操作失败: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/export/{email}")
async def export_credential(
    email: str,
    token: str = Depends(verify_panel_token)):
    """导出单个凭证(规范格式,文件名=邮箱.json)"""
    try:
        database = await get_database()

        cred = await database.get_credential(email)
        if not cred:
            raise HTTPException(status_code=404, detail="凭证不存在")

        state = await database.get_credential_state(email)

        export_data = {
            "refresh_token": cred.get("refresh_token", ""),
            "access_token": cred.get("access_token", ""),
            "expiry": cred.get("expiry", ""),
            "email": email,
            "tier": state.get("tier", "pro"),
            "project_id": cred.get("project_id", ""),
            "supported_models": state.get("supported_models", []),
        }

        content = json.dumps(export_data, ensure_ascii=False, indent=2)

        return Response(
            content=content,
            media_type="application/json",
            headers={"Content-Disposition": f'attachment; filename="{email}.json"'},
        )

    except HTTPException:
        raise
    except Exception as e:
        log.error(f"导出凭证失败: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/download-all")
async def download_all_creds(
    token: str = Depends(verify_panel_token)):
    """打包导出所有凭证(每个文件为 邮箱.json)"""
    try:
        return await download_all_creds_common()
    except HTTPException:
        raise
    except Exception as e:
        log.error(f"打包导出失败: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/verify-project/{email}")
async def verify_credential_project(
    email: str,
    token: str = Depends(verify_panel_token)):
    """检验凭证(重新获取 Project ID / Tier,可修复 403)"""
    try:
        return await verify_credential_project_common(email)
    except HTTPException:
        raise
    except Exception as e:
        log.error(f"检验凭证失败 {email}: {e}")
        raise HTTPException(status_code=500, detail=f"检验失败: {str(e)}")


@router.post("/refresh-models/{email}")
async def refresh_cred_models_endpoint(
    email: str,
    token: str = Depends(verify_panel_token),
):
    """刷新指定凭证支持的模型列表"""
    try:
        database = await get_database()
        cred = await database.get_credential(email)
        if not cred:
            raise HTTPException(status_code=404, detail="凭证不存在")

        models = await refresh_credential_models(email, cred)
        if models is None:
            raise HTTPException(status_code=400, detail="获取凭证模型列表失败,请检查凭证是否有效")

        return JSONResponse(content={
            "success": True,
            "email": email,
            "supported_models": models,
            "count": len(models),
            "message": f"成功刷新模型列表(共 {len(models)} 个模型)",
        })
    except HTTPException:
        raise
    except Exception as e:
        log.error(f"刷新凭证模型列表失败 {email}: {e}")
        raise HTTPException(status_code=500, detail=f"刷新模型列表失败: {str(e)}")


@router.get("/errors/{email}")
async def get_credential_errors(
    email: str,
    token: str = Depends(verify_panel_token)):
    """获取凭证的错误信息"""
    try:
        database = await get_database()
        error_info = await database.get_credential_errors(email)
        return JSONResponse(content=error_info)

    except HTTPException:
        raise
    except Exception as e:
        log.error(f"获取凭证错误信息失败 {email}: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/quota/{email}")
async def get_credential_quota(
    email: str,
    token: str = Depends(verify_panel_token)):
    """获取凭证的额度信息"""
    try:
        database = await get_database()

        credential_data = await database.get_credential(email)
        if not credential_data:
            raise HTTPException(status_code=404, detail="凭证不存在")

        creds = Credentials.from_dict(credential_data)

        await creds.refresh_if_needed()

        updated_data = creds.to_dict()
        if updated_data != credential_data:
            log.info(f"Token已自动刷新: {email}")
            await database.store_credential(email, updated_data)
            credential_data = updated_data

        access_token = credential_data.get("access_token") or credential_data.get("token")
        if not access_token:
            raise HTTPException(status_code=400, detail="凭证中没有访问令牌")

        quota_info = await fetch_quota_info(access_token, credential_data.get("project_id", ""))

        if quota_info.get("success"):
            result = {
                "success": True,
                "email": email,
                "models": quota_info.get("models", {})
            }
            for extra_key in ("minResetDays", "accountTier", "isPro", "quotaGroups", "source"):
                if extra_key in quota_info:
                    result[extra_key] = quota_info[extra_key]
            return JSONResponse(content=result)
        else:
            return JSONResponse(
                status_code=400,
                content={
                    "success": False,
                    "email": email,
                    "error": quota_info.get("error", "未知错误")
                }
            )

    except HTTPException:
        raise
    except Exception as e:
        log.error(f"获取凭证额度失败 {email}: {e}")
        raise HTTPException(status_code=500, detail=f"获取额度失败: {str(e)}")


@router.post("/test/{email}")
async def test_credential(
    email: str,
    _token: str = Depends(verify_panel_token)
):
    """
    测试凭证是否可用

    Returns:
        - 200: 凭证可用
        - 429: 凭证被限流但有效
        - 其他: 凭证失败(返回实际错误码)
    """
    try:
        database = await get_database()

        credential_data = await database.get_credential(email)
        if not credential_data:
            raise HTTPException(status_code=404, detail="凭证不存在")

        credentials = Credentials.from_dict(credential_data)
        token_refreshed = await credentials.refresh_if_needed()

        if token_refreshed:
            log.info(f"Token已自动刷新: {email}")
            credential_data = credentials.to_dict()
            await database.store_credential(email, credential_data)

        access_token = credential_data.get("access_token") or credential_data.get("token")
        if not access_token:
            raise HTTPException(status_code=400, detail="凭证中没有访问令牌")

        project_id = credential_data.get("project_id", "")
        if not project_id:
            raise HTTPException(status_code=400, detail="凭证中没有项目ID")

        test_model = "gemini-2.5-flash-lite"

        api_base_url = await get_antigravity_api_url()
        headers = build_antigravity_headers(access_token)

        response = await post_async(
            url=f"{api_base_url}/v1internal:generateContent",
            json={
                "model": test_model,
                "project": project_id,
                "request": {
                    "contents": [{"role": "user", "parts": [{"text": "hi"}]}]
                }
            },
            headers=headers,
            timeout=30.0
        )

        status_code = response.status_code

        if status_code == 200 or status_code == 429:
            log.info(f"凭证测试成功: {email} (status={status_code})")
            if status_code == 200:
                await database.update_credential_state(email, {
                    "error_codes": [],
                    "error_messages": {}
                })

            return JSONResponse(
                status_code=status_code,
                content={
                    "success": True,
                    "status_code": status_code,
                    "message": "测试成功",
                    "email": email
                }
            )
        else:
            log.warning(f"凭证测试失败: {email} (status={status_code})")
            try:
                error_text = response.text if hasattr(response, "text") else ""
                error_codes = [status_code]
                error_messages = {str(status_code): error_text if error_text else f"HTTP {status_code}"}

                await database.update_credential_state(email, {
                    "error_codes": error_codes,
                    "error_messages": error_messages
                })
            except Exception as e:
                log.error(f"保存测试错误信息失败: {e}")

        error_text = response.text if hasattr(response, "text") else ""

        return JSONResponse(
            status_code=status_code,
            content={
                "success": False,
                "status_code": status_code,
                "message": f"测试失败: HTTP {status_code}",
                "error": error_text,
                "email": email
            }
        )

    except HTTPException:
        raise
    except Exception as e:
        log.error(f"测试凭证失败 {email}: {e}")
        raise HTTPException(status_code=500, detail=f"测试失败: {str(e)}")