
"""
Phase 7: Self-Service Optimization & Channel Effectiveness Analysis
Channel profiling, password reset analysis, funnel analysis
"""

import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from pathlib import Path
import warnings
warnings.filterwarnings('ignore')

# Paths
OUTPUT_DIR = Path("/app/workspace/temp_files")
IMG_DIR = Path("/app/workspace/assets/images")

print("=" * 80)
print("PHASE 7: SELF-SERVICE OPTIMIZATION & CHANNEL EFFECTIVENESS")
print("=" * 80)

# Load cleaned data
incidents_df = pd.read_pickle(OUTPUT_DIR / "incidents_cleaned.pkl")
tasks_df = pd.read_pickle(OUTPUT_DIR / "tasks_cleaned.pkl")

# ============================================================================
# 1. CHANNEL PREFERENCE PROFILING & FUNNEL ANALYSIS
# ============================================================================
print("\n📱 CHANNEL PREFERENCE ANALYSIS")
print("-" * 40)

# Incident channel distribution
channel_dist = incidents_df['Channel'].value_counts()
print("\n  Incident Channel Distribution:")
for ch, count in channel_dist.items():
    print(f"    {ch}: {count:,} ({count/len(incidents_df)*100:.1f}%)")

# Channel funnel
total_incidents = len(incidents_df)
channel_data = channel_dist.reset_index()
channel_data.columns = ['Channel', 'Count']
channel_data['Percentage'] = (channel_data['Count'] / total_incidents * 100).round(1)

fig_channel = px.bar(
    channel_data, x='Channel', y='Count',
    title='Incident Volume by Channel',
    labels={'Channel': 'Channel', 'Count': 'Number of Incidents'},
    color='Channel',
    color_discrete_sequence=px.colors.qualitative.Set2
)
fig_channel.update_layout(template='plotly_white', height=400)
fig_channel.write_html(IMG_DIR / "channel_distribution_incidents.html")
fig_channel.write_image(IMG_DIR / "channel_distribution_incidents.png")
print("✓ Saved: channel_distribution_incidents")

# Channel by Category
channel_cat = pd.crosstab(incidents_df['Channel'], incidents_df['Category'])
fig_channel_cat = px.imshow(
    channel_cat, text_auto=True, aspect='auto',
    title='Channel × Category Heatmap',
    color_continuous_scale='Blues'
)
fig_channel_cat.update_layout(template='plotly_white', height=500)
fig_channel_cat.write_html(IMG_DIR / "channel_category_heatmap.html")
fig_channel_cat.write_image(IMG_DIR / "channel_category_heatmap.png")
print("✓ Saved: channel_category_heatmap")

# ============================================================================
# 2. SELF-SERVICE RESOLUTION QUALITY ASSESSMENT
# ============================================================================
print("\n✅ SELF-SERVICE RESOLUTION QUALITY")
print("-" * 40)

# Define self-service vs manual channels
self_service_channels = ['Self-service', 'Virtual Agent', 'IVR', 'Chatbot']
incidents_df['Is_SelfService'] = incidents_df['Channel'].isin(self_service_channels)

# Compare outcomes by channel type
self_service = incidents_df[incidents_df['Is_SelfService'] == True]
manual_service = incidents_df[incidents_df['Is_SelfService'] == False]

print("\n  Self-Service Channel Metrics:")
print(f"    Volume: {len(self_service):,} ({len(self_service)/len(incidents_df)*100:.1f}%)")
print(f"    Mean Resolution Time: {self_service['Resolution_Hours'].mean():.1f} hours")
print(f"    Median Resolution Time: {self_service['Resolution_Hours'].median():.1f} hours")

print("\n  Manual/Other Channel Metrics:")
print(f"    Volume: {len(manual_service):,} ({len(manual_service)/len(incidents_df)*100:.1f}%)")
print(f"    Mean Resolution Time: {manual_service['Resolution_Hours'].mean():.1f} hours")
print(f"    Median Resolution Time: {manual_service['Resolution_Hours'].median():.1f} hours")

