#!/usr/bin/env python3
"""
Complete MSP Pricing Calculator
Arizona Computer Guru - GPS + Web/Email Hosting
"""

# ============================================================================
# GPS ENDPOINT MONITORING
# ============================================================================

GPS_TIERS = {
    'basic': {
        'name': 'GPS-BASIC: Essential Protection',
        'price_per_endpoint': 19,
    },
    'pro': {
        'name': 'GPS-PRO: Business Protection (MOST POPULAR)',
        'price_per_endpoint': 26,
    },
    'advanced': {
        'name': 'GPS-ADVANCED: Maximum Protection',
        'price_per_endpoint': 39,
    }
}

EQUIPMENT_PACK = {
    'base_price': 25,
    'base_devices': 10,
    'additional_device_price': 3
}

SUPPORT_PLANS = {
    'essential': {'name': 'Essential Support', 'price': 200, 'hours': 2},
    'standard': {'name': 'Standard Support (MOST POPULAR)', 'price': 380, 'hours': 4},
    'premium': {'name': 'Premium Support', 'price': 540, 'hours': 6},
    'priority': {'name': 'Priority Support', 'price': 850, 'hours': 10}
}

# ============================================================================
# WEB HOSTING
# ============================================================================

WEB_HOSTING = {
    'starter': {
        'name': 'Starter Hosting',
        'price': 15,
        'storage_gb': 5,
        'websites': 1
    },
    'business': {
        'name': 'Business Hosting (MOST POPULAR)',
        'price': 35,
        'storage_gb': 25,
        'websites': 5
    },
    'commerce': {
        'name': 'Commerce Hosting',
        'price': 65,
        'storage_gb': 50,
        'websites': 'unlimited'
    }
}

# ============================================================================
# EMAIL HOSTING
# ============================================================================

WHM_EMAIL = {
    'base_price_per_mailbox': 2,
    'included_storage_gb': 5,
    'storage_block_price': 2,  # Per 5GB block
    'storage_block_size_gb': 5
}

M365_PLANS = {
    'basic': {
        'name': 'M365 Business Basic',
        'price_per_user': 7,
        'storage_gb': 50
    },
    'standard': {
        'name': 'M365 Business Standard (MOST POPULAR)',
        'price_per_user': 14,
        'storage_gb': 50
    },
    'premium': {
        'name': 'M365 Business Premium',
        'price_per_user': 24,
        'storage_gb': 50
    },
    'exchange': {
        'name': 'Exchange Online Plan 1',
        'price_per_user': 5,
        'storage_gb': 50
    }
}

EMAIL_SECURITY_ADDON = {
    'price_per_mailbox': 3,
    'name': 'Email Security & Filtering (MailProtector/INKY)'
}

# ============================================================================
# ADD-ON SERVICES
# ============================================================================

ADDONS = {
    'dedicated_ip': {'name': 'Dedicated IP', 'price': 5},
    'premium_ssl': {'name': 'SSL Certificate (Premium)', 'price': 6.25},  # $75/year / 12
    'offsite_backup': {'name': 'Daily Offsite Backup', 'price': 10},
    'web_storage_10gb': {'name': 'Additional Web Storage (10GB)', 'price': 5}
}

# ============================================================================
# CALCULATOR FUNCTIONS
# ============================================================================

