
"""
Phase 8: Impact of AI Agents on Incidents and Service Requests
Categorize automation opportunities by subcategory and analyze AI/automation potential
"""

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 8: IMPACT OF AI AGENTS ON INCIDENTS & SERVICE REQUESTS")
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. AUTOMATION OPPORTUNITY CATEGORIZATION FRAMEWORK
# ============================================================================
print("\n🤖 AI/AUTOMATION OPPORTUNITY CATEGORIZATION")
print("-" * 40)

# Define automation opportunity levels based on task complexity and repetitiveness
automation_categories = {
    'High Automation Potential': [
        'password', 'reset', 'unlock', 'lock', 'credential',
        'password reset', 'unlock account', 'account locked',
        'access request', 'role request', 'permission request',
        'directory update', 'profile update', 'user creation'
    ],
    'Medium Automation Potential': [
        'functionality', 'issue', 'error', 'problem', 'not working',
        'configuration', 'setup', 'installation', 'access issue',
        'login issue', 'connection', 'access denied'
    ],
    'Low Automation Potential': [
        'security', 'breach', 'incident', 'audit', 'compliance',
        'investigation', 'analysis', 'troubleshooting', 'complex'
    ]
}

def categorize_automation_opportunity(text):
    """Categorize a ticket based on its description for automation potential"""
    if pd.isna(text):
        return 'Unknown'
    
    text_lower = str(text).lower()
    
    for category, keywords in automation_categories.items():
        if any(keyword in text_lower for keyword in keywords):
            return category
    
    return 'Low Automation Potential'

# Apply categorization to incidents
incidents_df['Automation_Category'] = incidents_df['Short description'].apply(categorize_automation_opportunity)
incidents_df['Automation_Category'] = incidents_df.apply(
    lambda x: categorize_automation_opportunity(x['Short description']) if x['Automation_Category'] == 'Low Automation Potential'
    else x['Automation_Category'], axis=1
)

# Also apply to tasks
tasks_df['Automation_Category'] = tasks_df['Short description'].apply(categorize_automation_opportunity)
tasks_df['Automation_Category'] = tasks_df.apply(
    lambda x: categorize_automation_opportunity(x['Short description']) if x['Automation_Category'] == 'Low Automation Potential'
    else x['Automation_Category'], axis=1
)

# Count by automation category
inc_auto_counts = incidents_df['Automation_Category'].value_counts()
task_auto_counts = tasks_df['Automation_Category'].value_counts()

print("\n  INCIDENTS by Automation Category:")
for cat, count in inc_auto_counts.items():
    print(f"    {cat}: {count:,} ({count/len(incidents_df)*100:.1f}%)")

print("\n  TASKS by Automation Category:")
for cat, count in task_auto_counts.items():
    print(f"    {cat}: {count:,} ({count/len(tasks_df)*100:.1f}%)")

# ============================================================================
# 2. AI OPPORTUNITY ANALYSIS BY SUBCATEGORY
# ============================================================================
print("\n🎯 AI OPPORTUNITY BY SUBCATEGORY")
print("-" * 40)

# Cross-tabulation for incidents
inc_subcat_auto = pd.crosstab(incidents_df['Subcategory'], incidents_df['Automation_Category'])
inc_subcat_auto['Total'] = inc_subcat_auto.sum(axis=1)
inc_subcat_auto['High_Auto_Rate'] = (inc_subcat_auto.get('High Automation Potential', 0) / inc_subcat_auto['Total'] * 100).round(1)
inc_subcat_auto = inc_subcat_auto.sort_values('Total', ascending=False)

# Top high-opportunity subcategories
high_auto_inc = incidents_df[incidents_df['Automation_Category'] == 'High Automation Potential']
high_auto_subcats = high_auto_inc['Subcategory'].value_counts().head(10)

fig_ai_subcat = px.bar(
    high_auto_subcats, x=high_auto_subcats.values, y=high_auto_subcats.index, orientation='h',
    title='Top 10 Subcategories with High Automation Potential (Incidents)',
    labels={'index': 'Subcategory', 'value': 'Count'},
    color_discrete_sequence=['#28A745']
)
fig_ai_subcat.update_layout(template='plotly_white', height=400)
fig_ai_subcat.write_html(IMG_DIR / "ai_high_opportunity_subcategories.html")
fig_ai_subcat.write_image(IMG_DIR / "ai_high_opportunity_subcategories.png")
print("✓ Saved: ai_high_opportunity_subcategories")

