from __future__ import annotations

import json
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

from src.quant_engine.snapshot_admin_store_v1 import (
    DEFAULT_DB,
    DEFAULT_SEED_JSON,
    import_seed_json,
    load_account_snapshot_rows,
    load_settings_rows,
    parse_account_snapshot_tsv,
    validate_account_snapshot_rows,
    validate_settings_rows,
    write_export_json,
)

OUT = ROOT / "Temp" / "snapshot_admin_workflow_v1.json"


def main() -> int:
    db_path = DEFAULT_DB
    seed_path = DEFAULT_SEED_JSON
    summary = import_seed_json(db_path, seed_path)
    settings_rows = load_settings_rows(db_path)
    snapshot_rows = load_account_snapshot_rows(db_path)
    settings_errors = validate_settings_rows(settings_rows)
    snapshot_errors = validate_account_snapshot_rows(snapshot_rows)
    exported = write_export_json(db_path, ROOT / "Temp" / "snapshot_admin_export_v1.json")
    tsv_rows = parse_account_snapshot_tsv(
        "\n".join(
            [
                "captured_at\taccount\taccount_type\tticker\tname\tholding_quantity\tavailable_quantity\taverage_cost\ttotal_cost\tcurrent_price\tmarket_value\tprofit_loss\treturn_pct\timmediate_cash\tsettlement_cash_d2\tavailable_cash\topen_order_amount\tmonthly_contribution_limit\tmonthly_contribution_used\tparse_status\tuser_confirmed\tstop_price\thighest_price_since_entry\tentry_date\tentry_stage\tposition_type\tlast_updated",
                "2026-06-21T09:00:00+09:00\treal\t일반계좌\t005930\t삼성전자\t10\t10\t70000\t700000\t71000\t710000\t10000\t1.43\t1000000\t1000000\t1000000\t0\t\t\tCAPTURE_READ_OK\tY\t65000\t72000\t2026-06-01\tstage_1\tcore\t2026-06-21T09:05:00+09:00",
            ]
        )
    )
    payload = {
        "status": "PASS",
        "db_path": str(db_path),
        "seed_path": str(seed_path),
        "summary": summary,
        "settings_rows": len(settings_rows),
        "account_snapshot_rows": len(snapshot_rows),
        "settings_errors": settings_errors,
        "snapshot_errors": snapshot_errors,
        "export_path": str(exported),
        "tsv_parse_rows": len(tsv_rows),
    }
    OUT.parent.mkdir(parents=True, exist_ok=True)
    OUT.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
    print(json.dumps(payload, ensure_ascii=False, indent=2))
    if settings_errors or snapshot_errors:
        print("FAIL")
        return 1
    print("PASS")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
