#!/usr/bin/env python3
"""
TI沉淀/新进拆分 - 线索明细补全 v3
基于 xiaoxi_xhs_lead_detail.csv 进线月 3-5 月，join bi_vala_app_account
Join 键：手机号 XXTEA 加密 → tel_encrypt（主），UID→id（备）
输出：ti_pool_split_20260608_v3.csv
"""

import csv
import sys
sys.path.insert(0, '/root/.openclaw/workspace/scripts')
from phone_encrypt import encrypt_phone
import psycopg2
from datetime import datetime
from collections import defaultdict

# ── 1. 读取线索明细 ──────────────────────────────────────────
leads = []
with open('/root/.openclaw/workspace/tmp/xiaoxi_xhs_lead_detail.csv', 'r', encoding='utf-8') as f:
    reader = csv.DictReader(f)
    for row in reader:
        intake_month = row.get('进线月', '').strip()
        if intake_month in ('2026-03', '2026-04', '2026-05'):
            leads.append(row)

print(f"进线月 3-5 月共 {len(leads)} 条线索")

# ── 2. 收集所有手机号和用户ID ────────────────────────────────
phones = set()
user_ids = set()
for row in leads:
    phone = row.get('手机号', '').strip()
    uid_str = row.get('用户ID', '').strip()
    if phone:
        phones.add(phone)
    if uid_str and uid_str.isdigit():
        user_ids.add(int(uid_str))

print(f"去重手机号: {len(phones)} 个, 去重用户ID: {len(user_ids)} 个")

# ── 3. 加密手机号 ────────────────────────────────────────────
phone_enc_map = {}  # 密文 -> 明文
for p in phones:
    try:
        enc = encrypt_phone(p)
        phone_enc_map[enc] = p
    except Exception as e:
        print(f"  加密失败: {p}: {e}")

print(f"加密成功: {len(phone_enc_map)} 个")

# ── 4. 查询 bi_vala_app_account ──────────────────────────────
conn = psycopg2.connect(
    host='bj-postgres-16pob4sg.sql.tencentcdb.com',
    port=28591,
    user='ai_member',
    password='LdfjdjL83h3h3^$&**YGG*',
    dbname='vala_bi'
)
cur = conn.cursor()

# 4a. 按 tel_encrypt 匹配
enc_map = {}  # 明文phone -> {id, created_at, key_from}
batch_size = 500
enc_list = list(phone_enc_map.keys())
for i in range(0, len(enc_list), batch_size):
    batch = enc_list[i:i+batch_size]
    placeholders = ','.join(['%s'] * len(batch))
    cur.execute(f"""
        SELECT id, tel_encrypt, created_at, key_from
        FROM bi_vala_app_account
        WHERE tel_encrypt IN ({placeholders})
    """, batch)
    for row in cur.fetchall():
        enc_val = row[1]
        if enc_val in phone_enc_map:
            plain = phone_enc_map[enc_val]
            enc_map[plain] = {'id': row[0], 'created_at': row[2], 'key_from': row[3] or ''}

print(f"tel_encrypt 匹配到 {len(enc_map)} 个用户")

# 4b. 按 id 匹配（补充）
uid_list = list(user_ids)
id_map = {}
for i in range(0, len(uid_list), batch_size):
    batch = uid_list[i:i+batch_size]
    placeholders = ','.join(['%s'] * len(batch))
    cur.execute(f"""
        SELECT id, created_at, key_from
        FROM bi_vala_app_account
        WHERE id IN ({placeholders})
    """, batch)
    for row in cur.fetchall():
        id_map[row[0]] = {'created_at': row[1], 'key_from': row[2] or ''}

print(f"id 匹配到 {len(id_map)} 个用户")

# ── 5. 计算 prior_lead_same_phone ────────────────────────────
all_leads = []
with open('/root/.openclaw/workspace/tmp/xiaoxi_xhs_lead_detail.csv', 'r', encoding='utf-8') as f:
    reader = csv.DictReader(f)
    for row in reader:
        all_leads.append(row)

