from django.contrib import admin
from .models import (
    AnalysisGridCell,
    AnalysisGridObservation,
    BlockSubdivision,
    RemoteSensingClusterBlock,
    RemoteSensingClusterAssignment,
    RemoteSensingSubdivisionOption,
    RemoteSensingSubdivisionOptionAssignment,
    RemoteSensingSubdivisionOptionBlock,
    RemoteSensingRun,
    RemoteSensingSubdivisionResult,
    SoilLocation,
)


class BlockSubdivisionInline(admin.TabularInline):
    model = BlockSubdivision
    extra = 0
    readonly_fields = (
        "block_code",
        "chunk_size_sqm",
        "grid_point_count",
        "centroid_count",
        "status",
        "created_at",
        "updated_at",
    )
    fields = readonly_fields
    show_change_link = True


@admin.register(SoilLocation)
class SoilLocationAdmin(admin.ModelAdmin):
    list_display = ("id", "latitude", "longitude", "is_complete", "created_at")
    list_filter = ("created_at",)
    search_fields = ("latitude", "longitude")
    readonly_fields = ("created_at", "updated_at")
    inlines = [BlockSubdivisionInline]


@admin.register(BlockSubdivision)
class BlockSubdivisionAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "soil_location",
        "block_code",
        "chunk_size_sqm",
        "grid_point_count",
        "centroid_count",
        "status",
        "updated_at",
    )
    list_filter = ("status", "chunk_size_sqm", "created_at")
    search_fields = ("block_code", "soil_location__latitude", "soil_location__longitude")
    readonly_fields = ("created_at", "updated_at")


@admin.register(RemoteSensingRun)
class RemoteSensingRunAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "soil_location",
        "block_code",
        "provider",
        "chunk_size_sqm",
        "status",
        "temporal_start",
        "temporal_end",
        "created_at",
    )
    list_filter = ("provider", "status", "chunk_size_sqm", "created_at")
    search_fields = ("block_code", "soil_location__latitude", "soil_location__longitude")
    readonly_fields = ("created_at", "updated_at")


@admin.register(AnalysisGridCell)
class AnalysisGridCellAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "cell_code",
        "soil_location",
        "block_code",
        "chunk_size_sqm",
        "centroid_lat",
        "centroid_lon",
        "created_at",
    )
    list_filter = ("chunk_size_sqm", "created_at")
    search_fields = ("cell_code", "block_code", "soil_location__latitude", "soil_location__longitude")
    readonly_fields = ("created_at", "updated_at")


@admin.register(AnalysisGridObservation)
class AnalysisGridObservationAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "cell",
        "temporal_start",
        "temporal_end",
        "ndvi",
        "ndwi",
        "created_at",
    )
    list_filter = ("temporal_start", "temporal_end", "created_at")
    search_fields = ("cell__cell_code", "cell__block_code")
    readonly_fields = ("created_at", "updated_at")



@admin.register(RemoteSensingSubdivisionResult)
class RemoteSensingSubdivisionResultAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "soil_location",
        "block_code",
        "cluster_count",
        "chunk_size_sqm",
        "temporal_start",
        "temporal_end",
        "created_at",
    )
    list_filter = ("chunk_size_sqm", "cluster_count", "created_at")
    search_fields = ("block_code", "soil_location__latitude", "soil_location__longitude")
    readonly_fields = ("created_at", "updated_at")


@admin.register(RemoteSensingClusterBlock)
class RemoteSensingClusterBlockAdmin(admin.ModelAdmin):
    list_display = (
        "uuid",
        "soil_location",
        "block_code",
        "sub_block_code",
        "cluster_label",
        "cell_count",
        "updated_at",
    )
    list_filter = ("cluster_label", "chunk_size_sqm", "created_at")
    search_fields = ("uuid", "block_code", "sub_block_code", "soil_location__latitude", "soil_location__longitude")
    readonly_fields = ("created_at", "updated_at")


@admin.register(RemoteSensingSubdivisionOption)
class RemoteSensingSubdivisionOptionAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "result",
        "requested_k",
        "effective_cluster_count",
        "is_active",
        "is_recommended",
        "selection_source",
        "updated_at",
    )
    list_filter = ("is_active", "is_recommended", "selection_source", "created_at")
    search_fields = ("result__block_code", "result__soil_location__latitude", "result__soil_location__longitude")
    readonly_fields = ("created_at", "updated_at")


@admin.register(RemoteSensingSubdivisionOptionBlock)
class RemoteSensingSubdivisionOptionBlockAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "option",
        "cluster_label",
        "sub_block_code",
        "cell_count",
        "updated_at",
    )
    list_filter = ("cluster_label", "created_at")
    search_fields = ("sub_block_code", "option__result__block_code")
    readonly_fields = ("created_at", "updated_at")


@admin.register(RemoteSensingSubdivisionOptionAssignment)
class RemoteSensingSubdivisionOptionAssignmentAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "option",
        "cell",
        "cluster_label",
        "updated_at",
    )
    list_filter = ("cluster_label", "created_at")
    search_fields = ("cell__cell_code", "option__result__block_code")
    readonly_fields = ("created_at", "updated_at")


@admin.register(RemoteSensingClusterAssignment)
class RemoteSensingClusterAssignmentAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "result",
        "cell",
        "cluster_label",
        "created_at",
    )
    list_filter = ("cluster_label", "created_at")
    search_fields = ("cell__cell_code", "result__block_code")
    readonly_fields = ("created_at", "updated_at")
