"""
Admin interface for auth models
"""
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from igny8_core.admin.base import AccountAdminMixin
from .models import User, Account, Plan, Subscription, Site, Sector, SiteUserAccess, Industry, IndustrySector, SeedKeyword, PasswordResetToken


@admin.register(Plan)
class PlanAdmin(admin.ModelAdmin):
    """Plan admin - Global, no account filtering needed"""
    list_display = ['name', 'slug', 'price', 'billing_cycle', 'max_sites', 'max_users', 'included_credits', 'is_active']
    list_filter = ['is_active', 'billing_cycle']
    search_fields = ['name', 'slug']
    readonly_fields = ['created_at']
    
    fieldsets = (
        ('Plan Info', {
            'fields': ('name', 'slug', 'price', 'billing_cycle', 'features', 'is_active')
        }),
        ('User / Site Limits', {
            'fields': ('max_users', 'max_sites', 'max_industries', 'max_author_profiles')
        }),
        ('Planner Limits', {
            'fields': ('max_keywords', 'max_clusters', 'daily_cluster_limit', 'daily_keyword_import_limit', 'monthly_cluster_ai_credits')
        }),
        ('Writer Limits', {
            'fields': ('daily_content_tasks', 'daily_ai_requests', 'monthly_word_count_limit', 'monthly_content_ai_credits')
        }),
        ('Image Limits', {
            'fields': ('monthly_image_count', 'monthly_image_ai_credits', 'max_images_per_task', 'image_model_choices')
        }),
        ('AI Controls', {
            'fields': ('daily_ai_request_limit', 'monthly_ai_credit_limit')
        }),
        ('Billing & Credits', {
            'fields': ('included_credits', 'extra_credit_price', 'allow_credit_topup', 'auto_credit_topup_threshold', 'auto_credit_topup_amount', 'credits_per_month')
        }),
        ('Stripe Integration', {
            'fields': ('stripe_product_id', 'stripe_price_id')
        }),
    )


