from django.contrib import admin

from vbv_lernwelt.shop.models import CheckoutInformation, Product
from vbv_lernwelt.shop.services import get_transaction_state


@admin.action(description="ABACUS: Create invoices")
def generate_invoice(modeladmin, request, queryset):
    pass


@admin.action(description="DATATRANS: Sync transaction states")
def sync_transaction_state(modeladmin, request, queryset):
    for checkout in queryset:
        state = get_transaction_state(transaction_id=checkout.transaction_id)
        checkout.state = state.value
        checkout.save(
            update_fields=[
                "state",
            ]
        )


@admin.register(CheckoutInformation)
class CheckoutInformationAdmin(admin.ModelAdmin):
    list_display = (
        "product_sku",
        "user",
        "product_name",
        "product_price",
        "updated_at",
        "state",
        "invoice_transmitted_at",
    )
    search_fields = ["user__email"]
    list_filter = ("state", "product_name")
    actions = [generate_invoice, sync_transaction_state]


@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = (
        "sku",
        "name",
        "price",
        "description",
    )
