#!/usr/bin/env python3
"""福利品用户名单 — 合并 parent_address 并生成 Excel
用法: python3 welfare_merge.py <订单CSV文件> [起始日期] [结束日期]
  起始日期/结束日期仅用于文件命名，格式 YYYY-MM-DD
输出: output/福利品用户名单[_YYYY-MM-DD_YYYY-MM-DD].xlsx
"""

import csv
import os
import sys
from collections import Counter
from datetime import datetime

# ── 文件路径 ──────────────────────────────────────────────
ORDER_FILE = sys.argv[1] if len(sys.argv) > 1 else 'output/welfare_step1_result.txt'
START_DATE = sys.argv[2] if len(sys.argv) > 2 else ''
END_DATE = sys.argv[3] if len(sys.argv) > 3 else ''

WORKSPACE = '/root/.openclaw/workspace'
ADDR_FILE = f'{WORKSPACE}/output/parent_address_final.txt'

# 输出文件名
if START_DATE and END_DATE:
    suffix = f'_{START_DATE}_{END_DATE}'
elif START_DATE:
    suffix = f'_{START_DATE}_'
else:
    suffix = ''
OUTPUT = f'{WORKSPACE}/output/福利品用户名单{suffix}.xlsx'

# ── 读取订单数据 ───────────────────────────────────────────
orders = []
with open(ORDER_FILE, 'r') as f:
    first_line = f.readline()
    f.seek(0)
    delim = '\t' if '\t' in first_line else ','
    reader = csv.DictReader(f, delimiter=delim)
    for row in reader:
        orders.append(row)
print(f"读取订单数: {len(orders)}")

# 按 (account_id, trade_no) 去重
seen = set()
orders_dedup = []
dup_count = 0
for row in orders:
    key = (row['用户ID'], row['交易号'])
    if key in seen:
        dup_count += 1
        continue
    seen.add(key)
    orders_dedup.append(row)
print(f"去重后订单数: {len(orders_dedup)} (移除 {dup_count} 条重复)")

# ── 读取地址数据 ───────────────────────────────────────────
addr_map = {}
if os.path.exists(ADDR_FILE):
    with open(ADDR_FILE, 'r') as f:
        lines = f.readlines()
        header_idx = 0
        for i, line in enumerate(lines):
            if line.startswith('account_id'):
                header_idx = i
                break
        # 自动检测分隔符：优先检查第一行含 \t、| 还是 ,
        header_line = lines[header_idx]
        if '\t' in header_line:
            delim = '\t'
        elif '|' in header_line:
            delim = '|'
        else:
            delim = ','
        reader = csv.DictReader(lines[header_idx:], delimiter=delim)
        for row in reader:
            # 清理可能的空白
            row = {k.strip(): v.strip() if v else '' for k, v in row.items()}
            addr_map[row['account_id']] = row
    print(f"读取地址记录数: {len(addr_map)}")
else:
    print("⚠️ 未找到 parent_address 数据文件，地址列将为空")

# ── 合并 ──────────────────────────────────────────────────
matched = 0
unmatched = 0
for row in orders_dedup:
    aid = row['用户ID']
    if aid in addr_map:
        row['收件人'] = addr_map[aid].get('name', '')
        row['电话'] = addr_map[aid].get('phone_number', '')
        row['区域'] = addr_map[aid].get('region', '')
        row['地址'] = addr_map[aid].get('address', '')
        matched += 1
    else:
        row['收件人'] = ''
        row['电话'] = ''
        row['区域'] = ''
        row['地址'] = ''
        unmatched += 1
print(f"有地址: {matched}, 无地址: {unmatched}")

# ── 统计 ──────────────────────────────────────────────────
case_counts = Counter(row['来源情况'] for row in orders_dedup)
unique_users = set(row['用户ID'] for row in orders_dedup)
users_with_addr = set(row['用户ID'] for row in orders_dedup if row['收件人'])

user_case = {}
for row in orders_dedup:
    uid = row['用户ID']
    if uid not in user_case:
        user_case[uid] = set()
    user_case[uid].add(row['来源情况'])
case_user_counts = Counter('+'.join(sorted(v)) for v in user_case.values())

print(f"\n总用户数: {len(unique_users)} | 有地址: {len(users_with_addr)} | 无地址: {len(unique_users - users_with_addr)}")
print(f"总订单数: {len(orders_dedup)}")
print("情况分布（按用户数）:")
for k, v in case_user_counts.most_common():
    print(f"  {k}: {v}")

# ── 生成 Excel ────────────────────────────────────────────
try:
    from openpyxl import Workbook
    from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
    from openpyxl.utils import get_column_letter
except ImportError:
    os.system('pip install openpyxl -q')
    from openpyxl import Workbook
    from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
    from openpyxl.utils import get_column_letter