@admin.register(Account)
class AccountAdmin(AccountAdminMixin, admin.ModelAdmin):
    list_display = ['name', 'slug', 'owner', 'plan', 'status', 'credits', 'created_at']
    list_filter = ['status', 'plan']
    search_fields = ['name', 'slug']
    readonly_fields = ['created_at', 'updated_at']
    
    def get_queryset(self, request):
        """Override to 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
        # Owners can see their own accounts
        if hasattr(request.user, 'role') and request.user.role == 'owner':
            return qs.filter(owner=request.user)
        # Admins can see their account
        try:
            user_account = getattr(request.user, 'account', None)
            if user_account:
                return qs.filter(id=user_account.id)
        except (AttributeError, Exception):
            # If account access fails (e.g., column mismatch), return empty
            pass
        return qs.none()


@admin.register(Subscription)
class SubscriptionAdmin(AccountAdminMixin, admin.ModelAdmin):
    list_display = ['account', 'status', 'current_period_start', 'current_period_end']
    list_filter = ['status']
    search_fields = ['account__name', 'stripe_subscription_id']
    readonly_fields = ['created_at', 'updated_at']


@admin.register(PasswordResetToken)
class PasswordResetTokenAdmin(admin.ModelAdmin):
    list_display = ['user', 'token', 'used', 'expires_at', 'created_at']
    list_filter = ['used', 'expires_at', 'created_at']
    search_fields = ['user__email', 'token']
    readonly_fields = ['created_at', 'token']
    
    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(user__account=user_account)
        return qs.none()


class SectorInline(admin.TabularInline):
    """Inline admin for sectors within Site admin."""
    model = Sector
    extra = 0
    fields = ['industry_sector', 'name', 'slug', 'status', 'is_active', 'get_keywords_count', 'get_clusters_count']
    readonly_fields = ['get_keywords_count', 'get_clusters_count']
    
    def get_keywords_count(self, obj):
        if obj.pk:
            return getattr(obj, 'keywords_set', obj.keywords_set).count()
        return 0
    get_keywords_count.short_description = 'Keywords'
    
    def get_clusters_count(self, obj):
        if obj.pk:
            return getattr(obj, 'clusters_set', obj.clusters_set).count()
        return 0
    get_clusters_count.short_description = 'Clusters'


@admin.register(Site)
class SiteAdmin(AccountAdminMixin, admin.ModelAdmin):
    list_display = ['name', 'slug', 'account', 'industry', 'domain', 'status', 'is_active', 'get_sectors_count']
    list_filter = ['status', 'is_active', 'account', 'industry']
    search_fields = ['name', 'slug', 'domain', 'industry__name']
    readonly_fields = ['created_at', 'updated_at']
    inlines = [SectorInline]
    
    def get_sectors_count(self, obj):
        try:
            return obj.get_active_sectors_count()
        except:
            return 0
    get_sectors_count.short_description = 'Active Sectors'
    
    def get_industry_display(self, obj):
        """Safely get industry name"""
        try:
            return obj.industry.name if obj.industry else '-'
        except:
            return '-'
    get_industry_display.short_description = 'Industry'


@admin.register(Sector)
class SectorAdmin(AccountAdminMixin, admin.ModelAdmin):
    list_display = ['name', 'slug', 'site', 'industry_sector', 'get_industry', 'status', 'is_active', 'get_keywords_count', 'get_clusters_count']
    list_filter = ['status', 'is_active', 'site', 'industry_sector__industry']
    search_fields = ['name', 'slug', 'site__name', 'industry_sector__name']
    readonly_fields = ['created_at', 'updated_at']
    
    def get_industry(self, obj):
        """Safely get industry name"""
        try:
            if obj.industry_sector and obj.industry_sector.industry:
                return obj.industry_sector.industry.name
        except:
            pass
        return '-'
    get_industry.short_description = 'Industry'
    
    def get_keywords_count(self, obj):
        """Safely get keywords count"""
        try:
            if obj.pk:
                return getattr(obj, 'keywords_set', obj.keywords_set).count()
        except:
            pass
        return 0
    get_keywords_count.short_description = 'Keywords'
    
    def get_clusters_count(self, obj):
        """Safely get clusters count"""
        try:
            if obj.pk:
                return getattr(obj, 'clusters_set', obj.clusters_set).count()
        except:
            pass
        return 0
    get_clusters_count.short_description = 'Clusters'


@admin.register(SiteUserAccess)
class SiteUserAccessAdmin(admin.ModelAdmin):
    list_display = ['user', 'site', 'granted_at', 'granted_by']
    list_filter = ['granted_at']
    search_fields = ['user__email', 'site__name']
    readonly_fields = ['granted_at']


class IndustrySectorInline(admin.TabularInline):
    """Inline admin for industry sectors within Industry admin."""
    model = IndustrySector
    extra = 0
    fields = ['name', 'slug', 'description', 'is_active']
    readonly_fields = []


@admin.register(Industry)
class IndustryAdmin(admin.ModelAdmin):
    list_display = ['name', 'slug', 'is_active', 'get_sectors_count', 'created_at']
    list_filter = ['is_active']
    search_fields = ['name', 'slug', 'description']
    readonly_fields = ['created_at', 'updated_at']
    inlines = [IndustrySectorInline]
    
    def get_sectors_count(self, obj):
        return obj.sectors.filter(is_active=True).count()
    get_sectors_count.short_description = 'Active Sectors'


@admin.register(IndustrySector)
class IndustrySectorAdmin(admin.ModelAdmin):
    list_display = ['name', 'slug', 'industry', 'is_active']
    list_filter = ['is_active', 'industry']
    search_fields = ['name', 'slug', 'description']
    readonly_fields = ['created_at', 'updated_at']


@admin.register(SeedKeyword)
class SeedKeywordAdmin(admin.ModelAdmin):
    """SeedKeyword admin - Global reference data, no account filtering"""
    list_display = ['keyword', 'industry', 'sector', 'volume', 'difficulty', 'intent', 'is_active', 'created_at']
    list_filter = ['is_active', 'industry', 'sector', 'intent']
    search_fields = ['keyword']
    readonly_fields = ['created_at', 'updated_at']
    
    fieldsets = (
        ('Keyword Info', {
            'fields': ('keyword', 'industry', 'sector', 'is_active')
        }),
        ('SEO Metrics', {
            'fields': ('volume', 'difficulty', 'intent')
        }),
        ('Timestamps', {
            'fields': ('created_at', 'updated_at')
        }),
    )


@admin.register(User)
class UserAdmin(BaseUserAdmin):
    list_display = ['email', 'username', 'account', 'role', 'is_active', 'is_staff', 'created_at']
    list_filter = ['role', 'account', 'is_active', 'is_staff']
    search_fields = ['email', 'username']
    readonly_fields = ['created_at', 'updated_at']
    
    fieldsets = BaseUserAdmin.fieldsets + (
        ('IGNY8 Info', {'fields': ('account', 'role')}),
        ('Timestamps', {'fields': ('created_at', 'updated_at')}),
    )
    
    add_fieldsets = BaseUserAdmin.add_fieldsets + (
        ('IGNY8 Info', {'fields': ('account', 'role')}),
    )
    
    def get_queryset(self, request):
        """Filter users 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'

