o
    |^j	                     @  s   d Z ddlmZ ddlZddlmZmZmZ dZd&d	d
Z	d'ddZ
d(ddZd(ddZd)ddZd*ddZd(ddZd+dd Zd!d"d,d$d%ZdS )-z6Shared Mcube phone validation helpers (Classic + 2.0).    )annotationsN)AnyDictOptionalz^[+]?[0-9]{6,}$exprstrreturnc              	   C  s*   d|  d}d|  d| dt  d| d	S )zDSQL expression: return trimmed phone only when 6+ digits, else NULL.z
TRIM(CAST(z
 AS CHAR))z
CASE WHEN z IS NOT NULL AND z	 REGEXP 'z' THEN z ELSE NULL END)VALID_PHONE_SQL_PATTERN)r   trimmed r   ;/home/aiteam/pcaa-dev/dashboard-backend/mcube_phone_util.pyvalid_phone_sql   s
   r   valuer   c                 C  s   d dd t| p	dD S )N c                 s  s    | ]	}|  r|V  qd S )N)isdigit).0chr   r   r   	<genexpr>   s    zphone_digits.<locals>.<genexpr>)joinr   r   r   r   r   phone_digits   s   r   boolc                 C  s   t t| dkS )N   )lenr   r   r   r   r   is_plausible_phone   s   r   c                 C  s,   t | pd }t|o| ot|dkS )zETrue when value looks like a Mcube eid/extension, not a display name.r   r   )r   stripr   r   r   )r   textr   r   r   is_numeric_agent_token   s   r   
agent_nameagent_callinfoc                 C  s8   t | pd }t |pd }|r|S t|rdS |S )zNDrop bare numeric agent ids from agent_callinfo when no agent name is present.r   )r   r   r   )r   r   nameliner   r   r   sanitize_agent_callinfo"   s   r"   callidc                 C  s$   t | }t|dk rdS |d d S )N
   r   )r   r   )r#   digitsr   r   r   phone_from_callid-   s   r&   c                 C  s8   t | }t|dk rdS |drdS |drdS dS )zJTrue for toll-free / virtual numbers that must not be used as lead phones.r$   F080T1800)r   r   
startswith)r   r%   r   r   r   is_likely_trunk_did4   s   

r*   rowDict[str, Any]c                 C  sp  t | dp| dpd }t|rt|s|S t | dp!d  }t | dp.d }t | dp9d }t| d}d}|d	kr\t|rQ|}n.tt|d
kr[|}n#t|rgt	|sg|}nt|rrt|sr|}nt|ry|}nt|r|}|s||fD ]}|rt|rt|s|} nq|rt|rt|s|rt|r|S |rt|rt|r|S dS |S )a  
    Resolve customer phone from a Mcube source/raw row.

    Mcube Classic inbound: callfrom is usually the customer.
    Mcube 2.0 inbound: callfrom is often an agent extension (e.g. 421) and callto
    holds the customer number.

    clicktocalldid is a virtual DID / click-to-call bridge number. It must not be
    used as the customer phone or unrelated outbound calls collapse under one lead.
    customer_callinfocustomer_phoner   	directioninboundcalltocallfromr#   outboundr$   )
r   getr   r   r*   lowerr&   r   r   r   )r+   explicitr/   r1   r2   callid_phoneresolvedr   r   r   r   pick_mcube_customer_phone@   sD    r9   r   )defaultr:   c                C  st   dD ]}t | |pd }t|r|  S qt| }t|r"|S dD ]}t | |p-d }|r7|  S q$|S )z0Best-effort customer phone for display/backfill.)
lead_phoner   )r-   r.   r;   )r   r4   r   r   r9   )r+   r:   keyr   r8   r   r   r   resolve_customer_phonet   s   r=   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   r   r   )r#   r   r   r   )r+   r,   r   r   )r+   r,   r:   r   r   r   )__doc__
__future__r   retypingr   r   r   r	   r   r   r   r   r"   r&   r*   r9   r=   r   r   r   r   <module>   s    

	





7