from django.contrib import admin

from vbv_lernwelt.shop.models import CheckoutInformation, Country, 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",
    )
    actions = [generate_invoice, sync_transaction_state]


@admin.register(Country)
class CountryAdmin(admin.ModelAdmin):
    list_display = (
        "country_id",
        "name_de",
        "name_fr",
        "name_it",
    )


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