from django.contrib import admin

from apps.channels.models import Genre, Network, Channel, ChannelsZone, ChannelNetworkMembership, Jingle



# Register the Genre model with the admin site.
@admin.register(Genre)
class GenreAdmin(admin.ModelAdmin):
    list_display = ("name", ) # Fields displayed in the list view
    search_fields = ("name", ) # Enables searching by name 
    list_filter = ("name", ) # Adds filters on the right side of the list view
    list_per_page = 20  # Number of items to display per page
    
    # Define fieldsets to group fields in the detail view
    fieldsets = (
        (None, {
            "fields": ("name",)
        }),
        ("Description", {
            "fields": ("description",),
            "classes": ("collapse",)  # Collapsible section
        }),
    )

# Register the Network model with the admin site.
@admin.register(Network)
class NetworkAdmin(admin.ModelAdmin):
    list_display = ("name", "logo")  # Fields displayed in the list view
    search_fields = ("name",) # Enables searching by name or channel_number or channel_name
    list_filter = ("name",) # Adds filters on the right side of the list view 
    list_per_page = 20  # Number of items to display per page
    
    # Define fieldsets to group fields in the detail view
    fieldsets = (
        (None, {
            "fields": ("name", "logo")
        }),
        # ("Channel", {
        #     "fields": ("channel_number", "channel_name",)
        # }),
        ("Description", {
            "fields": ("description",),
            "classes": ("collapse",)  # Collapsible section
        }),
    )

# Register the ChannelsZone model with the admin site.
@admin.register(ChannelsZone)
class ChannelsZoneAdmin(admin.ModelAdmin):
    list_display = ("channel", "region", "zonename")
    search_fields = ("channel__name", "region", "zonename")
    list_filter = ("region", "zonename")

    # Define fieldsets to group fields in the detail view
    fieldsets = (
        (None, {
            "fields": ("channel", "region", "zonename")
        }), 
    )

# Register the ChannelNetworkMembership model with the admin site.
@admin.register(ChannelNetworkMembership)
class ChannelNetworkMembershipAdmin(admin.ModelAdmin):
    list_display = ("channel", "network", "channel_name")
    search_fields = ("channel__name", "channel_name", "network__name")
    list_filter = ("channel__name", "network__name", "channel_name" )
    
    fieldsets = (
        (None, {
            "fields": ("channel", "network", "channel_name", "channel_number")
        }), 
    )

# Register the Jingle model with the admin site.
@admin.register(Jingle)
class JingleAdmin(admin.ModelAdmin):
    list_display = ("title", "channel", "status")
    search_fields = ("title", "description", "channel__name")
    list_filter = ("status",)
    
    fieldsets = (
        (None, {
            "fields": ("channel", "title", "description", "status")
        }),         
        ("Files", {
            "fields": ("video_file", "md5_file")
        }), 
    )


# Define an Inline for ChannelsZone within the Channel admin for easier management.
class ChannelsZoneInline(admin.TabularInline):
    model = ChannelsZone
    extra = 1

    fieldsets = (
        (None, {
            "fields": ("region", "zonename")
        }), 
    )

# Define an Inline for ChannelNetworkMembership within the Channel admin for easier management.
class ChannelNetworkMembershipInline(admin.TabularInline):
    model = ChannelNetworkMembership
    extra = 1

    fieldsets = (
        (None, {
            "fields": ("network", "channel_name", "channel_number")
        }), 
    )

# Define an Inline for Jingle within the Channel admin for easier management.
class JingleInline(admin.TabularInline):
    model = Jingle
    extra = 1

    fieldsets = (
        (None, {
            "fields": ("title", "description", "status", "video_file", "md5_file")
        }), 
    )

# Register the Channel model with the admin site.
@admin.register(Channel)
class ChannelAdmin(admin.ModelAdmin):
    list_display = ("name", "channel_number", "owner", "launch_date")
    search_fields = ("name", "owner", "headquarters_location")
    list_filter = ("launch_date",)
    inlines = [ChannelsZoneInline, ChannelNetworkMembershipInline, JingleInline, ]  # Display ChannelsZone and Jingle as inlines in Channel admin.
    fieldsets = (
        (None, {
            "fields": ("name", "channel_number", "description", "logo")
        }),   
        ("Relations", {
            "fields": ("genres", "languages")
        }),
        ("Optional Information", {
            "fields": ("owner", "launch_date", "website", "headquarters_location")
        }), 
    )