from django.contrib import admin
from .models import (
    UserProfile, Project, Worker, Team, WorkLog, 
    PayrollRecord, Loan, PayrollAdjustment, 
    ExpenseReceipt, ExpenseLineItem
)

@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
    list_display = ('user',)
    search_fields = ('user__username', 'user__first_name', 'user__last_name')

@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
    list_display = ('name', 'active')
    list_filter = ('active',)
    search_fields = ('name', 'description')
    filter_horizontal = ('supervisors',)

@admin.register(Worker)
class WorkerAdmin(admin.ModelAdmin):
    list_display = ('name', 'id_number', 'monthly_salary', 'active')
    list_filter = ('active', 'has_drivers_license')
    search_fields = ('name', 'id_number', 'phone_number')

    # === FIELDSETS ===
    # Organise the worker edit form into clear sections
    fieldsets = (
        ('Personal Info', {
            'fields': ('name', 'id_number', 'phone_number', 'monthly_salary',
                       'employment_date', 'active', 'notes'),
        }),
        ('Sizing', {
            'fields': ('shoe_size', 'overall_top_size', 'pants_size', 'tshirt_size'),
        }),
        ('Documents & License', {
            'fields': ('photo', 'id_document', 'has_drivers_license', 'drivers_license'),
        }),
    )

@admin.register(Team)
class TeamAdmin(admin.ModelAdmin):
    list_display = ('name', 'supervisor', 'pay_frequency', 'pay_start_date', 'active')
    list_editable = ('pay_frequency', 'pay_start_date')
    list_filter = ('active', 'supervisor', 'pay_frequency')
    search_fields = ('name',)
    filter_horizontal = ('workers',)

@admin.register(WorkLog)
class WorkLogAdmin(admin.ModelAdmin):
    list_display = ('date', 'project', 'supervisor', 'overtime_amount')
    list_filter = ('date', 'project', 'supervisor')
    search_fields = ('project__name', 'notes')
    filter_horizontal = ('workers', 'priced_workers')

@admin.register(PayrollRecord)
class PayrollRecordAdmin(admin.ModelAdmin):
    list_display = ('worker', 'date', 'amount_paid')
    list_filter = ('date', 'worker')
    search_fields = ('worker__name',)
    filter_horizontal = ('work_logs',)

@admin.register(Loan)
class LoanAdmin(admin.ModelAdmin):
    list_display = ('worker', 'principal_amount', 'remaining_balance', 'date', 'active')
    list_filter = ('active', 'date', 'worker')
    search_fields = ('worker__name', 'reason')

@admin.register(PayrollAdjustment)
class PayrollAdjustmentAdmin(admin.ModelAdmin):
    list_display = ('worker', 'type', 'amount', 'date')
    list_filter = ('type', 'date', 'worker')
    search_fields = ('worker__name', 'description')

class ExpenseLineItemInline(admin.TabularInline):
    model = ExpenseLineItem
    extra = 1

@admin.register(ExpenseReceipt)
class ExpenseReceiptAdmin(admin.ModelAdmin):
    list_display = ('vendor_name', 'date', 'total_amount', 'user')
    list_filter = ('date', 'payment_method', 'vat_type')
    search_fields = ('vendor_name', 'description')
    inlines = [ExpenseLineItemInline]

@admin.register(ExpenseLineItem)
class ExpenseLineItemAdmin(admin.ModelAdmin):
    list_display = ('product_name', 'amount', 'receipt')
    search_fields = ('product_name', 'receipt__vendor_name')