from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from django.utils.safestring import mark_safe

from gaehsnitz.models import Donation, Payment, Drink, Consumption
from gaehsnitz.templatetags.money import euro

User = get_user_model()


class ConsumptionInline(admin.TabularInline):
    model = Consumption
    extra = 0


@admin.register(User)
class CustomUserAdmin(UserAdmin):
    list_display = ("username", "consumed_drinks_price")
    ordering = ("username",)
    list_filter = []
    fieldsets = (
        (
            None,
            {
                "fields": (
                    "username",
                    "password",
                )
            },
        ),
        ("BALANCE", {"fields": ("consumed_drinks_price",)}),
    )
    readonly_fields = ("consumed_drinks_price",)
    inlines = (ConsumptionInline,)

    def consumed_drinks_price(self, user: User):
        return euro(user.consumed_drinks_price)


@admin.register(Donation)
class DonationAdmin(admin.ModelAdmin):
    list_display = ("date", "amount", "note")
    ordering = ("-date",)

    @admin.display(ordering="amount")
    def amount(self, donation: Donation):
        return euro(donation.amount)


@admin.register(Payment)
class PaymentAdmin(admin.ModelAdmin):
    list_display = ("date", "purpose", "amount")
    ordering = ("-date",)

    @admin.display(ordering="amount")
    def amount(self, payment: Payment):
        return euro(payment.amount)


@admin.register(Drink)
class DrinkAdmin(admin.ModelAdmin):
    list_display = ("name", "purchase_price_per_crate", "crates_purchased", "purchase_price_total")
    fieldsets = (
        (None, {"fields": ("name",)}),
        (
            "crates",
            {
                "fields": (
                    "crates_ordered",
                    "crates_purchased",
                    "crates_returned",
                )
            },
        ),
        (
            "bottles",
            {
                "fields": (
                    "bottles_per_crate",
                    "bottles_total",
                    "bottles_returned",
                )
            },
        ),
        (
            "amount",
            {
                "fields": (
                    "bottle_size",
                    "amount_per_crate",
                    "amount_total",
                )
            },
        ),
        (
            "purchase",
            {
                "fields": (
                    "purchase_price_per_crate",
                    "purchase_price_per_bottle",
                    "purchase_price_total",
                )
            },
        ),
        (
            "deposit",
            {
                "fields": (
                    "deposit_per_crate",
                    "deposit_total",
                    "deposit_refund",
                    "deposit_kept",
                )
            },
        ),
        (
            "sales",
            {
                "fields": (
                    "sale_price_per_bottle",
                    "bottles_sold",
                    "sales_purchase_value",
                    "sale_price_total",
                    "bottles_given_away",
                    "giveaway_purchase_value",
                    "balance",
                )
            },
        ),
    )
    readonly_fields = (
        "bottles_total",
        "bottles_returned",
        "amount_per_crate",
        "amount_total",
        "purchase_price_per_bottle",
        "purchase_price_total",
        "deposit_total",
        "deposit_refund",
        "deposit_kept",
        "bottles_sold",
        "sales_purchase_value",
        "sale_price_total",
        "bottles_given_away",
        "giveaway_purchase_value",
        "balance",
    )

    def purchase_price_per_bottle(self, drink: Drink):
        return euro(drink.purchase_price_per_bottle)

    def purchase_price_total(self, drink: Drink):
        return euro(drink.purchase_price_total)

    def deposit_total(self, drink: Drink):
        return euro(drink.deposit_total)

    def deposit_refund(self, drink: Drink):
        return euro(drink.deposit_refund)

    def deposit_kept(self, drink: Drink):
        return euro(drink.deposit_kept)

    def sales_purchase_value(self, drink: Drink):
        return euro(drink.sales_purchase_value)

    def sale_price_total(self, drink: Drink):
        return euro(drink.sale_price_total)

    def giveaway_purchase_value(self, drink: Drink):
        return euro(drink.giveaway_purchase_value)

    def balance(self, drink: Drink):
        return mark_safe(f"<b>{euro(drink.balance)}</b>")
