from django.contrib import admin
from market.models import Market, MarketOption, UserBet, UserPointChange


class MarketOptionInline(admin.TabularInline):
    model = MarketOption
    extra = 1
    fields = ["text"]


@admin.register(Market)
class MarketAdmin(admin.ModelAdmin):
    list_display = ["title", "status", "end_date", "created_by", "created_at"]
    list_filter = ["status", "created_at"]
    search_fields = ["uuid", "title"]
    readonly_fields = [
        "uuid",
        "created_at",
        "updated_at",
        "created_by",
        "winning_option",
    ]
    inlines = [MarketOptionInline]
    fieldsets = (
        ("Info", {"fields": ["uuid", "title", "description"]}),
        ("Configuration", {"fields": ["end_date", "multiplier"]}),
        ("Status", {"fields": ["status", "winning_option"]}),
        ("Metadata", {"fields": ["created_by", "created_at", "updated_at"]}),
    )

    def has_change_permission(self, request, obj=None):
        # Prevent any changes to resolved markets
        if obj and obj.status == Market.Status.RESOLVED:
            return False
        return super().has_change_permission(request, obj)

    def save_model(self, request, obj, form, change):
        if not change:  # Creating new market
            obj.created_by = request.user
        super().save_model(request, obj, form, change)

    @admin.action(description="Publish selected draft markets")
    def publish_markets(self, request, queryset):
        updated = queryset.filter(status=Market.Status.DRAFT).update(
            status=Market.Status.OPEN
        )
        self.message_user(request, f"Published {updated} market(s).")

    @admin.action(description="Close selected markets")
    def close_markets(self, request, queryset):
        updated = queryset.filter(status=Market.Status.OPEN).update(
            status=Market.Status.CLOSED
        )
        self.message_user(request, f"Closed {updated} market(s).")

    actions = ["publish_markets", "close_markets"]


@admin.register(MarketOption)
class MarketOptionAdmin(admin.ModelAdmin):
    list_display = ["text", "market"]
    list_filter = ["market"]
    search_fields = ["uuid", "text", "market__title"]
    readonly_fields = ["uuid"]


@admin.register(UserBet)
class UserBetAdmin(admin.ModelAdmin):
    list_display = ["user", "option", "amount", "created_at"]
    list_filter = ["user", "created_at", "option__market"]
    search_fields = ["uuid", "user__username", "option__text"]
    readonly_fields = ["uuid", "user", "option", "amount", "created_at", "updated_at"]

    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False


@admin.register(UserPointChange)
class UserPointChangeAdmin(admin.ModelAdmin):
    list_display = ["user", "market", "amount", "reason", "created_at"]
    list_filter = ["user", "reason", "created_at", "market"]
    search_fields = ["uuid", "user__username", "market__title"]
    readonly_fields = ["uuid", "created_at", "updated_at"]

    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False