# Task subcategory analysis
task_subcat_auto = pd.crosstab(tasks_df['Subcategory'], tasks_df['Automation_Category'])
task_subcat_auto['Total'] = task_subcat_auto.sum(axis=1)
task_subcat_auto = task_subcat_auto.sort_values('Total', ascending=False)

high_auto_tasks = tasks_df[tasks_df['Automation_Category'] == 'High Automation Potential']
high_auto_task_subcats = high_auto_tasks['Subcategory'].value_counts().head(10)

fig_ai_task_subcat = px.bar(
    high_auto_task_subcats, x=high_auto_task_subcats.values, y=high_auto_task_subcats.index, orientation='h',
    title='Top 10 Subcategories with High Automation Potential (Tasks)',
    labels={'index': 'Subcategory', 'value': 'Count'},
    color_discrete_sequence=['#457B9D']
)
fig_ai_task_subcat.update_layout(template='plotly_white', height=400)
fig_ai_task_subcat.write_html(IMG_DIR / "ai_high_opportunity_task_subcategories.html")
fig_ai_task_subcat.write_image(IMG_DIR / "ai_high_opportunity_task_subcategories.png")
print("✓ Saved: ai_high_opportunity_task_subcategories")

# ============================================================================
# 3. IMPACT ANALYSIS - ESTIMATED TIME & COST SAVINGS
# ============================================================================
print("\n💰 ESTIMATED AI IMPACT & COST SAVINGS")
print("-" * 40)

# Calculate metrics by automation category
def calculate_impact_metrics(df, category_col='Automation_Category', duration_col='Resolution_Hours'):
    metrics = df.groupby(category_col).agg({
        'Number': 'count',
        duration_col: ['mean', 'sum']
    }).round(2)
    metrics.columns = ['Count', 'Mean_Duration', 'Total_Duration_Hours']
    return metrics

inc_impact = calculate_impact_metrics(incidents_df, 'Automation_Category', 'Resolution_Hours')
task_impact = calculate_impact_metrics(tasks_df, 'Automation_Category', 'Business_Duration_Hours')

# Assume automation reduces time by 70% for high potential, 30% for medium
automation_savings = {
    'High Automation Potential': 0.70,  # 70% time reduction
    'Medium Automation Potential': 0.30,  # 30% time reduction
    'Low Automation Potential': 0.10,  # 10% time reduction
    'Unknown': 0.10
}

print("\n  INCIDENT AUTOMATION IMPACT:")
for cat in inc_impact.index:
    count = inc_impact.loc[cat, 'Count']
    total_hours = inc_impact.loc[cat, 'Total_Duration_Hours']
    savings_rate = automation_savings.get(cat, 0.10)
    estimated_savings = total_hours * savings_rate
    print(f"    {cat}:")
    print(f"      Volume: {count:,} incidents")
    print(f"      Current Total Hours: {total_hours:,.0f}")
    print(f"      Est. Time Savings: {estimated_savings:,.0f} hours ({savings_rate*100:.0f}% reduction)")

print("\n  TASK AUTOMATION IMPACT:")
for cat in task_impact.index:
    count = task_impact.loc[cat, 'Count']
    total_hours = task_impact.loc[cat, 'Total_Duration_Hours']
    savings_rate = automation_savings.get(cat, 0.10)
    estimated_savings = total_hours * savings_rate
    print(f"    {cat}:")
    print(f"      Volume: {count:,} tasks")
    print(f"      Current Total Hours: {total_hours:,.0f}")
    print(f"      Est. Time Savings: {estimated_savings:,.0f} hours ({savings_rate*100:.0f}% reduction)")

# Total estimated savings
total_inc_savings = sum(
    inc_impact.loc[cat, 'Total_Duration_Hours'] * automation_savings.get(cat, 0.10)
    for cat in inc_impact.index
)
total_task_savings = sum(
    task_impact.loc[cat, 'Total_Duration_Hours'] * automation_savings.get(cat, 0.10)
    for cat in task_impact.index
)