# Channel effectiveness comparison
channel_metrics = incidents_df.groupby('Channel').agg({
    'Number': 'count',
    'Resolution_Hours': ['mean', 'median'],
    'Priority': lambda x: (x == '4 - Low').mean() * 100
}).round(2)
channel_metrics.columns = ['Volume', 'Mean_Resolution', 'Median_Resolution', 'Low_Priority_Pct']
channel_metrics = channel_metrics.sort_values('Volume', ascending=False)

fig_channel_metrics = make_subplots(
    rows=1, cols=2,
    subplot_titles=['Resolution Time by Channel', 'Volume by Channel']
)

fig_channel_metrics.add_trace(
    go.Bar(x=channel_metrics.index, y=channel_metrics['Mean_Resolution'],
           marker_color='#457B9D', name='Mean Hours'),
    row=1, col=1
)

fig_channel_metrics.add_trace(
    go.Bar(x=channel_metrics.index, y=channel_metrics['Volume'],
           marker_color='#2A9D8F', name='Volume'),
    row=1, col=2
)

fig_channel_metrics.update_layout(template='plotly_white', height=400, showlegend=False)
fig_channel_metrics.write_html(IMG_DIR / "channel_effectiveness_comparison.html")
fig_channel_metrics.write_image(IMG_DIR / "channel_effectiveness_comparison.png")
print("✓ Saved: channel_effectiveness_comparison")

# ============================================================================
# 3. PASSWORD RESET INCIDENT CONCENTRATION ANALYSIS
# ============================================================================
print("\n🔐 PASSWORD RESET CONCENTRATION ANALYSIS")
print("-" * 40)

# Identify password-related incidents (keyword search)
password_keywords = ['password', 'pwd', 'reset', 'lock', 'unlock', 'credential']
incidents_df['Is_Password_Related'] = incidents_df['Short description'].str.lower().str.contains(
    '|'.join(password_keywords), na=False
)

# Also check subcategory
password_subcats = ['Password Reset', 'Password', 'pwd reset', 'Account Locked', 'Unlock Account']
incidents_df['Is_Password_Related'] = incidents_df['Is_Password_Related'] | \
    incidents_df['Subcategory'].str.lower().str.contains('|'.join(password_keywords), na=False)

password_incidents = incidents_df[incidents_df['Is_Password_Related'] == True]
non_password_incidents = incidents_df[incidents_df['Is_Password_Related'] == False]

print("\n  Password-Related Incident Summary:")
print(f"    Total Password Incidents: {len(password_incidents):,}")
print(f"    Percentage of All Incidents: {len(password_incidents)/len(incidents_df)*100:.1f}%")
print(f"    Mean Resolution Time: {password_incidents['Resolution_Hours'].mean():.1f} hours")
print(f"    Median Resolution Time: {password_incidents['Resolution_Hours'].median():.1f} hours")

# Self-service adoption for password incidents
password_self_service = password_incidents[password_incidents['Is_SelfService']].shape[0]
password_manual = password_incidents[~password_incidents['Is_SelfService']].shape[0]

print(f"\n  Password Incident Channel Split:")
print(f"    Self-Service: {password_self_service:,} ({password_self_service/len(password_incidents)*100:.1f}%)")
print(f"    Manual: {password_manual:,} ({password_manual/len(password_incidents)*100:.1f}%)")

# Visualization - Pie chart
fig_password_pie = go.Figure(go.Pie(
    labels=['Password-Related', 'Non-Password'],
    values=[len(password_incidents), len(non_password_incidents)],
    hole=0.4,
    marker_colors=['#E63946', '#457B9D'],
    textinfo='label+percent',
    title='Incident Distribution'
))
fig_password_pie.update_layout(template='plotly_white', height=400)
fig_password_pie.write_html(IMG_DIR / "password_reset_distribution.html")
fig_password_pie.write_image(IMG_DIR / "password_reset_distribution.png")
print("✓ Saved: password_reset_distribution")

