from django.contrib import admin

from .forms import TaskAdminForm, TaskListForm
from .models import Task, TaskList


class TaskAdmin(admin.ModelAdmin):
    form = TaskAdminForm
    model = Task
    fieldsets = (
        (
            None,
            {
                "fields": (
                    "title",
                    "task_list",
                    "assigned_to",
                    "due_date",
                    "completed",
                    "completed_date",
                    "note",
                    "priority",
                )
            },
        ),
    )

    list_display = [
        "title",
        "task_list",
        "assigned_to",
        "due_date",
        "completed",
        "priority",
    ]
    list_filter = ("task_list",)
    search_fields = ("title",)

    def save_model(self, request, obj, form, change):
        obj.created_by = request.user
        super().save_model(request, obj, form, change)


class TaskListAdmin(admin.ModelAdmin):
    form = TaskListForm
    model = TaskList


admin.site.register(TaskList, TaskListAdmin)
admin.site.register(Task, TaskAdmin)
