from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from .models import MinistryReportSettings
from .ministry_reports_helper import invalidate_ministry_report_cache
from customers.models import Customer
from savings.models import SavingAccount, SavingAccountTransactions
from loans.models import LoanApplication
from shares.models import SharesTransaction
from ledgers.models import SystemTransactions


@receiver(post_save, sender=MinistryReportSettings)
@receiver(post_delete, sender=MinistryReportSettings)
def invalidate_ministry_cache_on_settings_change(sender, instance, **kwargs):
    """Invalidate ministry report cache when settings change"""
    invalidate_ministry_report_cache(instance.organisation_id)


@receiver(post_save, sender=Customer)
@receiver(post_delete, sender=Customer)
def invalidate_ministry_cache_on_customer_change(sender, instance, **kwargs):
    """Invalidate ministry report cache when customer data changes"""
    if hasattr(instance, 'customer_branch') and instance.customer_branch:
        org_id = instance.customer_branch.branch_organisation_id
        invalidate_ministry_report_cache(org_id)


@receiver(post_save, sender=SavingAccount)
@receiver(post_delete, sender=SavingAccount)
def invalidate_ministry_cache_on_savings_change(sender, instance, **kwargs):
    """Invalidate ministry report cache when savings account changes"""
    if hasattr(instance, 'customer_branch') and instance.customer_branch:
        org_id = instance.customer_branch.branch_organisation_id
        invalidate_ministry_report_cache(org_id)


@receiver(post_save, sender=SavingAccountTransactions)
def invalidate_ministry_cache_on_savings_transaction(sender, instance, **kwargs):
    """Invalidate ministry report cache when savings transactions occur"""
    if hasattr(instance, 'customer_account') and instance.customer_account:
        if hasattr(instance.customer_account, 'customer_branch') and instance.customer_account.customer_branch:
            org_id = instance.customer_account.customer_branch.branch_organisation_id
            invalidate_ministry_report_cache(org_id)


@receiver(post_save, sender=LoanApplication)
@receiver(post_delete, sender=LoanApplication)
def invalidate_ministry_cache_on_loan_change(sender, instance, **kwargs):
    """Invalidate ministry report cache when loan data changes"""
    if hasattr(instance, 'organisation_branch') and instance.organisation_branch:
        org_id = instance.organisation_branch.branch_organisation_id
        invalidate_ministry_report_cache(org_id)


@receiver(post_save, sender=SharesTransaction)
@receiver(post_delete, sender=SharesTransaction)
def invalidate_ministry_cache_on_shares_change(sender, instance, **kwargs):
    """Invalidate ministry report cache when shares data changes"""
    if hasattr(instance, 'organisation_branch') and instance.organisation_branch:
        org_id = instance.organisation_branch.branch_organisation_id
        invalidate_ministry_report_cache(org_id)


@receiver(post_save, sender=SystemTransactions)
def invalidate_ministry_cache_on_system_transaction(sender, instance, **kwargs):
    """Invalidate ministry report cache when system transactions occur"""
    # Get organization from credit or debit chart
    org_id = None
    if hasattr(instance, 'credit_chart') and instance.credit_chart:
        org_id = instance.credit_chart.account_organisation_id
    elif hasattr(instance, 'debit_chart') and instance.debit_chart:
        org_id = instance.debit_chart.account_organisation_id
    
    if org_id:
        invalidate_ministry_report_cache(org_id)