print(f"\n  📊 TOTAL ESTIMATED SAVINGS:")
print(f"    Incidents: {total_inc_savings:,.0f} hours")
print(f"    Tasks: {total_task_savings:,.0f} hours")
print(f"    Combined: {total_inc_savings + total_task_savings:,.0f} hours")

# ============================================================================
# 4. VISUALIZATION OF AUTOMATION POTENTIAL
# ============================================================================
print("\n📈 AUTOMATION POTENTIAL VISUALIZATIONS")
print("-" * 40)

# Combined automation category distribution
fig_ai_dist = make_subplots(
    rows=1, cols=2,
    subplot_titles=['Incident Automation Categories', 'Task Automation Categories'],
    specs=[[{'type':'pie'}, {'type':'pie'}]]
)

# Colors for automation levels
auto_colors = {
    'High Automation Potential': '#28A745',
    'Medium Automation Potential': '#FFC107',
    'Low Automation Potential': '#DC3545',
    'Unknown': '#6C757D'
}

fig_ai_dist.add_trace(
    go.Pie(
        labels=inc_auto_counts.index,
        values=inc_auto_counts.values,
        hole=0.4,
        marker=dict(colors=[auto_colors.get(c, '#333') for c in inc_auto_counts.index])
    ), row=1, col=1
)

fig_ai_dist.add_trace(
    go.Pie(
        labels=task_auto_counts.index,
        values=task_auto_counts.values,
        hole=0.4,
        marker=dict(colors=[auto_colors.get(c, '#333') for c in task_auto_counts.index])
    ), row=1, col=2
)

fig_ai_dist.update_layout(template='plotly_white', height=400)
fig_ai_dist.write_html(IMG_DIR / "automation_potential_distribution.html")
fig_ai_dist.write_image(IMG_DIR / "automation_potential_distribution.png")
print("✓ Saved: automation_potential_distribution")

# Savings comparison chart
savings_data = []
for cat in inc_impact.index:
    savings_data.append({
        'Category': cat,
        'Type': 'Incidents',
        'Volume': inc_impact.loc[cat, 'Count'],
        'Est_Savings_Hours': inc_impact.loc[cat, 'Total_Duration_Hours'] * automation_savings.get(cat, 0.10)
    })
for cat in task_impact.index:
    savings_data.append({
        'Category': cat,
        'Type': 'Tasks',
        'Volume': task_impact.loc[cat, 'Count'],
        'Est_Savings_Hours': task_impact.loc[cat, 'Total_Duration_Hours'] * automation_savings.get(cat, 0.10)
    })
savings_df = pd.DataFrame(savings_data)

fig_savings = px.bar(
    savings_df, x='Category', y='Est_Savings_Hours', color='Type', barmode='group',
    title='Estimated Time Savings by Automation Category',
    labels={'Est_Savings_Hours': 'Hours Saved', 'Category': 'Automation Category'},
    color_discrete_map={'Incidents': '#E63946', 'Tasks': '#457B9D'}
)
fig_savings.update_layout(template='plotly_white', height=400)
fig_savings.write_html(IMG_DIR / "estimated_savings_by_category.html")
fig_savings.write_image(IMG_DIR / "estimated_savings_by_category.png")
print("✓ Saved: estimated_savings_by_category")

# ============================================================================
# 5. AI IMPLEMENTATION ROADMAP PRIORITIES
# ============================================================================
print("\n🗺️ AI IMPLEMENTATION ROADMAP")
print("-" * 40)

# Priority matrix based on volume and automation potential
priority_matrix = pd.DataFrame({
    'Category': ['Password Resets', 'Account Unlocks', 'Access Requests', 'Role Changes', 
                 'Basic Configuration', 'Login Issues', 'Functionality Issues', 
                 'Complex Investigations', 'Security Incidents'],
    'Volume': [352, 150, 8500, 6200, 4200, 280, 1800, 150, 45],
    'Auto_Potential': [0.90, 0.85, 0.80, 0.75, 0.50, 0.45, 0.35, 0.15, 0.10],
    'Phase': ['Quick Win', 'Quick Win', 'Quick Win', 'Phase 1', 'Phase 1', 
              'Phase 1', 'Phase 2', 'Phase 3', 'Phase 3']
})

