from django.contrib import admin
from .models import InstallationAssignment, InstallationReport, InstallationPhoto, InstallationItemChange


@admin.register(InstallationAssignment)
class InstallationAssignmentAdmin(admin.ModelAdmin):
    list_display = ('process_instance', 'installer', 'scheduled_date', 'created')
    search_fields = ('process_instance__code', 'installer__username', 'installer__first_name', 'installer__last_name')
    list_filter = ('scheduled_date',)


class InstallationPhotoInline(admin.TabularInline):
    model = InstallationPhoto
    extra = 0


class InstallationItemChangeInline(admin.TabularInline):
    model = InstallationItemChange
    extra = 0


@admin.register(InstallationReport)
class InstallationReportAdmin(admin.ModelAdmin):
    list_display = (
        'assignment', 'visited_date', 'meter_type', 'meter_size', 'water_meter_manufacturer',
        'discharge_pipe_diameter', 'usage_type', 'exploitation_license_number',
        'motor_power', 'pre_calibration_flow_rate', 'post_calibration_flow_rate',
        'new_water_meter_serial', 'seal_number', 'sim_number',
        'is_meter_suspicious', 'approved', 'created'
    )
    list_filter = ('is_meter_suspicious', 'approved', 'visited_date', 'meter_type', 'usage_type', 'water_meter_manufacturer')
    search_fields = (
        'assignment__process_instance__code', 'new_water_meter_serial', 'seal_number', 'exploitation_license_number', 'sim_number'
    )
    inlines = [InstallationPhotoInline, InstallationItemChangeInline]
    fieldsets = (
        ('زمان و تایید', {
            'fields': ('visited_date', 'approved', 'approved_at')
        }),
        ('کنتور و سازنده', {
            'fields': (
                'meter_type', 'meter_size', 'water_meter_manufacturer', 'new_water_meter_serial', 'seal_number', 'is_meter_suspicious', 'sim_number'
            )
        }),
        ('مشخصات هیدرولیکی', {
            'fields': ('discharge_pipe_diameter', 'pre_calibration_flow_rate', 'post_calibration_flow_rate')
        }),
        ('کاربری و مجوز', {
            'fields': ('usage_type', 'exploitation_license_number')
        }),
        ('توان و محرکه', {
            'fields': ('driving_force', 'motor_power')
        }),
        ('توضیحات', {
            'fields': ('description',)
        }),
    )


@admin.register(InstallationPhoto)
class InstallationPhotoAdmin(admin.ModelAdmin):
    list_display = ('report', 'created')


@admin.register(InstallationItemChange)
class InstallationItemChangeAdmin(admin.ModelAdmin):
    list_display = ('report', 'item', 'change_type', 'quantity', 'unit_price', 'total_price', 'created')
    list_filter = ('change_type',)

