# Generated migration to add features matching tenant model field names

from django.db import migrations


def add_tenant_features(apps, schema_editor):
    """Add Feature records matching tenant model field names.

    This ensures has_feature() calls work correctly when we remove tenant fields.
    The billing system uses different naming (e.g., 'sms_enabled'), but the tenant
    model uses 'can_use_sms_reminders'. We need both to coexist during migration,
    then the can_ prefixed versions become authoritative.
    """
    Feature = apps.get_model("billing", "Feature")

    # Features that match tenant model field names exactly
    # These are the codes used in has_feature() calls throughout the codebase
    features = [
        # Boolean features - Platform permissions
        {
            "code": "can_manage_oauth_credentials",
            "name": "OAuth Credential Management",
            "description": "Manage OAuth credentials for external integrations",
            "feature_type": "boolean",
        },
        {
            "code": "can_accept_payments",
            "name": "Accept Online Payments",
            "description": "Accept online payments via Stripe Connect",
            "feature_type": "boolean",
        },
        {
            "code": "can_use_custom_domain",
            "name": "Custom Domain",
            "description": "Configure a custom domain for booking pages",
            "feature_type": "boolean",
        },
        {
            "code": "can_white_label",
            "name": "White Label",
            "description": "Remove SmoothSchedule branding",
            "feature_type": "boolean",
        },
        {
            "code": "can_api_access",
            "name": "API Access",
            "description": "Access the REST API for integrations",
            "feature_type": "boolean",
        },
        # Boolean features - Capabilities
        {
            "code": "can_add_video_conferencing",
            "name": "Video Conferencing",
            "description": "Add video conferencing links to events",
            "feature_type": "boolean",
        },
        {
            "code": "can_connect_to_api",
            "name": "External API Connections",
            "description": "Connect to external APIs",
            "feature_type": "boolean",
        },
        {
            "code": "can_book_repeated_events",
            "name": "Recurring Events",
            "description": "Book repeated/recurring events",
            "feature_type": "boolean",
        },
        {
            "code": "can_require_2fa",
            "name": "Require 2FA",
            "description": "Require two-factor authentication for users",
            "feature_type": "boolean",
        },
        {
            "code": "can_download_logs",
            "name": "Download Logs",
            "description": "Download system logs for auditing",
            "feature_type": "boolean",
        },
        {
            "code": "can_delete_data",
            "name": "Data Deletion",
            "description": "Permanently delete data",
            "feature_type": "boolean",
        },
        {
            "code": "can_use_sms_reminders",
            "name": "SMS Reminders",
            "description": "Send SMS reminders to customers and staff",
            "feature_type": "boolean",
        },
        {
            "code": "can_use_masked_phone_numbers",
            "name": "Masked Phone Numbers",
            "description": "Use masked phone numbers for privacy",
            "feature_type": "boolean",
        },
        {
            "code": "can_use_pos",
            "name": "Point of Sale",
            "description": "Use the Point of Sale (POS) system",
            "feature_type": "boolean",
        },
        {
            "code": "can_use_mobile_app",
            "name": "Mobile App",
            "description": "Access the mobile app for field staff",
            "feature_type": "boolean",
        },
        {
            "code": "can_export_data",
            "name": "Data Export",
            "description": "Export appointments, customers, and other data",
            "feature_type": "boolean",
        },
        {
            "code": "can_create_plugins",
            "name": "Create Plugins",
            "description": "Create custom automation plugins",
            "feature_type": "boolean",
        },
        {
            "code": "can_use_webhooks",
            "name": "Webhooks",
            "description": "Use webhooks for integrations",
            "feature_type": "boolean",
        },
        {
            "code": "can_use_calendar_sync",
            "name": "Calendar Sync",
            "description": "Sync with Google Calendar and other providers",
            "feature_type": "boolean",
        },
        {
            "code": "can_use_contracts",
            "name": "Contracts & E-Signatures",
            "description": "Create and manage e-signature contracts",
            "feature_type": "boolean",
        },
        {
            "code": "can_customize_booking_page",
            "name": "Booking Page Customization",
            "description": "Customize the public booking page and site builder",
            "feature_type": "boolean",
        },
        # Integer features - Limits
        {
            "code": "max_event_types",
            "name": "Maximum Event Types",
            "description": "Maximum number of event/service types. 0 = unlimited",
            "feature_type": "integer",
        },
        {
            "code": "max_calendars_connected",
            "name": "Maximum Connected Calendars",
            "description": "Maximum external calendars connected. 0 = unlimited",
            "feature_type": "integer",
        },
        {
            "code": "max_pages",
            "name": "Maximum Pages",
            "description": "Maximum number of site builder pages. 0 = unlimited",
            "feature_type": "integer",
        },
    ]

    for feature_data in features:
        Feature.objects.update_or_create(
            code=feature_data["code"],
            defaults={
                "name": feature_data["name"],
                "description": feature_data["description"],
                "feature_type": feature_data["feature_type"],
            },
        )


def reverse_add_tenant_features(apps, schema_editor):
    """Remove the tenant-named features."""
    Feature = apps.get_model("billing", "Feature")
    Feature.objects.filter(
        code__in=[
            "can_manage_oauth_credentials",
            "can_accept_payments",
            "can_use_custom_domain",
            "can_white_label",
            "can_api_access",
            "can_add_video_conferencing",
            "can_connect_to_api",
            "can_book_repeated_events",
            "can_require_2fa",
            "can_download_logs",
            "can_delete_data",
            "can_use_sms_reminders",
            "can_use_masked_phone_numbers",
            "can_use_pos",
            "can_use_mobile_app",
            "can_export_data",
            "can_create_plugins",
            "can_use_webhooks",
            "can_use_calendar_sync",
            "can_use_contracts",
            "can_customize_booking_page",
            "max_event_types",
            "max_calendars_connected",
            "max_pages",
        ]
    ).delete()


class Migration(migrations.Migration):

    dependencies = [
        ("billing", "0010_add_stackable_addons"),
    ]

    operations = [
        migrations.RunPython(add_tenant_features, reverse_add_tenant_features),
    ]
