from django.contrib import admin
from .models import (
    Region, Constituency, Farmer, AgriculturalHolding,
    CropProduction, LivestockProduction, Forestry, Fishery, LandRegistry
)

class CropProductionInline(admin.TabularInline):
    model = CropProduction
    extra = 1

class LivestockProductionInline(admin.TabularInline):
    model = LivestockProduction
    extra = 1

class ForestryInline(admin.TabularInline):
    model = Forestry
    extra = 1

class FisheryInline(admin.TabularInline):
    model = Fishery
    extra = 1

class LandRegistryInline(admin.TabularInline):
    model = LandRegistry
    extra = 1

class AgriculturalHoldingInline(admin.TabularInline):
    model = AgriculturalHolding
    extra = 1

@admin.register(Region)
class RegionAdmin(admin.ModelAdmin):
    list_display = ('name', 'code')
    search_fields = ('name',)

@admin.register(Constituency)
class ConstituencyAdmin(admin.ModelAdmin):
    list_display = ('name', 'region')
    list_filter = ('region',)
    search_fields = ('name',)

@admin.register(Farmer)
class FarmerAdmin(admin.ModelAdmin):
    list_display = ('name', 'id_number', 'constituency', 'created_at')
    list_filter = ('constituency__region', 'constituency')
    search_fields = ('name', 'id_number')
    inlines = [AgriculturalHoldingInline, LandRegistryInline]

@admin.register(AgriculturalHolding)
class HoldingAdmin(admin.ModelAdmin):
    list_display = ('farmer', 'primary_activity', 'size_hectares')
    list_filter = ('primary_activity',)
    inlines = [CropProductionInline, LivestockProductionInline, ForestryInline, FisheryInline]

@admin.register(CropProduction)
class CropProductionAdmin(admin.ModelAdmin):
    list_display = ('crop_type', 'holding', 'area_hectares', 'expected_yield')
    list_filter = ('crop_type',)
    search_fields = ('crop_type', 'holding__farmer__name')

@admin.register(LivestockProduction)
class LivestockProductionAdmin(admin.ModelAdmin):
    list_display = ('animal_type', 'holding', 'count')
    list_filter = ('animal_type',)
    search_fields = ('animal_type', 'holding__farmer__name')

@admin.register(Forestry)
class ForestryAdmin(admin.ModelAdmin):
    list_display = ('tree_species', 'holding', 'area_hectares', 'purpose')
    list_filter = ('purpose',)
    search_fields = ('tree_species', 'holding__farmer__name')

@admin.register(Fishery)
class FisheryAdmin(admin.ModelAdmin):
    list_display = ('species', 'holding', 'type', 'capacity')
    list_filter = ('type', 'species')
    search_fields = ('species', 'holding__farmer__name')

@admin.register(LandRegistry)
class LandRegistryAdmin(admin.ModelAdmin):
    list_display = ('parcel_number', 'farmer', 'ownership_type', 'area_hectares')
    list_filter = ('ownership_type',)
    search_fields = ('parcel_number', 'farmer__name', 'title_deed_number')