from django.contrib import admin
from .models import (
    Department, Shift, Employee, BiometricDevice, 
    AttendanceLog, DailyAttendance, LeaveType, 
    LeaveRequest, LeaveBalance
)

@admin.register(Department)
class DepartmentAdmin(admin.ModelAdmin):
    list_display = ('name', 'code')

@admin.register(Shift)
class ShiftAdmin(admin.ModelAdmin):
    list_display = ('name', 'start_time', 'end_time')

@admin.register(Employee)
class EmployeeAdmin(admin.ModelAdmin):
    list_display = ('employee_id', 'biometric_id', 'first_name', 'last_name', 'department', 'position', 'is_active')
    list_filter = ('department', 'shift', 'is_active')
    search_fields = ('employee_id', 'biometric_id', 'first_name', 'last_name', 'position')

@admin.register(BiometricDevice)
class BiometricDeviceAdmin(admin.ModelAdmin):
    list_display = ('name', 'ip_address', 'port', 'is_active', 'last_sync')
    list_filter = ('is_active',)

@admin.register(AttendanceLog)
class AttendanceLogAdmin(admin.ModelAdmin):
    list_display = ('employee', 'timestamp', 'device')
    list_filter = ('device', 'timestamp')
    search_fields = ('employee__first_name', 'employee__last_name', 'employee__employee_id', 'employee__biometric_id')

@admin.register(DailyAttendance)
class DailyAttendanceAdmin(admin.ModelAdmin):
    list_display = ('employee', 'date', 'check_in', 'check_out', 'status', 'is_late', 'worked_hours')
    list_filter = ('date', 'status', 'is_late')
    search_fields = ('employee__first_name', 'employee__last_name', 'employee__employee_id')

@admin.register(LeaveType)
class LeaveTypeAdmin(admin.ModelAdmin):
    list_display = ('name', 'total_days', 'color_code')

@admin.register(LeaveRequest)
class LeaveRequestAdmin(admin.ModelAdmin):
    list_display = ('employee', 'leave_type', 'start_date', 'end_date', 'status', 'applied_on')
    list_filter = ('status', 'leave_type', 'start_date')
    actions = ['approve_requests', 'reject_requests']

    def approve_requests(self, request, queryset):
        queryset.update(status='approved', approved_by=request.user)
    approve_requests.short_description = "Approve selected leave requests"

    def reject_requests(self, request, queryset):
        queryset.update(status='rejected')
    reject_requests.short_description = "Reject selected leave requests"

@admin.register(LeaveBalance)
class LeaveBalanceAdmin(admin.ModelAdmin):
    list_display = ('employee', 'leave_type', 'allocated', 'used', 'remaining')
    list_filter = ('leave_type',)
