# -*- coding: utf-8 -*-
"""
Adtlas Accounts App Configuration

This module configures the accounts app for the Adtlas DAI Management System.
It handles user authentication, role-based permissions, and user management.

Features:
    - Custom user model with email-based authentication
    - Dynamic role and permission system
    - User profile management
    - Activity tracking and audit logging
    - Session management and security

Author: Adtlas Development Team
Version: 2.0.0
Last Updated: 2025-07-08
"""

from django.apps import AppConfig


class AccountsConfig(AppConfig):
    """
    Configuration class for the accounts app.
    
    This class defines the configuration for the accounts application,
    including the app name, default auto field type, and any signals
    or initialization code.
    """
    
    default_auto_field = "django.db.models.BigAutoField"
    name = "apps.accounts"
    verbose_name = "User Accounts & Authentication"
    
    def ready(self):
        """
        Initialize the app when Django starts.
        
        This method is called when the app is ready and can be used
        to register signals, perform initialization, or set up
        any app-specific configurations.
        """
        # Import signals to register them
        try:
            from . import signals
        except ImportError:
            pass
        
        # Create default roles and permissions
        self.create_default_roles()
    
    def create_default_roles(self):
        """
        Create default system roles if they don"t exist.
        
        This method creates the basic role structure needed for
        the application to function properly.
        """
        try:
            from django.contrib.auth.models import Permission
            from django.contrib.contenttypes.models import ContentType
            from .models import Role
            
            # Define default roles
            default_roles = [
                {
                    "name": "Super Administrator",
                    "code": "super_admin",
                    "description": "Full system access with all permissions",
                    "role_type": "system",
                    "level": 1,
                    "is_active": True,
                    "is_default": False
                },
                {
                    "name": "Administrator",
                    "code": "admin",
                    "description": "Administrative access to most features",
                    "role_type": "system",
                    "level": 2,
                    "is_active": True,
                    "is_default": False
                },
                {
                    "name": "Manager",
                    "code": "manager",
                    "description": "Management access to campaigns and analytics",
                    "role_type": "system",
                    "level": 3,
                    "is_active": True,
                    "is_default": False
                },
                {
                    "name": "Operator",
                    "code": "operator",
                    "description": "Operational access to create and manage content",
                    "role_type": "system",
                    "level": 4,
                    "is_active": True,
                    "is_default": False
                },
                {
                    "name": "Viewer",
                    "code": "viewer",
                    "description": "Read-only access to view data and reports",
                    "role_type": "system",
                    "level": 5,
                    "is_active": True,
                    "is_default": True
                }
            ]
            
            # Create roles if they don"t exist
            for role_data in default_roles:
                role, created = Role.objects.get_or_create(
                    code=role_data["code"],
                    defaults=role_data
                )
                
                if created:
                    print(f"Created default role: {role.name}")
        
        except Exception as e:
            # Silently fail during migrations or if models aren"t ready
            pass