# Resolution time comparison
fig_password_bar = go.Figure(go.Bar(
    x=['Password-Related', 'Non-Password'],
    y=[password_incidents['Resolution_Hours'].mean(), non_password_incidents['Resolution_Hours'].mean()],
    marker_color=['#E63946', '#457B9D'],
    text=[f"{password_incidents['Resolution_Hours'].mean():.1f}h", f"{non_password_incidents['Resolution_Hours'].mean():.1f}h"],
    name='Mean Hours'
))
fig_password_bar.update_layout(
    title='Resolution Time: Password vs Non-Password Incidents',
    template='plotly_white', height=400
)
fig_password_bar.write_html(IMG_DIR / "password_resolution_comparison.html")
fig_password_bar.write_image(IMG_DIR / "password_resolution_comparison.png")
print("✓ Saved: password_resolution_comparison")

# Top password-related subcategories
password_subcats = password_incidents['Subcategory'].value_counts().head(10)
fig_password_subcat = px.bar(
    password_subcats, x=password_subcats.values, y=password_subcats.index, orientation='h',
    title='Top Password-Related Subcategories',
    labels={'index': 'Subcategory', 'value': 'Count'},
    color_discrete_sequence=['#E63946']
)
fig_password_subcat.update_layout(template='plotly_white', height=400)
fig_password_subcat.write_html(IMG_DIR / "password_subcategories.html")
fig_password_subcat.write_image(IMG_DIR / "password_subcategories.png")
print("✓ Saved: password_subcategories")

# ============================================================================
# 4. SERVICE BRIDGE INTEGRATION EFFICIENCY
# ============================================================================
print("\n🌉 SERVICE BRIDGE INTEGRATION ANALYSIS")
print("-" * 40)

# Identify Service Bridge channel
incidents_df['Is_ServiceBridge'] = incidents_df['Channel'] == 'Service Bridge'
service_bridge = incidents_df[incidents_df['Is_ServiceBridge'] == True]
other_channels = incidents_df[incidents_df['Is_ServiceBridge'] == False]

print("\n  Service Bridge Channel Metrics:")
print(f"    Volume: {len(service_bridge):,} ({len(service_bridge)/len(incidents_df)*100:.1f}%)")
if len(service_bridge) > 0:
    print(f"    Mean Resolution Time: {service_bridge['Resolution_Hours'].mean():.1f} hours")
    print(f"    Median Resolution Time: {service_bridge['Resolution_Hours'].median():.1f} hours")
    
    # Categories handled via Service Bridge
    sb_categories = service_bridge['Category'].value_counts()
    print(f"\n  Top Categories via Service Bridge:")
    for cat, count in sb_categories.head(5).items():
        print(f"    {cat}: {count:,}")

# Compare Service Bridge vs Other
fig_sb_compare = go.Figure()
fig_sb_compare.add_trace(go.Bar(
    x=['Service Bridge', 'Other Channels'],
    y=[service_bridge['Resolution_Hours'].mean() if len(service_bridge) > 0 else 0,
       other_channels['Resolution_Hours'].mean()],
    name='Mean Resolution Hours',
    marker_color=['#F4A261', '#2A9D8F']
))
fig_sb_compare.update_layout(
    title='Resolution Time: Service Bridge vs Other Channels',
    template='plotly_white', height=400
)
fig_sb_compare.write_html(IMG_DIR / "service_bridge_comparison.html")
fig_sb_compare.write_image(IMG_DIR / "service_bridge_comparison.png")
print("✓ Saved: service_bridge_comparison")

# ============================================================================
# 5. SELF-SERVICE FUNNEL & ADOPTION TRENDS
# ============================================================================
print("\n📊 SELF-SERVICE ADOPTION FUNNEL")
print("-" * 40)

# Create funnel visualization
funnel_stages = ['Total Incidents', 'Self-Service Initiated', 'Self-Service Resolved', 'Manual Escalation Required']
funnel_values = [
    len(incidents_df),
    len(self_service),
    len(self_service[self_service['Resolution_Hours'] <= 4]),  # Quick resolution threshold
    len(manual_service)
]

