from django.contrib import admin

from .models import ActionPlanStep, ProblemCase, RootCause, SolutionOption


class RootCauseInline(admin.TabularInline):
    model = RootCause
    extra = 0


class SolutionOptionInline(admin.TabularInline):
    model = SolutionOption
    extra = 0
    fields = ("rank", "title", "impact", "efficiency", "speed", "low_risk", "decision_score", "success_rate")
    readonly_fields = ("decision_score",)


@admin.register(ProblemCase)
class ProblemCaseAdmin(admin.ModelAdmin):
    list_display = ("title", "business_area", "urgency", "priority_score", "financial_impact", "status", "created_at")
    list_filter = ("business_area", "status", "urgency")
    search_fields = ("title", "description")
    inlines = [RootCauseInline, SolutionOptionInline]


@admin.register(SolutionOption)
class SolutionOptionAdmin(admin.ModelAdmin):
    list_display = ("title", "problem", "rank", "decision_score", "success_rate")
    list_filter = ("rank",)
    search_fields = ("title", "problem__title")


@admin.register(ActionPlanStep)
class ActionPlanStepAdmin(admin.ModelAdmin):
    list_display = ("solution", "day_index", "title", "is_done")
    list_filter = ("is_done",)
    search_fields = ("title", "task", "solution__title")


@admin.register(RootCause)
class RootCauseAdmin(admin.ModelAdmin):
    list_display = ("factor", "problem", "contribution_score", "parent")
    search_fields = ("factor", "why_chain", "problem__title")
