from django.contrib import admin, messages
from .models import KYCRequest, KYCRequestRussia
from .temporal import KycWorkflowClient


@admin.action(description="Start KYC workflow")
def start_kyc_workflow(modeladmin, request, queryset):
    """Start KYC workflow for selected requests."""
    for kyc_request in queryset:
        workflow_id = KycWorkflowClient.start(kyc_request)
        if workflow_id:
            messages.success(request, f"Workflow started for {kyc_request.uuid}")
        else:
            messages.error(request, f"Failed to start workflow for {kyc_request.uuid}")


@admin.register(KYCRequest)
class KYCRequestAdmin(admin.ModelAdmin):
    list_display = ('uuid', 'user_id', 'team_name', 'country_code', 'workflow_status', 'contact_person', 'created_at')
    list_filter = ('workflow_status', 'country_code', 'created_at')
    search_fields = ('uuid', 'user_id', 'team_name', 'contact_email', 'contact_person')
    readonly_fields = ('uuid', 'created_at', 'updated_at', 'content_type', 'object_id')
    ordering = ('-created_at',)
    actions = [start_kyc_workflow]

    fieldsets = (
        ('Основная информация', {
            'fields': ('uuid', 'user_id', 'team_name', 'country_code', 'workflow_status')
        }),
        ('Контактная информация', {
            'fields': ('contact_person', 'contact_email', 'contact_phone')
        }),
        ('Детали страны (GenericFK)', {
            'fields': ('content_type', 'object_id'),
            'classes': ('collapse',)
        }),
        ('Статус', {
            'fields': ('score', 'approved_by', 'approved_at', 'created_at', 'updated_at')
        }),
    )


@admin.register(KYCRequestRussia)
class KYCRequestRussiaAdmin(admin.ModelAdmin):
    list_display = ('id', 'company_name', 'inn', 'ogrn')
    search_fields = ('company_name', 'inn', 'ogrn')
    ordering = ('-id',)

    fieldsets = (
        ('Компания', {
            'fields': ('company_name', 'company_full_name', 'inn', 'kpp', 'ogrn', 'address')
        }),
        ('Банковские реквизиты', {
            'fields': ('bank_name', 'bik', 'correspondent_account')
        }),
    )