def calculate_whm_email(mailboxes, storage_gb_per_mailbox=5, include_security=False):
    """
    Calculate WHM email hosting costs

    Args:
        mailboxes: Number of mailboxes
        storage_gb_per_mailbox: Storage per mailbox in GB
        include_security: Add email security filtering
    """
    base_cost = mailboxes * WHM_EMAIL['base_price_per_mailbox']

    # Calculate storage blocks needed
    if storage_gb_per_mailbox > WHM_EMAIL['included_storage_gb']:
        additional_gb = storage_gb_per_mailbox - WHM_EMAIL['included_storage_gb']
        blocks_needed = -(-additional_gb // WHM_EMAIL['storage_block_size_gb'])  # Ceiling division
        storage_cost = mailboxes * blocks_needed * WHM_EMAIL['storage_block_price']
    else:
        blocks_needed = 0
        storage_cost = 0

    total_mailbox_cost = base_cost + storage_cost

    # Email security
    security_cost = mailboxes * EMAIL_SECURITY_ADDON['price_per_mailbox'] if include_security else 0

    total_cost = total_mailbox_cost + security_cost

    return {
        'mailboxes': mailboxes,
        'storage_per_mailbox_gb': storage_gb_per_mailbox,
        'base_cost': base_cost,
        'storage_cost': storage_cost,
        'security_cost': security_cost,
        'total_cost': total_cost,
        'cost_per_mailbox': total_cost / mailboxes if mailboxes > 0 else 0
    }


def calculate_m365_email(users, plan='standard'):
    """Calculate Microsoft 365 email costs"""
    plan_data = M365_PLANS.get(plan, M365_PLANS['standard'])

    return {
        'users': users,
        'plan': plan_data['name'],
        'price_per_user': plan_data['price_per_user'],
        'total_cost': users * plan_data['price_per_user'],
        'storage_per_user_gb': plan_data['storage_gb']
    }


def calculate_web_hosting(tier='business', extra_storage_gb=0):
    """Calculate web hosting costs"""
    tier_data = WEB_HOSTING.get(tier, WEB_HOSTING['business'])

    # Extra storage in 10GB increments
    extra_storage_cost = 0
    if extra_storage_gb > 0:
        blocks = -(-extra_storage_gb // 10)  # Ceiling division
        extra_storage_cost = blocks * ADDONS['web_storage_10gb']['price']

    return {
        'tier': tier_data['name'],
        'base_cost': tier_data['price'],
        'extra_storage_gb': extra_storage_gb,
        'extra_storage_cost': extra_storage_cost,
        'total_cost': tier_data['price'] + extra_storage_cost
    }


def calculate_complete_quote(
    # GPS
    gps_endpoints=0,
    gps_tier='pro',
    equipment_devices=0,
    support_plan=None,

    # Web Hosting
    web_hosting_tier=None,
    web_extra_storage_gb=0,

    # Email
    email_type=None,  # 'whm' or 'm365'
    email_users=0,
    whm_storage_gb=5,
    whm_security=False,
    m365_plan='standard',

    # Add-ons
    dedicated_ip=False,
    premium_ssl=False,
    offsite_backup=False
):
    """
    Calculate complete quote including GPS, web hosting, and email
    """
    result = {
        'gps': None,
        'web': None,
        'email': None,
        'addons': [],
        'totals': {}
    }

    monthly_total = 0

    # GPS Monitoring
    if gps_endpoints > 0:
        from gps_calculator import calculate_gps_quote
        gps_quote = calculate_gps_quote(
            endpoints=gps_endpoints,
            tier=gps_tier,
            equipment_devices=equipment_devices,
            support_plan=support_plan
        )
        result['gps'] = gps_quote
        monthly_total += gps_quote['totals']['monthly']

    # Web Hosting
    if web_hosting_tier:
        web_quote = calculate_web_hosting(web_hosting_tier, web_extra_storage_gb)
        result['web'] = web_quote
        monthly_total += web_quote['total_cost']

    # Email Hosting
    if email_type == 'whm' and email_users > 0:
        email_quote = calculate_whm_email(email_users, whm_storage_gb, whm_security)
        result['email'] = {'type': 'WHM Email', 'details': email_quote}
        monthly_total += email_quote['total_cost']
    elif email_type == 'm365' and email_users > 0:
        email_quote = calculate_m365_email(email_users, m365_plan)
        result['email'] = {'type': 'Microsoft 365', 'details': email_quote}
        monthly_total += email_quote['total_cost']

    # Add-ons
    addon_cost = 0
    if dedicated_ip:
        result['addons'].append(ADDONS['dedicated_ip'])
        addon_cost += ADDONS['dedicated_ip']['price']
    if premium_ssl:
        result['addons'].append(ADDONS['premium_ssl'])
        addon_cost += ADDONS['premium_ssl']['price']
    if offsite_backup:
        result['addons'].append(ADDONS['offsite_backup'])
        addon_cost += ADDONS['offsite_backup']['price']

    monthly_total += addon_cost

    # Totals
    result['totals'] = {
        'monthly': monthly_total,
        'annual': monthly_total * 12,
        'addon_cost': addon_cost
    }

    return result


def print_complete_quote(quote):
    """Print formatted complete quote"""
    print("\n" + "="*70)
    print("COMPLETE MSP PRICING QUOTE - ARIZONA COMPUTER GURU")
    print("="*70)

    # GPS Section
    if quote['gps']:
        print("\n[GPS ENDPOINT MONITORING & SUPPORT]")
        gps = quote['gps']
        print(f"  {gps['gps']['tier']}")
        print(f"  {gps['gps']['endpoints']} endpoints × ${gps['gps']['price_per_endpoint']} = ${gps['gps']['monthly_cost']}")

        if gps['equipment']['devices'] > 0:
            print(f"  Equipment Pack: {gps['equipment']['devices']} devices = ${gps['equipment']['monthly_cost']}")

        if gps['support']['monthly_cost'] > 0:
            print(f"  {gps['support']['plan']}: ${gps['support']['monthly_cost']} ({gps['support']['hours_included']} hrs)")

    # Web Hosting Section
    if quote['web']:
        print("\n[WEB HOSTING]")
        web = quote['web']
        print(f"  {web['tier']}: ${web['base_cost']}")
        if web['extra_storage_gb'] > 0:
            print(f"  Extra Storage ({web['extra_storage_gb']}GB): ${web['extra_storage_cost']}")

    # Email Section
    if quote['email']:
        print("\n[EMAIL HOSTING]")
        email = quote['email']
        print(f"  {email['type']}")

        if email['type'] == 'WHM Email':
            details = email['details']
            print(f"  {details['mailboxes']} mailboxes × {details['storage_per_mailbox_gb']}GB")
            print(f"  Base: ${details['base_cost']}")
            if details['storage_cost'] > 0:
                print(f"  Additional Storage: ${details['storage_cost']}")
            if details['security_cost'] > 0:
                print(f"  Security Add-on: ${details['security_cost']}")
        else:  # M365
            details = email['details']
            print(f"  {details['plan']}")
            print(f"  {details['users']} users × ${details['price_per_user']} = ${details['total_cost']}")

    # Add-ons
    if quote['addons']:
        print("\n[ADD-ON SERVICES]")
        for addon in quote['addons']:
            print(f"  {addon['name']}: ${addon['price']}")

    # Totals
    print("\n" + "-"*70)
    print(f"MONTHLY TOTAL: ${quote['totals']['monthly']}")
    print(f"ANNUAL TOTAL: ${quote['totals']['annual']}")
    print("="*70 + "\n")


# ============================================================================
# EXAMPLE USAGE
# ============================================================================

if __name__ == "__main__":
    print("\nCOMPLETE MSP PRICING CALCULATOR")
    print("Arizona Computer Guru")
    print("="*70)

    # Example 1: Small Office - GPS + Web + WHM Email
    print("\n\nExample 1: Small Office")
    print("10 GPS endpoints + Website + 5 WHM email users")
    quote1 = calculate_complete_quote(
        gps_endpoints=10,
        gps_tier='pro',
        support_plan='standard',
        web_hosting_tier='business',
        email_type='whm',
        email_users=5,
        whm_storage_gb=10,
        whm_security=True
    )
    print_complete_quote(quote1)

    # Example 2: Modern Business - GPS + Web + M365
    print("\n\nExample 2: Modern Business")
    print("22 GPS endpoints + Website + 15 M365 users")
    quote2 = calculate_complete_quote(
        gps_endpoints=22,
        gps_tier='pro',
        support_plan='premium',
        web_hosting_tier='business',
        email_type='m365',
        email_users=15,
        m365_plan='standard'
    )
    print_complete_quote(quote2)

    # Example 3: E-Commerce Business
    print("\n\nExample 3: E-Commerce Business")
    print("42 GPS endpoints + Commerce hosting + 20 M365 users + Dedicated IP")
    quote3 = calculate_complete_quote(
        gps_endpoints=42,
        gps_tier='pro',
        support_plan='priority',
        web_hosting_tier='commerce',
        email_type='m365',
        email_users=20,
        m365_plan='standard',
        dedicated_ip=True,
        premium_ssl=True
    )
    print_complete_quote(quote3)

    # Example 4: Web + Email Only (No GPS)
    print("\n\nExample 4: Web & Email Only")
    print("Small business - Website + 8 WHM email users")
    quote4 = calculate_complete_quote(
        web_hosting_tier='business',
        email_type='whm',
        email_users=8,
        whm_storage_gb=10,
        whm_security=True
    )
    print_complete_quote(quote4)
