# apps/core/admin.py
from apps.core.utils.get_true_host import get_true_host
from django.contrib import admin

from ..models.api_operation import API_operation
from ..models.security_code import SecurityCode


@admin.register(API_operation)
class API_operationAdmin(admin.ModelAdmin):
    # 隐藏默认操作
    actions = None

    def has_add_permission(self, request):
        return False

    def has_change_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

    def get_queryset(self, request):
        return API_operation.objects.none()  # 返回空查询集

    def changelist_view(self, request, extra_context=None):
        extra_context = extra_context or {}
        code_config = SecurityCode.get_solo()
        protocol = 'https' if request.is_secure() else 'http'
        extra_context.update({
            'code': getattr(code_config, 'code', ''),
            'full_host': get_true_host(request)
        })
        return super().changelist_view(request, extra_context)

    # 自定义视图
    # def get_urls(self):
    #     urls = super().get_urls()
    #     custom_urls = [
    #         path('run-task/', self.run_ai_task, name='ai_control_task')
    #     ]
    #     return custom_urls + urls

    # # 按钮操作处理方法
    # def run_ai_task(self, request):
    #     if request.method == 'POST':
    #         task_type = request.POST.get('task_type')
    #         try:
    #             result = AIService.execute_task(task_type, {})
    #             self.message_user(request, f"{task_type} 执行成功", level=messages.SUCCESS)
    #         except Exception as e:
    #             self.message_user(request, f"操作失败: {str(e)}", level=messages.ERROR)
    #     return JsonResponse({'status': 'ok'})
