# Generated by Django 5.2.8 on 2025-12-16 17:56

from django.db import migrations


# Default role configurations - duplicated here to avoid import issues during migrations
DEFAULT_ROLES = {
    'Full Access Staff': {
        'description': 'Complete access to all features (similar to manager)',
        'permissions': {
            'can_access_dashboard': True,
            'can_access_scheduler': True,
            'can_access_tasks': True,
            'can_access_my_schedule': True,
            'can_access_my_availability': True,
            'can_access_site_builder': True,
            'can_access_gallery': True,
            'can_access_customers': True,
            'can_access_services': True,
            'can_access_resources': True,
            'can_access_staff': True,
            'can_access_contracts': True,
            'can_access_time_blocks': True,
            'can_access_locations': True,
            'can_access_messages': True,
            'can_access_tickets': True,
            'can_access_payments': True,
            'can_access_automations': True,
            'can_delete_customers': True,
            'can_cancel_appointments': True,
            'can_delete_appointments': True,
            'can_refund_payments': True,
            'can_delete_resources': True,
            'can_delete_services': True,
            'can_invite_staff': True,
            'can_self_approve_time_off': True,
        },
    },
    'Front Desk': {
        'description': 'Access to scheduling, customers, and basic operations',
        'permissions': {
            'can_access_dashboard': True,
            'can_access_scheduler': True,
            'can_access_my_schedule': True,
            'can_access_my_availability': True,
            'can_access_customers': True,
            'can_access_tickets': True,
            'can_cancel_appointments': True,
        },
    },
    'Limited Staff': {
        'description': 'Basic access to own schedule only',
        'permissions': {
            'can_access_dashboard': True,
            'can_access_my_schedule': True,
            'can_access_my_availability': True,
        },
    },
}


def create_default_roles_for_tenants(apps, schema_editor):
    """Create default staff roles for all existing tenants"""
    Tenant = apps.get_model('core', 'Tenant')
    StaffRole = apps.get_model('users', 'StaffRole')
    User = apps.get_model('users', 'User')

    for tenant in Tenant.objects.all():
        limited_role = None

        # Create default roles for each tenant
        for role_name, role_config in DEFAULT_ROLES.items():
            role, created = StaffRole.objects.get_or_create(
                tenant=tenant,
                name=role_name,
                defaults={
                    'description': role_config['description'],
                    'permissions': role_config['permissions'],
                    'is_default': True,
                }
            )
            if role_name == 'Limited Staff':
                limited_role = role

        # Assign existing TENANT_STAFF users to 'Limited Staff' role
        if limited_role:
            User.objects.filter(
                tenant=tenant,
                role='TENANT_STAFF',
                staff_role__isnull=True
            ).update(staff_role=limited_role)


def reverse_migration(apps, schema_editor):
    """
    Reverse: Remove staff_role from users and delete default roles.
    Note: This will not delete custom roles created by users.
    """
    User = apps.get_model('users', 'User')
    StaffRole = apps.get_model('users', 'StaffRole')

    # Clear staff_role from all users
    User.objects.filter(staff_role__isnull=False).update(staff_role=None)

    # Delete default roles
    StaffRole.objects.filter(is_default=True).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('users', '0011_add_staff_role_model'),
    ]

    operations = [
        migrations.RunPython(
            create_default_roles_for_tenants,
            reverse_migration,
        ),
    ]
