#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import re

SQL = "SELECT ACCOUNT AS name, PHONE AS phone FROM SYS_USER WHERE DELETE_FLAG = 'NOT_DELETE' ORDER BY NAME"
SAMPLE_CONTACTS = [
    {"name": "SuperPark (超管)", "phone": ""},
    {"name": "赤峰云铜", "phone": "07e1bc7b1c1bf563ccf844c1984c6ca4"},
    {"name": "松山产业园", "phone": "f51f3cfff3217f733286dcf0c563fa7e"},
    {"name": "红山产业园", "phone": "82475d6b8a5fb3e8096c3fa47062f072"},
    {"name": "元宝山产业园", "phone": "69f38a8fb6fcfe6c5a5c25e8f4e4c9d9"},
    {"name": "东山产业园", "phone": "0af8c32b1c6b7dc9e7fa1ca30606dee5"}
]

def normalize_phone(v: str) -> str:
    s = (v or '').strip()
    if not s:
        return '手机号缺失/不可用'
    if re.fullmatch(r'1\d{10}', s):
        return s
    if re.fullmatch(r'[0-9a-fA-F]{16,64}', s):
        return '手机号缺失/不可用'
    if '*' in s or len(s) < 7:
        return '手机号缺失/不可用'
    return s

def find_contact(keyword: str):
    key = (keyword or '').strip().lower()
    if not key:
        return None
    for c in SAMPLE_CONTACTS:
        if key == c['name'].lower() or key in c['name'].lower():
            return {'name': c['name'], 'phone': normalize_phone(c.get('phone', ''))}
    return None

result = {
    'status': 'need_contact_selection',
    'sql': SQL,
    'contacts': [{
        'name': c['name'],
        'phone': normalize_phone(c.get('phone', ''))
    } for c in SAMPLE_CONTACTS]
}
print(json.dumps(result, ensure_ascii=False))