phone_earliest_month = {}
for row in all_leads:
    phone = row.get('手机号', '').strip()
    intake_month = row.get('进线月', '').strip()
    if phone and intake_month:
        if phone not in phone_earliest_month or intake_month < phone_earliest_month[phone]:
            phone_earliest_month[phone] = intake_month

print(f"全部线索去重手机号: {len(phone_earliest_month)} 个")

# ── 6. 组装输出 ──────────────────────────────────────────────
output_rows = []
for row in leads:
    phone = row.get('手机号', '').strip()
    uid_str = row.get('用户ID', '').strip()
    uid = int(uid_str) if uid_str.isdigit() else None
    intake_month = row.get('进线月', '').strip()
    intake_date_str = row.get('进线日期', '').strip()

    # Join 逻辑：优先 tel_encrypt，备 id
    acct = None
    join_method = ''
    if phone and phone in enc_map:
        acct = enc_map[phone]
        join_method = 'tel'
    elif uid and uid in id_map:
        acct = id_map[uid]
        join_method = 'id'

    create_time = acct['created_at'] if acct else None
    key_from = acct['key_from'] if acct else ''

    # register_before_intake: Y/N
    register_before_intake = ''
    days_register_to_intake = ''
    if create_time and intake_month:
        try:
            intake_first_day = datetime.strptime(intake_month + '-01', '%Y-%m-%d').date()
            create_date = create_time.date() if hasattr(create_time, 'date') else datetime.strptime(str(create_time)[:10], '%Y-%m-%d').date()
            register_before_intake = 'Y' if create_date < intake_first_day else 'N'
            if intake_date_str:
                intake_date = datetime.strptime(intake_date_str, '%Y-%m-%d').date()
                days_register_to_intake = (intake_date - create_date).days
        except:
            pass

    # prior_lead_same_phone: Y/N
    prior_lead_same_phone = ''
    if phone and intake_month and phone in phone_earliest_month:
        earliest = phone_earliest_month[phone]
        prior_lead_same_phone = 'Y' if earliest < intake_month else 'N'

    out = dict(row)
    out['create_time'] = str(create_time)[:19] if create_time else ''
    out['key_from'] = key_from
    out['register_before_intake'] = register_before_intake
    out['days_register_to_intake'] = days_register_to_intake
    out['prior_lead_same_phone'] = prior_lead_same_phone
    out['join_method'] = join_method
    output_rows.append(out)

# ── 7. 写 CSV ────────────────────────────────────────────────
fieldnames = list(leads[0].keys()) + ['create_time', 'key_from', 'register_before_intake', 'days_register_to_intake', 'prior_lead_same_phone', 'join_method']

with open('/root/.openclaw/workspace/output/ti_pool_split_20260608_v3.csv', 'w', encoding='utf-8-sig', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerows(output_rows)

print(f"\n输出: output/ti_pool_split_20260608_v3.csv, {len(output_rows)} 行")

# ── 8. 统计摘要 ──────────────────────────────────────────────
reg_y = sum(1 for r in output_rows if r['register_before_intake'] == 'Y')
reg_n = sum(1 for r in output_rows if r['register_before_intake'] == 'N')
reg_empty = sum(1 for r in output_rows if r['register_before_intake'] == '')
prior_y = sum(1 for r in output_rows if r['prior_lead_same_phone'] == 'Y')
prior_n = sum(1 for r in output_rows if r['prior_lead_same_phone'] == 'N')
prior_empty = sum(1 for r in output_rows if r['prior_lead_same_phone'] == '')
tel_join = sum(1 for r in output_rows if r['join_method'] == 'tel')
id_join = sum(1 for r in output_rows if r['join_method'] == 'id')
no_join = sum(1 for r in output_rows if r['join_method'] == '')

print(f"\n── 统计摘要 ──")
print(f"Join方式: tel={tel_join}, id={id_join}, 未匹配={no_join}")
print(f"注册早于进线月: Y={reg_y}, N={reg_n}, 空={reg_empty}")
print(f"同手机号早前进线: Y={prior_y}, N={prior_n}, 空={prior_empty}")

from collections import Counter
months = Counter(r['进线月'] for r in output_rows)
print(f"进线月分布: {dict(months)}")

cur.close()
conn.close()
