"""Django admin configuration for PI FYAD."""

from django.contrib import admin

from .models import (
    CharacterOwner,
    CharacterPlanet,
    CustomsOffice,
    PlanetPin,
    PlanetTemplate,
    StorageContents,
    TemplatePin,
)


@admin.register(CharacterOwner)
class CharacterOwnerAdmin(admin.ModelAdmin):
    list_display = ["character", "last_planet_sync", "last_customs_sync", "sync_enabled"]
    list_filter = ["sync_enabled"]
    search_fields = ["character__character_name"]
    readonly_fields = ["created_at"]


class PlanetPinInline(admin.TabularInline):
    model = PlanetPin
    extra = 0
    readonly_fields = ["pin_id", "type_id", "type_name", "is_extractor", "is_factory", "is_storage"]
    fields = ["pin_id", "type_name", "is_extractor", "is_factory", "is_storage", "factory_tier"]


@admin.register(CharacterPlanet)
class CharacterPlanetAdmin(admin.ModelAdmin):
    list_display = ["owner", "planet_name", "solar_system_name", "planet_type_id", "num_pins", "last_update"]
    list_filter = ["planet_type_id"]
    search_fields = ["planet_name", "solar_system_name", "owner__character__character_name"]
    inlines = [PlanetPinInline]


class StorageContentsInline(admin.TabularInline):
    model = StorageContents
    extra = 0
    readonly_fields = ["type_id", "type_name", "quantity", "total_volume"]


@admin.register(PlanetPin)
class PlanetPinAdmin(admin.ModelAdmin):
    list_display = ["pin_id", "type_name", "planet", "is_extractor", "is_factory", "is_storage", "factory_tier"]
    list_filter = ["is_extractor", "is_factory", "is_storage", "factory_tier"]
    search_fields = ["type_name", "planet__planet_name"]
    inlines = [StorageContentsInline]


@admin.register(CustomsOffice)
class CustomsOfficeAdmin(admin.ModelAdmin):
    list_display = ["office_id", "planet", "tax_rate", "allow_alliance", "last_update"]
    search_fields = ["planet__planet_name"]


class TemplatePinInline(admin.TabularInline):
    model = TemplatePin
    extra = 0
    readonly_fields = ["pin_index", "type_id", "type_name"]


@admin.register(PlanetTemplate)
class PlanetTemplateAdmin(admin.ModelAdmin):
    list_display = ["name", "planet_type_name", "visibility", "created_by", "pin_count", "created_at"]
    list_filter = ["visibility", "planet_type_id"]
    search_fields = ["name", "description"]
    inlines = [TemplatePinInline]
    readonly_fields = ["created_at", "updated_at"]
