from django.contrib import admin

from mtp.apps.common.constants import SYNC_STATUS_FIELDS

from .models import (
    CertificationCompetencyThreshold,
    CertificationProgram,
    CertificationProgramUserStatus,
    CertificationStatus,
    Competency,
    CompetencyTree,
    CompetencyTreeUserScore,
    CompetencyUserScore,
    Curriculum,
    Dimension,
    DimensionUserScore,
    Lesson,
    LessonDimensionWeight,
    LessonRecommendationThreshold,
    RecommendationLevel,
    Topic,
)


class CertificationProgramAdmin(admin.ModelAdmin):
    list_display = ('name', 'tree')
    list_filter = ('tree',)

    fieldsets = (
        (None, {
            'fields': (
                'name',
                'tree',
            )
        }),
        SYNC_STATUS_FIELDS,
    )


class CertificationCompetencyThresholdInline(admin.TabularInline):
    model = CertificationCompetencyThreshold
    fields = ('competency', 'threshold')
    extra = 0


class CertificationStatusAdmin(admin.ModelAdmin):
    list_display = ('name', 'program', 'global_threshold', 'is_reference')
    list_editable = ('global_threshold', 'is_reference')
    list_filter = ('program',)

    fieldsets = (
        (None, {
            'fields': (
                'program',
                'name',
                'global_threshold',
                'is_reference',
            )
        }),
        SYNC_STATUS_FIELDS,
    )

    inlines = (
        CertificationCompetencyThresholdInline,
    )


class CertificationProgramUserStatusInline(admin.TabularInline):
    model = CertificationProgramUserStatus
    fields = ('program', 'status', 'date_reached', 'is_validated')
    extra = 0
    fk_name = 'user'


class CompetencyTreeAdmin(admin.ModelAdmin):
    list_display = ('name', 'job_profile')

    fieldsets = (
        (None, {
            'fields': (
                'name',
                'job_profile',
            )
        }),
        SYNC_STATUS_FIELDS,
    )


class CompetencyTreeUserScoreInline(admin.TabularInline):
    model = CompetencyTreeUserScore
    fields = ('tree', 'score')
    extra = 0


class CompetencyAdmin(admin.ModelAdmin):
    list_display = ('name', 'code', 'tree', 'sort_order', 'is_ghost')
    list_editable = ('code', 'sort_order', 'is_ghost',)
    list_filter = ('tree', 'is_ghost')
    ordering = ('tree', 'sort_order', '-is_ghost', 'name')

    fieldsets = (
        (None, {
            'fields': (
                'name',
                'code',
                'tree',
                'sort_order',
                'description',
                'is_ghost',
            )
        }),
        SYNC_STATUS_FIELDS,
    )


class CompetencyUserScoreInline(admin.TabularInline):
    model = CompetencyUserScore
    fields = ('competency', 'score')
    extra = 0


class DimensionAdmin(admin.ModelAdmin):
    list_display = ('name', 'code', 'competency', 'sort_order', 'default_weight')
    list_editable = ('code', 'sort_order', 'default_weight',)
    list_filter = ('competency__tree', 'competency',)
    ordering = ('competency__sort_order', 'sort_order', 'name')

    fieldsets = (
        (None, {
            'fields': (
                'name',
                'code',
                'competency',
                'sort_order',
                'default_weight',
                'description',
            )
        }),
        SYNC_STATUS_FIELDS,
    )


class DimensionUserScoreInline(admin.TabularInline):
    model = DimensionUserScore
    fields = ('dimension', 'score')
    extra = 0


class RecommendationLevelAdmin(admin.ModelAdmin):
    list_display = ('name', 'is_reference')
    list_editable = ('is_reference',)
    ordering = ('name',)

    fieldsets = (
        (None, {
            'fields': (
                'name',
                'description',
            )
        }),
        SYNC_STATUS_FIELDS,
    )


class CurriculumAdmin(admin.ModelAdmin):
    list_display = ('name',)
    ordering = ('name',)

    fieldsets = (
        (None, {
            'fields': (
                'name',
                'description',
                'job_profile',
                'mgroups',
                'active_recommendation_levels',
            )
        }),
        SYNC_STATUS_FIELDS,
    )
    filter_horizontal = ('mgroups', 'active_recommendation_levels')


class TopicAdmin(admin.ModelAdmin):
    list_display = ('name', 'curriculum', 'sort_order')
    list_editable = ('sort_order',)
    list_filter = ('curriculum',)
    ordering = ('sort_order', 'name',)

    fieldsets = (
        (None, {
            'fields': (
                'name',
                'curriculum',
                'description',
                'sort_order',
            )
        }),
        SYNC_STATUS_FIELDS,
    )


class LessonDimensionWeightInline(admin.TabularInline):
    model = LessonDimensionWeight
    fields = ('dimension', 'weight')
    extra = 0


class LessonRecommendationThresholdInline(admin.TabularInline):
    model = LessonRecommendationThreshold
    fields = ('level', 'threshold')
    extra = 0


class LessonAdmin(admin.ModelAdmin):
    list_display = ('name', 'type', 'curriculum', 'topic', 'sort_order', 'is_active')
    list_editable = ('sort_order', 'is_active',)
    list_filter = ('curriculum', 'topic', 'is_active')
    ordering = ('curriculum', 'topic', 'sort_order', 'name')

    fieldsets = (
        (None, {
            'fields': (
                'name',
                'curriculum',
                'topic',
                'type',
                'duration',
                'description',
                'sort_order',
                'is_active',
            )
        }),
        SYNC_STATUS_FIELDS,
    )

    inlines = (
        LessonDimensionWeightInline,
        LessonRecommendationThresholdInline,
    )


admin.site.register(CompetencyTree, CompetencyTreeAdmin)
admin.site.register(Competency, CompetencyAdmin)
admin.site.register(Dimension, DimensionAdmin)

admin.site.register(CertificationProgram, CertificationProgramAdmin)
admin.site.register(CertificationStatus, CertificationStatusAdmin)

admin.site.register(Curriculum, CurriculumAdmin)
admin.site.register(Lesson, LessonAdmin)
admin.site.register(RecommendationLevel, RecommendationLevelAdmin)
admin.site.register(Topic, TopicAdmin)
