"""
Settings Models Admin
"""
from django.contrib import admin
from igny8_core.admin.base import AccountAdminMixin
from .settings_models import SystemSettings, AccountSettings, UserSettings, ModuleSettings, ModuleEnableSettings, AISettings


@admin.register(SystemSettings)
class SystemSettingsAdmin(admin.ModelAdmin):
    """SystemSettings - Global, no account filtering"""
    list_display = ['key', 'description', 'updated_at']
    search_fields = ['key', 'description']
    readonly_fields = ['created_at', 'updated_at']


@admin.register(AccountSettings)
class AccountSettingsAdmin(AccountAdminMixin, admin.ModelAdmin):
    list_display = ['account', 'key', 'is_active', 'updated_at']
    list_filter = ['is_active', 'account']
    search_fields = ['key', 'account__name']
    readonly_fields = ['created_at', 'updated_at']
    
    def get_account_display(self, obj):
        """Safely get account name"""
        try:
            account = getattr(obj, 'account', None)
            return account.name if account else '-'
        except:
            return '-'
    get_account_display.short_description = 'Account'


@admin.register(UserSettings)
class UserSettingsAdmin(admin.ModelAdmin):
    list_display = ['user', 'account', 'key', 'updated_at']
    list_filter = ['account']
    search_fields = ['key', 'user__email', 'account__name']
    readonly_fields = ['created_at', 'updated_at']
    
    def get_queryset(self, request):
        """Filter by account for non-superusers"""
        qs = super().get_queryset(request)
        if request.user.is_superuser or (hasattr(request.user, 'is_developer') and request.user.is_developer()):
            return qs
        user_account = getattr(request.user, 'account', None)
        if user_account:
            return qs.filter(account=user_account)
        return qs.none()
    
    def get_account_display(self, obj):
        """Safely get account name"""
        try:
            account = getattr(obj, 'account', None)
            return account.name if account else '-'
        except:
            return '-'
    get_account_display.short_description = 'Account'


@admin.register(ModuleSettings)
class ModuleSettingsAdmin(AccountAdminMixin, admin.ModelAdmin):
    list_display = ['account', 'module_name', 'key', 'is_active', 'updated_at']
    list_filter = ['module_name', 'is_active', 'account']
    search_fields = ['key', 'module_name', 'account__name']
    readonly_fields = ['created_at', 'updated_at']
    
    def get_account_display(self, obj):
        """Safely get account name"""
        try:
            account = getattr(obj, 'account', None)
            return account.name if account else '-'
        except:
            return '-'
    get_account_display.short_description = 'Account'


@admin.register(AISettings)
class AISettingsAdmin(AccountAdminMixin, admin.ModelAdmin):
    list_display = ['account', 'integration_type', 'is_active', 'updated_at']
    list_filter = ['integration_type', 'is_active', 'account']
    search_fields = ['integration_type', 'account__name']
    readonly_fields = ['created_at', 'updated_at']
    
    def get_account_display(self, obj):
        """Safely get account name"""
        try:
            account = getattr(obj, 'account', None)
            return account.name if account else '-'
        except:
            return '-'
    get_account_display.short_description = 'Account'


@admin.register(ModuleEnableSettings)
class ModuleEnableSettingsAdmin(AccountAdminMixin, admin.ModelAdmin):
    list_display = [
        'account',
        'planner_enabled',
        'writer_enabled',
        'thinker_enabled',
        'automation_enabled',
        'site_builder_enabled',
        'linker_enabled',
        'optimizer_enabled',
        'publisher_enabled',
    ]
    list_filter = ['planner_enabled', 'writer_enabled', 'thinker_enabled', 'automation_enabled', 'site_builder_enabled', 'linker_enabled', 'optimizer_enabled', 'publisher_enabled']
    search_fields = ['account__name']

