from django.contrib import admin

from .models import (
    Comment,
    FileIndex,
    Project,
    ProjectAsset,
    ProjectContributor,
    ProjectDraft,
    Rating,
    Tag,
    TagCategory,
    TagList,
    TempUpload,
    Version,
    VersionFile,
)


@admin.register(FileIndex)
class FileIndexAdmin(admin.ModelAdmin):
    list_display = ('uuid', 'kind', 'owner', 'original_filename', 'size', 'created_at')
    search_fields = ('uuid', 'original_filename', 'stored_path')
    list_filter = ('kind',)


@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
    list_display = ('slug', 'title', 'category', 'owner', 'rating_score', 'rating_count', 'created_at', 'updated_at')
    search_fields = ('slug', 'title', 'summary')
    list_filter = ('category', 'created_at')


@admin.register(Version)
class VersionAdmin(admin.ModelAdmin):
    list_display = ('project', 'version_name', 'pack_format', 'downloads', 'created_at')
    search_fields = ('project__title', 'version_name')


@admin.register(VersionFile)
class VersionFileAdmin(admin.ModelAdmin):
    list_display = ('version', 'filename', 'kind', 'created_at')
    list_filter = ('kind',)
    search_fields = ('version__project__title', 'version__version_name', 'file__original_filename')


@admin.register(ProjectAsset)
class ProjectAssetAdmin(admin.ModelAdmin):
    list_display = ('project', 'caption', 'uploaded_by', 'created_at')
    search_fields = ('project__title', 'caption')


@admin.register(ProjectContributor)
class ProjectContributorAdmin(admin.ModelAdmin):
    list_display = ('project', 'user', 'added_by', 'added_at')
    search_fields = ('project__title', 'user__username')


@admin.register(TagCategory)
class TagCategoryAdmin(admin.ModelAdmin):
    list_display = ('slug', 'name', 'color')


@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
    list_display = ('name', 'category', 'created_by', 'created_at')
    list_filter = ('category',)
    search_fields = ('name',)


@admin.register(TagList)
class TagListAdmin(admin.ModelAdmin):
    list_display = ('project', 'tag', 'auto', 'added_by', 'added_at')
    list_filter = ('auto',)
    search_fields = ('project__title', 'tag__name')


@admin.register(TempUpload)
class TempUploadAdmin(admin.ModelAdmin):
    list_display = ('uuid', 'user', 'kind', 'status', 'draft_uuid', 'created_at')
    list_filter = ('kind', 'status')
    search_fields = ('user__username',)


@admin.register(ProjectDraft)
class ProjectDraftAdmin(admin.ModelAdmin):
    list_display = ('uuid', 'user', 'draft_title', 'updated_at')
    search_fields = ('user__username',)

    @admin.display(description='Title')
    def draft_title(self, obj):
        return obj.data.get('title') or '(untitled)'


@admin.register(Rating)
class RatingAdmin(admin.ModelAdmin):
    list_display = ('project', 'user', 'value', 'updated_at')
    list_filter = ('value',)
    search_fields = ('project__title', 'user__username')


@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    list_display = ('project', 'user', 'body_preview', 'created_at')
    search_fields = ('project__title', 'user__username', 'body')

    @admin.display(description='Body')
    def body_preview(self, obj):
        return obj.body[:80]