wb = Workbook()
header_fill = PatternFill(start_color='4472C4', end_color='4472C4', fill_type='solid')
header_font = Font(color='FFFFFF', bold=True, size=11)
thin_border = Border(
    left=Side(style='thin'), right=Side(style='thin'),
    top=Side(style='thin'), bottom=Side(style='thin')
)

# ── Sheet 1: 福利品用户名单 ────────────────────────────────
ws = wb.active
ws.title = "福利品用户名单"
headers = ['用户ID', '交易号', '商品ID', '商品名称', '渠道(key_from)', '购课日期',
           '支付金额(元)', '退款金额(元)', '退费状态', '来源情况',
           '收件人', '电话', '区域', '地址']

for col, h in enumerate(headers, 1):
    cell = ws.cell(row=1, column=col, value=h)
    cell.fill = header_fill
    cell.font = header_font
    cell.alignment = Alignment(horizontal='center', vertical='center')
    cell.border = thin_border

for i, row in enumerate(orders_dedup, 2):
    values = [
        int(row['用户ID']),
        row['交易号'],
        row['商品ID'],
        row['商品名称'].strip(),
        row['渠道'].strip(),
        row['购课日期'],
        float(row['支付金额(元)']),
        float(row['退款金额(元)']) if row['退款金额(元)'] else '',
        row['退费状态'],
        row['来源情况'],
        row['收件人'],
        row['电话'],
        row['区域'],
        row['地址'],
    ]
    for col, val in enumerate(values, 1):
        cell = ws.cell(row=i, column=col, value=val)
        cell.border = thin_border
        if col in (1, 2, 3, 7, 8, 9, 10):
            cell.alignment = Alignment(horizontal='center')
        elif col == 4:
            cell.alignment = Alignment(horizontal='left')

col_widths = [10, 22, 8, 22, 40, 20, 14, 14, 10, 14, 12, 16, 30, 40]
for i, w in enumerate(col_widths, 1):
    ws.column_dimensions[get_column_letter(i)].width = w
ws.freeze_panes = 'A2'

# ── Sheet 2: 统计汇总 ─────────────────────────────────────
ws2 = wb.create_sheet("统计汇总")
date_info = f" ({START_DATE} ~ {END_DATE})" if START_DATE and END_DATE else "（全部历史）"
ws2.merge_cells('A1:D1')
ws2.cell(row=1, column=1, value=f"福利品用户统计汇总{date_info}").font = Font(bold=True, size=14)
ws2.cell(row=2, column=1, value=f"总用户数: {len(unique_users)}")
ws2.cell(row=3, column=1, value=f"有地址的用户数: {len(users_with_addr)}")
ws2.cell(row=4, column=1, value=f"无地址的用户数: {len(unique_users - users_with_addr)}")
ws2.cell(row=5, column=1, value=f"总订单数: {len(orders_dedup)}")
ws2.cell(row=7, column=1, value="情况分布（按用户数）").font = Font(bold=True)
for i, (k, v) in enumerate(case_user_counts.most_common(), 8):
    ws2.cell(row=i, column=1, value=k)
    ws2.cell(row=i, column=2, value=v)
ws2.cell(row=7, column=4, value="情况分布（按订单行数）").font = Font(bold=True)
for i, (k, v) in enumerate(case_counts.most_common(), 8):
    ws2.cell(row=i, column=4, value=k)
    ws2.cell(row=i, column=5, value=v)

# ── Sheet 3: 用户地址汇总 ─────────────────────────────────
ws3 = wb.create_sheet("用户地址汇总")
addr_headers = ['用户ID', '收件人', '电话', '区域', '地址', '来源情况']
for col, h in enumerate(addr_headers, 1):
    cell = ws3.cell(row=1, column=col, value=h)
    cell.fill = header_fill
    cell.font = header_font
    cell.alignment = Alignment(horizontal='center', vertical='center')
    cell.border = thin_border

user_addrs = {}
for row in orders_dedup:
    uid = row['用户ID']
    if uid not in user_addrs:
        user_addrs[uid] = row
    elif row['来源情况'] not in user_addrs[uid]['来源情况']:
        user_addrs[uid]['来源情况'] = user_addrs[uid]['来源情况'] + '+' + row['来源情况']

for i, (uid, row) in enumerate(sorted(user_addrs.items()), 2):
    values = [int(uid), row['收件人'], row['电话'], row['区域'], row['地址'], row['来源情况']]
    for col, val in enumerate(values, 1):
        cell = ws3.cell(row=i, column=col, value=val)
        cell.border = thin_border
        cell.alignment = Alignment(horizontal='center')
        if col in (2, 4, 5):
            cell.alignment = Alignment(horizontal='left')

addr_col_widths = [10, 12, 16, 30, 40, 14]
for i, w in enumerate(addr_col_widths, 1):
    ws3.column_dimensions[get_column_letter(i)].width = w
ws3.freeze_panes = 'A2'

wb.save(OUTPUT)
print(f"\n✅ Excel 已保存: {OUTPUT}")