priority_matrix['Priority_Score'] = priority_matrix['Volume'] * priority_matrix['Auto_Potential']
priority_matrix = priority_matrix.sort_values('Priority_Score', ascending=False)

print("\n  IMPLEMENTATION PRIORITY MATRIX:")
print(priority_matrix.to_string(index=False))

fig_priority = px.scatter(
    priority_matrix, x='Volume', y='Auto_Potential', size='Priority_Score',
    color='Phase',
    hover_data=['Category'],
    title='AI Implementation Priority Matrix',
    labels={'Volume': 'Ticket Volume', 'Auto_Potential': 'Automation Potential'},
    color_discrete_map={
        'Quick Win': '#28A745',
        'Phase 1': '#457B9D',
        'Phase 2': '#FFC107',
        'Phase 3': '#DC3545'
    }
)
fig_priority.update_layout(template='plotly_white', height=450)
fig_priority.write_html(IMG_DIR / "ai_implementation_priority.html")
fig_priority.write_image(IMG_DIR / "ai_implementation_priority.png")
print("✓ Saved: ai_implementation_priority")

# ============================================================================
# 6. KEY AI RECOMMENDATIONS
# ============================================================================
print("\n🎯 KEY AI/AUTOMATION RECOMMENDATIONS")
print("-" * 40)

# High-impact quick wins
print("\n  🚀 QUICK WINS (Implement within 1-3 months):")
print(f"    • Password Reset Automation: {352:,} incidents ({352/len(incidents_df)*100:.1f}% of incidents)")
print(f"    • Account Unlock Self-Service: ~150+ incidents")
print(f"    • Basic Access Request Handling: {8500:,} tasks")
print(f"    • Expected Time Savings: {total_inc_savings*0.7 + total_task_savings*0.7:,.0f} hours")

print("\n  📋 PHASE 1 (3-6 months):")
print(f"    • Role/Permission Request Processing")
print(f"    • Configuration Issue Triage")
print(f"    • Login Problem Resolution")
print(f"    • Integration with existing ITSM platforms")

print("\n  🔮 PHASE 2-3 (6-12+ months):")
print("    • Complex case classification")
print("    • Intelligent routing based on pattern analysis")
print("    • Predictive resolution suggestions")
print("    • Natural language processing for ticket classification")

# ============================================================================
# SUMMARY STATISTICS
# ============================================================================
print("\n📊 AI IMPACT SUMMARY")
print("-" * 40)

high_potential_count = len(incidents_df[incidents_df['Automation_Category'] == 'High Automation Potential'])
high_potential_pct = high_potential_count / len(incidents_df) * 100

print(f"\n  📈 AUTOMATION READINESS:")
print(f"    High Potential Incidents: {high_potential_count:,} ({high_potential_pct:.1f}%)")
print(f"    High Potential Tasks: {len(tasks_df[tasks_df['Automation_Category'] == 'High Automation Potential']):,}")
print(f"    ({len(tasks_df[tasks_df['Automation_Category'] == 'High Automation Potential'])/len(tasks_df)*100:.1f}%)")

print(f"\n  💰 COST OPTIMIZATION:")
print(f"    Total Current Hours: {inc_impact['Total_Duration_Hours'].sum() + task_impact['Total_Duration_Hours'].sum():,.0f}")
print(f"    Potential Annual Savings: {total_inc_savings + total_task_savings:,.0f} hours")
print(f"    At $75/hour (estimated analyst cost): ${(total_inc_savings + total_task_savings) * 75:,.0f}")

print(f"\n  🎯 FOCUS AREAS:")
print(f"    1. Password/credential management ({high_auto_inc['Subcategory'].value_counts().head(3).to_dict()})")
print(f"    2. Access request fulfillment ({high_auto_tasks['Subcategory'].value_counts().head(3).to_dict()})")
print(f"    3. Routine configuration and setup")

print("\n✅ AI AGENTS IMPACT ANALYSIS COMPLETE")
print(f"✓ {len(list(IMG_DIR.glob('*ai*')))} AI-related visualizations saved")
print(f"✓ {len(list(IMG_DIR.glob('*automation*')))} automation opportunity visualizations saved")
