from django.db import models

class Region(models.Model):
    name = models.CharField(max_length=100, unique=True)
    code = models.CharField(max_length=10, unique=True, blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['name']

class Constituency(models.Model):
    region = models.ForeignKey(Region, on_delete=models.CASCADE, related_name='constituencies')
    name = models.CharField(max_length=100)

    def __str__(self):
        return f"{self.name} ({self.region.name})"

    class Meta:
        ordering = ['region', 'name']
        verbose_name_plural = "Constituencies"

class Farmer(models.Model):
    name = models.CharField(max_length=200)
    id_number = models.CharField(max_length=50, unique=True, verbose_name="National ID/Passport")
    phone_number = models.CharField(max_length=20, blank=True)
    constituency = models.ForeignKey(Constituency, on_delete=models.PROTECT)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

class AgriculturalHolding(models.Model):
    HOLDING_TYPES = [
        ('CROP', 'Crop Production'),
        ('LIVESTOCK', 'Livestock Production'),
        ('MIXED', 'Mixed Farming'),
        ('FISHERIES', 'Fisheries/Aquaculture'),
        ('FORESTRY', 'Forestry'),
    ]
    farmer = models.ForeignKey(Farmer, on_delete=models.CASCADE, related_name='holdings')
    size_hectares = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="Size (Hectares)")
    primary_activity = models.CharField(max_length=20, choices=HOLDING_TYPES)
    location_description = models.TextField(blank=True)

    def __str__(self):
        return f"{self.get_primary_activity_display()} - {self.farmer.name}"

class CropProduction(models.Model):
    holding = models.ForeignKey(AgriculturalHolding, on_delete=models.CASCADE, related_name='crops')
    crop_type = models.CharField(max_length=100)
    area_hectares = models.DecimalField(max_digits=10, decimal_places=2)
    expected_yield = models.DecimalField(max_digits=10, decimal_places=2, help_text="In metric tons", null=True, blank=True)
    
    def __str__(self):
        return f"{self.crop_type} - {self.holding.farmer.name}"

    class Meta:
        verbose_name_plural = "Crop Production"

class LivestockProduction(models.Model):
    holding = models.ForeignKey(AgriculturalHolding, on_delete=models.CASCADE, related_name='livestock')
    animal_type = models.CharField(max_length=100)
    count = models.PositiveIntegerField()
    
    def __str__(self):
        return f"{self.animal_type} ({self.count}) - {self.holding.farmer.name}"

    class Meta:
        verbose_name_plural = "Livestock Production"

class Forestry(models.Model):
    holding = models.ForeignKey(AgriculturalHolding, on_delete=models.CASCADE, related_name='forestry_items')
    tree_species = models.CharField(max_length=100)
    area_hectares = models.DecimalField(max_digits=10, decimal_places=2)
    purpose = models.CharField(max_length=100, blank=True, help_text="e.g. Timber, Conservation, Charcoal")
    
    def __str__(self):
        return f"{self.tree_species} - {self.holding.farmer.name}"

    class Meta:
        verbose_name_plural = "Forestry"

class Fishery(models.Model):
    holding = models.ForeignKey(AgriculturalHolding, on_delete=models.CASCADE, related_name='fisheries')
    type = models.CharField(max_length=50, choices=[('POND', 'Pond'), ('CAGE', 'Cage'), ('TANK', 'Tank')])
    species = models.CharField(max_length=100)
    capacity = models.PositiveIntegerField(help_text="Volume in cubic meters or number of units")
    
    def __str__(self):
        return f"{self.species} ({self.get_type_display()}) - {self.holding.farmer.name}"

    class Meta:
        verbose_name_plural = "Fisheries/Aquaculture"

class LandRegistry(models.Model):
    farmer = models.ForeignKey(Farmer, on_delete=models.CASCADE, related_name='land_records')
    parcel_number = models.CharField(max_length=100, unique=True)
    ownership_type = models.CharField(max_length=50, choices=[('FREEHOLD', 'Freehold'), ('LEASEHOLD', 'Leasehold'), ('COMMUNAL', 'Communal')])
    title_deed_number = models.CharField(max_length=100, blank=True)
    area_hectares = models.DecimalField(max_digits=10, decimal_places=2)
    
    def __str__(self):
        return f"Parcel {self.parcel_number} - {self.farmer.name}"

    class Meta:
        verbose_name_plural = "Land Registry"