fig_funnel = go.Figure(go.Funnel(
    y=funnel_stages,
    x=funnel_values,
    textinfo='value+percent previous',
    marker_color=['#457B9D', '#2A9D8F', '#28A745', '#DC3545']
))
fig_funnel.update_layout(title='Self-Service Resolution Funnel', template='plotly_white', height=400)
fig_funnel.write_html(IMG_DIR / "self_service_funnel.html")
fig_funnel.write_image(IMG_DIR / "self_service_funnel.png")
print("✓ Saved: self_service_funnel")

# Channel adoption trend over time
incidents_df['Month'] = incidents_df['Opened'].dt.to_period('M').astype(str)
channel_trend = incidents_df.groupby(['Month', 'Is_SelfService']).size().unstack(fill_value=0)
channel_trend.columns = ['Manual', 'Self-Service']
channel_trend['Self_Service_Rate'] = channel_trend['Self-Service'] / (channel_trend['Self-Service'] + channel_trend['Manual']) * 100

fig_trend = make_subplots(rows=1, cols=2,
                         subplot_titles=['Channel Volume Trend', 'Self-Service Adoption Rate'])

fig_trend.add_trace(
    go.Bar(x=channel_trend.index, y=channel_trend['Self-Service'],
           name='Self-Service', marker_color='#2A9D8F'),
    row=1, col=1
)
fig_trend.add_trace(
    go.Bar(x=channel_trend.index, y=channel_trend['Manual'],
           name='Manual', marker_color='#DC3545'),
    row=1, col=1
)

fig_trend.add_trace(
    go.Scatter(x=channel_trend.index, y=channel_trend['Self_Service_Rate'],
               name='SS Rate %', line=dict(color='#457B9D', width=3), mode='lines+markers'),
    row=1, col=2
)

fig_trend.update_layout(template='plotly_white', height=400, barmode='stack')
fig_trend.write_html(IMG_DIR / "self_service_adoption_trend.html")
fig_trend.write_image(IMG_DIR / "self_service_adoption_trend.png")
print("✓ Saved: self_service_adoption_trend")

# ============================================================================
# KEY OPTIMIZATION INSIGHTS
# ============================================================================
print("\n📊 SELF-SERVICE OPTIMIZATION KEY INSIGHTS")
print("-" * 40)

print("\n📱 CHANNEL ADOPTION:")
print(f"  Self-Service Adoption Rate: {len(self_service)/len(incidents_df)*100:.1f}%")
print(f"  Top Channel: {channel_dist.index[0]} ({channel_dist.values[0]:,} - {channel_dist.values[0]/len(incidents_df)*100:.1f}%)")

print("\n🔐 PASSWORD RESET OPPORTUNITY:")
print(f"  Password-Related Volume: {len(password_incidents):,} ({len(password_incidents)/len(incidents_df)*100:.1f}%)")
print(f"  Potential Automation Target: {password_manual:,} manual password incidents")

print("\n✅ SELF-SERVICE EFFECTIVENESS:")
ss_mean = self_service['Resolution_Hours'].mean() if len(self_service) > 0 else 0
manual_mean = manual_service['Resolution_Hours'].mean() if len(manual_service) > 0 else 0
print(f"  Self-Service Mean Resolution: {ss_mean:.1f} hours")
print(f"  Manual Mean Resolution: {manual_mean:.1f} hours")
print(f"  Efficiency Gain: {((manual_mean - ss_mean) / manual_mean * 100):.1f}%")

print("\n🌉 SERVICE BRIDGE UTILIZATION:")
print(f"  Service Bridge Volume: {len(service_bridge):,} ({len(service_bridge)/len(incidents_df)*100:.1f}%)")

print("\n✅ SELF-SERVICE OPTIMIZATION ANALYSIS COMPLETE")
print(f"✓ {len(list(IMG_DIR.glob('*self_service*')))} self-service visualizations saved")
print(f"✓ {len(list(IMG_DIR.glob('*password*')))} password analysis visualizations saved")
