from django.contrib import admin
from .models import RecommerceIntegration, WooCommerceIntegration, IntegrationCategoryMapping, GLMIntegration, OpenRouterIntegration


@admin.register(RecommerceIntegration)
class RecommerceIntegrationAdmin(admin.ModelAdmin):
    """Админка для Recommerce интеграции"""
    list_display = ['__str__', 'is_active', 'is_configured', 'updated_at']
    list_filter = ['is_active']
    readonly_fields = ['created_at', 'updated_at']

    fieldsets = (
        ('Основное', {'fields': ('name', 'is_active')}),
        ('API настройки', {'fields': ('store_url', 'api_token')}),
        ('Синхронизация', {'fields': ('auto_sync_products', 'import_orders')}),
        ('Служебное', {'fields': ('created_at', 'updated_at'), 'classes': ('collapse',)}),
    )


@admin.register(WooCommerceIntegration)
class WooCommerceIntegrationAdmin(admin.ModelAdmin):
    """Админка для WooCommerce интеграции"""
    list_display = ['__str__', 'is_active', 'is_configured', 'updated_at']
    list_filter = ['is_active']
    readonly_fields = ['created_at', 'updated_at']

    fieldsets = (
        ('Основное', {'fields': ('name', 'is_active')}),
        ('API настройки', {'fields': ('store_url', 'consumer_key', 'consumer_secret', 'api_version')}),
        ('Синхронизация', {'fields': ('auto_sync_products', 'import_orders')}),
        ('Служебное', {'fields': ('created_at', 'updated_at'), 'classes': ('collapse',)}),
    )


@admin.register(IntegrationCategoryMapping)
class IntegrationCategoryMappingAdmin(admin.ModelAdmin):
    """Админка для маппинга категорий на внешние площадки"""
    list_display = ['category', 'integration_type', 'external_category_sku', 'external_category_name', 'updated_at']
    list_filter = ['integration_type']
    search_fields = ['category__name', 'category__sku', 'external_category_sku', 'external_category_name']
    autocomplete_fields = ['category']
    readonly_fields = ['created_at', 'updated_at']

    fieldsets = (
        ('Связь', {'fields': ('category', 'integration_type')}),
        ('Внешняя категория', {'fields': ('external_category_sku', 'external_category_name')}),
        ('Служебное', {'fields': ('created_at', 'updated_at'), 'classes': ('collapse',)}),
    )


@admin.register(GLMIntegration)
class GLMIntegrationAdmin(admin.ModelAdmin):
    """Админка для GLM интеграции"""
    list_display = ['__str__', 'is_active', 'is_configured', 'model_name', 'updated_at']
    list_filter = ['is_active', 'is_coding_endpoint']
    readonly_fields = ['created_at', 'updated_at']

    fieldsets = (
        ('Основное', {'fields': ('name', 'is_active')}),
        ('API настройки', {'fields': ('api_key', 'api_url', 'model_name', 'temperature', 'is_coding_endpoint')}),
        ('Служебное', {'fields': ('created_at', 'updated_at'), 'classes': ('collapse',)}),
    )


@admin.register(OpenRouterIntegration)
class OpenRouterIntegrationAdmin(admin.ModelAdmin):
    """Админка для OpenRouter интеграции"""
    list_display = ['__str__', 'is_active', 'is_configured', 'model_name', 'updated_at']
    list_filter = ['is_active']
    readonly_fields = ['created_at', 'updated_at']

    fieldsets = (
        ('Основное', {'fields': ('name', 'is_active')}),
        ('API настройки', {'fields': ('api_key', 'api_url', 'model_name', 'temperature', 'max_tokens')}),
        ('Служебное', {'fields': ('created_at', 'updated_at'), 'classes': ('collapse',)}),
    )
