from django.contrib import admin
from django import forms
from django.urls import reverse
from django.utils.html import format_html

from .models import Metadata_Files, Submission_File, User_Profile_Images, User_Banner_Images

# Register your models here.

class Submission_FileAdmin(admin.ModelAdmin):
    list_display = ('id', 'file_link', 'date_added', 'file_hash', 'file',)
    search_fields = ['file_name', 'file_hash']

    def file_link(self, obj):
        url = reverse("admin:files_submission_file_change", args=[obj.id])
        return format_html('<a href="{}">{}</a>', url, obj.file_name)
    
    file_link.short_description = 'File Name'


class User_Banner_ImagesAdmin(admin.ModelAdmin):
    list_display = ('id', 'file_link', 'date_added', 'file_hash', 'file',)

    def file_link(self, obj):
        url = reverse("admin:files_user_banner_images_change", args=[obj.id])
        return format_html('<a href="{}">{}</a>', url, obj.file_name)
    
    file_link.short_description = 'File Name'


class User_Profile_ImagesAdmin(admin.ModelAdmin):
    list_display = ('id', 'file_link', 'date_added', 'file_hash', 'file',)

    def get_form(self, request, obj=None, **kwargs):
       form = super().get_form(request, obj, **kwargs)
       form.base_fields['file_hash'].widget = forms.TextInput(attrs={'readonly': True})
       return form

    def file_link(self, obj):
        url = reverse("admin:files_user_profile_images_change", args=[obj.id])
        return format_html('<a href="{}">{}</a>', url, obj.file_name)
    
    file_link.short_description = 'File Name'

class Metadata_FilesAdmin(admin.ModelAdmin):
    list_display = ('id', 'file_link', 'date_added', 'file_hash', 'file',)

    def get_form(self, request, obj=None, **kwargs):
       form = super().get_form(request, obj, **kwargs)
       form.base_fields['file_hash'].widget = forms.TextInput(attrs={'readonly': True})
       return form

    def file_link(self, obj):
        url = reverse("admin:files_metadata_files_change", args=[obj.id])
        return format_html('<a href="{}">{}</a>', url, obj.file_name)
    
    file_link.short_description = 'File Name'


admin.site.register(Metadata_Files, Metadata_FilesAdmin)
admin.site.register(Submission_File, Submission_FileAdmin)
admin.site.register(User_Banner_Images, User_Banner_ImagesAdmin)
admin.site.register(User_Profile_Images, User_Profile_ImagesAdmin)