from django.contrib import admin

from accounts.models import Role, Profile, Company


# Register your models here.
@admin.register(Role)
class RoleAdmin(admin.ModelAdmin):
    list_display = ['name', 'slug', 'parent', 'is_active']
    search_fields = ['name', 'slug']
    list_filter = ['is_active']
    ordering = ['parent__name', 'name']

@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
    list_display = [
        "user",
        "fullname",
        "user_type_display",
        "company_name",
        "pic_tag",
        "roles_str",
        "affairs",
        "county",
        "broker",
        "is_completed",
        "is_active",
        "jcreated",
    ]
    search_fields = [
        'user__username', 
        'user__first_name', 
        'user__last_name', 
        'user__phone_number',
        'company_name',
        'company_national_id',
        'national_code'
    ]
    list_filter = [
        'user_type',
        'user', 
        'roles', 
        'affairs', 
        'county', 
        'broker',
        'is_completed',
        'is_active'
    ]
    fieldsets = (
        ('اطلاعات کاربری', {
            'fields': ('user', 'user_type', 'pic', 'roles')
        }),
        ('اطلاعات شخصی - حقیقی', {
            'fields': ('national_code', 'address', 'phone_number_1', 'phone_number_2'),
            'classes': ('collapse',),
        }),
        ('اطلاعات شرکت - حقوقی', {
            'fields': ('company_name', 'company_national_id'),
            'classes': ('collapse',),
        }),
        ('اطلاعات بانکی', {
            'fields': ('card_number', 'account_number', 'bank_name'),
            'classes': ('collapse',),
        }),
        ('اطلاعات سازمانی', {
            'fields': ('affairs', 'county', 'broker', 'owner'),
        }),
        ('وضعیت', {
            'fields': ('is_completed', 'is_active'),
        }),
        ('تاریخ‌ها', {
            'fields': ('created', 'updated'),
            'classes': ('collapse',),
        }),
    )
    date_hierarchy = 'created'
    ordering = ['-created']
    readonly_fields = ['created', 'updated']

@admin.register(Company)
class CompanyAdmin(admin.ModelAdmin):
    list_display = ['name', 'logo', 'signature', 'address', 'phone', 'broker', 'registration_number']
    prepopulated_fields = {'slug': ('name',)}
    search_fields = ['name', 'address', 'phone']
    list_filter = ['is_active', 'broker']
    date_hierarchy = 'created'
    ordering = ['-created']