from django import forms
from django.contrib import admin

from .models import LoginToken, Token, User


class UserAdmin(admin.ModelAdmin):
    list_display = ["email"]
    search_fields = ["email"]
    filter_horizontal = ["unlocked_decks", "buds"]


class TokenAdminForm(forms.ModelForm):
    class Meta:
        model = Token
        fields = "__all__"

    def clean(self):
        cleaned_data = super().clean()
        if cleaned_data.get("token_type") == Token.FREE and not cleaned_data.get("expires_at"):
            raise forms.ValidationError("Free Tokens must have an expiration date.")
        return cleaned_data


class TokenAdmin(admin.ModelAdmin):
    form = TokenAdminForm


admin.site.register(User, UserAdmin)
admin.site.register(LoginToken)
admin.site.register(Token, TokenAdmin)
