"""
ReportService - Financial report generation service.

This service generates all major financial reports: Trial Balance, Income Statement,
Balance Sheet, Cash Flow Statement, and Account Analysis reports.
"""

from decimal import Decimal
from typing import Optional, Dict, List
from datetime import date
from django.core.cache import cache
from django.db.models import Sum, Q
from django.utils import timezone

import logging

from accounting.models import Account, GeneralLedger
from accounting.services.accounting_service import AccountingService
from accounting.exceptions import FinancialStatementImbalanceError


class ReportService:
    """
    Service for generating financial reports.
    Uses AccountingService for balance calculations and implements caching for performance.
    """

    def __init__(self):
        self.accounting_service = AccountingService()

    def generate_trial_balance(
        self, as_of_date: date, branch: Optional = None
    ) -> Dict:
        """
        Generate trial balance report.

        Args:
            as_of_date: The date to generate trial balance as of
            branch: Optional Branch to filter by

        Returns:
            dict: Trial balance data with report_date, branch, accounts list,
                  totals, balanced flag, and by_type breakdown
        """
        # Use AccountingService's implementation which already has caching
        return self.accounting_service.get_trial_balance(as_of_date, branch)

    def generate_trial_balance_full(
        self,
        as_of_date: date,
        branch: Optional = None,
        period_start: Optional[date] = None,
    ) -> Dict:
        """
        Generate the professional 8-column Trial Balance used by auditors:
          GL Code | Description | Opening Dr | Opening Cr
                              | Activity Dr | Activity Cr
                              | Closing Dr  | Closing Cr

        ``period_start`` defaults to the first day of the as_of_date's month.
        Opening balance = cumulative balance one day before period_start.
        Period activity = net movements from period_start to as_of_date.
        Closing balance = opening + activity.

        Returns the same dict shape as get_trial_balance() but each account
        entry additionally has:
          opening_debit, opening_credit, activity_debit, activity_credit,
          closing_debit, closing_credit
        """
        from datetime import timedelta

        if period_start is None:
            period_start = as_of_date.replace(day=1)

        opening_date = period_start - timedelta(days=1)

        cache_key = (
            f'trial_balance_full_{as_of_date}_{period_start}_'
            f'{branch.id if branch else "all"}'
        )
        cached = cache.get(cache_key)
        if cached:
            return cached

        accounts = (
            Account.objects.filter(is_active=True)
            .only('id', 'code', 'name', 'account_type')
            .order_by('code')
        )

        result_accounts = []
        total_op_d = total_op_c = Decimal('0.00')
        total_ac_d = total_ac_c = Decimal('0.00')
        total_cl_d = total_cl_c = Decimal('0.00')

        for account in accounts:
            # Opening balance (cumulative up to day before period start)
            opening_bal = self.accounting_service.calculate_account_balance(
                account, opening_date, branch
            )

            # Period activity (debits and credits during the period)
            ledger_filter = {
                'account': account,
                'transaction_date__gte': period_start,
                'transaction_date__lte': as_of_date,
            }
            if branch:
                ledger_filter['branch'] = branch

            agg = GeneralLedger.objects.filter(**ledger_filter).aggregate(
                total_debits=Sum('debit_amount'),
                total_credits=Sum('credit_amount'),
            )
            ac_d = agg['total_debits']  or Decimal('0.00')
            ac_c = agg['total_credits'] or Decimal('0.00')

            # Closing balance
            closing_bal = self.accounting_service.calculate_account_balance(
                account, as_of_date, branch
            )

            # Skip zero-movement accounts with zero balance
            if opening_bal == 0 and ac_d == 0 and ac_c == 0 and closing_bal == 0:
                continue

            # Express opening / closing as Dr or Cr per account normal balance
            def _split(balance, acc_type):
                """Return (debit, credit) tuple for a net balance."""
                if acc_type in ('asset', 'expense'):
                    return (balance, Decimal('0.00')) if balance >= 0 else (Decimal('0.00'), abs(balance))
                else:
                    return (Decimal('0.00'), balance) if balance >= 0 else (abs(balance), Decimal('0.00'))

            op_d, op_c = _split(opening_bal, account.account_type)
            cl_d, cl_c = _split(closing_bal, account.account_type)

            result_accounts.append({
                'code': account.code,
                'name': account.name,
                'type': account.account_type,
                'opening_debit':  op_d,
                'opening_credit': op_c,
                'activity_debit':  ac_d,
                'activity_credit': ac_c,
                'closing_debit':  cl_d,
                'closing_credit': cl_c,
                # Legacy keys for backward compatibility
                'debit_balance':  cl_d,
                'credit_balance': cl_c,
            })

            total_op_d += op_d;  total_op_c += op_c
            total_ac_d += ac_d;  total_ac_c += ac_c
            total_cl_d += cl_d;  total_cl_c += cl_c

        balanced = abs(total_cl_d - total_cl_c) < Decimal('0.01')

        report_data = {
            'source': 'accounting_system',
            'generated_at': timezone.now().isoformat(),
            'period': {'start': period_start, 'end': as_of_date},
            'report_date':    as_of_date,
            'period_start':   period_start,
            'branch':         branch.name if branch else 'All Branches',
            'accounts':       result_accounts,
            'totals': {
                'total_debits':   total_cl_d,
                'total_credits':  total_cl_c,
                'balanced':       balanced,
                'opening_debits':  total_op_d,
                'opening_credits': total_op_c,
                'activity_debits':  total_ac_d,
                'activity_credits': total_ac_c,
                'closing_debits':  total_cl_d,
                'closing_credits': total_cl_c,
            },
        }

        cache.set(cache_key, report_data, 3600)
        return report_data

    def generate_income_statement(
        self, start_date: date, end_date: date, branch: Optional = None
    ) -> Dict:
        """
        Generate comprehensive income statement (profit and loss).
        
        Performance optimizations:
        - Uses database aggregation for calculations (Requirement 20.1, 20.2)
        - Uses prefetch_related for reverse foreign key queries (Requirement 20.2)
        - Implements 1-hour caching (Requirement 20.4)
        - Uses only() to limit field retrieval (Requirement 20.1)
        - Uses aggregate() for calculations at database level (Requirement 20.1)

        Args:
            start_date: Start date of the period
            end_date: End date of the period
            branch: Optional Branch to filter by

        Returns:
            dict: Income statement with period, branch, income groups,
                  expense groups, and totals
        """
        cache_key = f"income_statement_{start_date}_{end_date}_{branch.id if branch else 'all'}"
        cached = cache.get(cache_key)
        if cached:
            return cached

        # Get income accounts and their balances using only() to limit fields (Requirement 20.1)
        # Using prefetch_related for ledger_entries optimization (Requirement 20.2)
        income_accounts = Account.objects.filter(
            account_type='income',
            is_active=True
        ).only('id', 'code', 'name', 'subtype').order_by('code')

        # Get expense accounts and their balances using only() to limit fields (Requirement 20.1)
        # Using prefetch_related for ledger_entries optimization (Requirement 20.2)
        expense_accounts = Account.objects.filter(
            account_type='expense',
            is_active=True
        ).only('id', 'code', 'name', 'subtype').order_by('code')

        # Calculate income by categories
        income_groups = self._group_income_accounts(
            income_accounts, start_date, end_date, branch
        )

        # Calculate expenses by categories
        expense_groups = self._group_expense_accounts(
            expense_accounts, start_date, end_date, branch
        )

        # Calculate totals
        gross_income = sum(
            group['total'] for group in income_groups.values()
        )
        total_expenses = sum(
            group['total'] for group in expense_groups.values()
        )
        net_income = gross_income - total_expenses

        report_data = {
            'source': 'accounting_system',
            'generated_at': timezone.now().isoformat(),
            'period': {'start': start_date, 'end': end_date},
            'branch': branch.name if branch else 'All Branches',
            'income': income_groups,
            'expenses': expense_groups,
            'totals': {
                'gross_income': gross_income,
                'total_expenses': total_expenses,
                'net_income': net_income
            }
        }

        # Cache for 1 hour
        cache.set(cache_key, report_data, 3600)

        return report_data

    def generate_balance_sheet(
        self, as_of_date: date, branch: Optional = None
    ) -> Dict:
        """
        Generate statement of financial position (balance sheet).

        Args:
            as_of_date: The date to generate balance sheet as of
            branch: Optional Branch to filter by

        Returns:
            dict: Balance sheet with as_of_date, branch, assets, liabilities,
                  equity, totals, and balanced flag
        """
        cache_key = f"balance_sheet_{as_of_date}_{branch.id if branch else 'all'}"
        cached = cache.get(cache_key)
        if cached:
            return cached

        # Get asset accounts grouped by subtype using only() to limit fields
        asset_accounts = Account.objects.filter(
            account_type='asset',
            is_active=True
        ).only('id', 'code', 'name', 'subtype').order_by('code')

        assets_by_subtype = {
            'current_asset': {'accounts': [], 'total': Decimal('0.00')},
            'loan_portfolio': {'accounts': [], 'total': Decimal('0.00')},
            'fixed_asset': {'accounts': [], 'total': Decimal('0.00')},
            'other_asset': {'accounts': [], 'total': Decimal('0.00')},
        }

        for account in asset_accounts:
            balance = self._calculate_balance(account, as_of_date, branch)
            if balance != Decimal('0.00'):
                subtype = account.subtype or 'other_asset'
                if subtype not in assets_by_subtype:
                    subtype = 'other_asset'
                
                assets_by_subtype[subtype]['accounts'].append({
                    'code': account.code,
                    'name': account.name,
                    'balance': balance
                })
                assets_by_subtype[subtype]['total'] += balance

        # Get liability accounts grouped by subtype using only() to limit fields
        liability_accounts = Account.objects.filter(
            account_type='liability',
            is_active=True
        ).only('id', 'code', 'name', 'subtype').order_by('code')

        liabilities_by_subtype = {
            'current_liability': {'accounts': [], 'total': Decimal('0.00')},
            'client_savings': {'accounts': [], 'total': Decimal('0.00')},
            'borrowings': {'accounts': [], 'total': Decimal('0.00')},
            'other_liability': {'accounts': [], 'total': Decimal('0.00')},
        }

        for account in liability_accounts:
            balance = self._calculate_balance(account, as_of_date, branch)
            if balance != Decimal('0.00'):
                subtype = account.subtype or 'other_liability'
                if subtype not in liabilities_by_subtype:
                    subtype = 'other_liability'
                
                liabilities_by_subtype[subtype]['accounts'].append({
                    'code': account.code,
                    'name': account.name,
                    'balance': balance
                })
                liabilities_by_subtype[subtype]['total'] += balance

        # Get equity accounts using only() to limit fields
        equity_accounts = Account.objects.filter(
            account_type='equity',
            is_active=True
        ).only('id', 'code', 'name').order_by('code')

        equity_list = []
        equity_total = Decimal('0.00')

        for account in equity_accounts:
            balance = self._calculate_balance(account, as_of_date, branch)
            if balance != Decimal('0.00'):
                equity_list.append({
                    'code': account.code,
                    'name': account.name,
                    'balance': balance
                })
                equity_total += balance

        # Get current period net income from income statement
        # Use beginning of year or earliest date as start
        period_start = date(as_of_date.year, 1, 1)
        income_stmt = self.generate_income_statement(period_start, as_of_date, branch)
        net_income = income_stmt['totals']['net_income']

        # Add net income to equity
        if net_income != Decimal('0.00'):
            equity_list.append({
                'code': 'NET_INCOME',
                'name': 'Net Income (Current Period)',
                'balance': net_income
            })
            equity_total += net_income

        # Calculate totals
        total_assets = sum(
            subtype_data['total'] for subtype_data in assets_by_subtype.values()
        )
        total_liabilities = sum(
            subtype_data['total'] for subtype_data in liabilities_by_subtype.values()
        )
        total_equity = equity_total

        # Verify accounting equation (Requirements 5.1, 5.2, 5.3)
        difference = abs(total_assets - (total_liabilities + total_equity))
        balanced = difference < Decimal('0.01')

        if not balanced:
            logging.getLogger(__name__).error(
                "SFP imbalance detected: difference=%s, as_of=%s", difference, as_of_date
            )
            raise FinancialStatementImbalanceError(difference)

        report_data = {
            'source': 'accounting_system',
            'generated_at': timezone.now().isoformat(),
            'as_of_date': as_of_date,
            'branch': branch.name if branch else 'All Branches',
            'assets': assets_by_subtype,
            'liabilities': liabilities_by_subtype,
            'equity': {
                'accounts': equity_list,
                'total': total_equity
            },
            'totals': {
                'total_assets': total_assets,
                'total_liabilities': total_liabilities,
                'total_equity': total_equity,
                'balanced': True,
            },
            'balanced': True,
            'difference': Decimal('0.00'),
        }

        # Cache for 1 hour
        cache.set(cache_key, report_data, 3600)

        return report_data

    def generate_cash_flow_statement(
        self, start_date: date, end_date: date, branch: Optional = None
    ) -> Dict:
        """
        Generate cash flow statement using indirect method.

        Args:
            start_date: Start date of the period
            end_date: End date of the period
            branch: Optional Branch to filter by

        Returns:
            dict: Cash flow statement with period, branch, operating, investing,
                  financing activities, net_change, beginning_cash, ending_cash,
                  and reconciled flag
        """
        cache_key = f"cash_flow_{start_date}_{end_date}_{branch.id if branch else 'all'}"
        cached = cache.get(cache_key)
        if cached:
            return cached

        # Get net income from income statement
        income_stmt = self.generate_income_statement(start_date, end_date, branch)
        net_income = income_stmt['totals']['net_income']

        # Operating Activities - Start with net income
        operating_activities = {
            'net_income': net_income,
            'adjustments': [],
            'working_capital_changes': [],
            'total': net_income
        }

        # Add back non-cash expenses (depreciation, amortization)
        depreciation = self._get_depreciation_expense(start_date, end_date, branch)
        if depreciation != Decimal('0.00'):
            operating_activities['adjustments'].append({
                'description': 'Depreciation and Amortization',
                'amount': depreciation
            })
            operating_activities['total'] += depreciation

        # Calculate working capital changes
        wc_changes = self._calculate_working_capital_changes(
            start_date, end_date, branch
        )
        operating_activities['working_capital_changes'] = wc_changes
        operating_activities['total'] += sum(
            item['amount'] for item in wc_changes
        )
        
        # For microfinance institutions, include loan portfolio changes in operating activities
        loan_portfolio_change = self._get_loan_portfolio_change(start_date, end_date, branch)
        if loan_portfolio_change != Decimal('0.00'):
            operating_activities['working_capital_changes'].append({
                'description': 'Change in Loan Portfolio',
                'amount': loan_portfolio_change
            })
            operating_activities['total'] += loan_portfolio_change

        # Investing Activities
        investing_activities = self._get_fixed_asset_activity(
            start_date, end_date, branch
        )

        # Financing Activities
        financing_activities = self._get_borrowing_activity(
            start_date, end_date, branch
        )

        # Calculate net change in cash
        net_change = (
            operating_activities['total'] +
            investing_activities['total'] +
            financing_activities['total']
        )

        # Get beginning and ending cash balances
        beginning_cash = self._get_cash_balance(start_date, branch, beginning=True)
        ending_cash = self._get_cash_balance(end_date, branch, beginning=False)

        # Verify reconciliation
        calculated_ending = beginning_cash + net_change
        reconciled = (abs(ending_cash - calculated_ending) < Decimal('0.01'))  # Allow for rounding

        report_data = {
            'period': {'start': start_date, 'end': end_date},
            'branch': branch.name if branch else 'All Branches',
            'operating_activities': operating_activities,
            'investing_activities': investing_activities,
            'financing_activities': financing_activities,
            'net_change': net_change,
            'beginning_cash': beginning_cash,
            'ending_cash': ending_cash,
            'calculated_ending_cash': calculated_ending,
            'reconciled': reconciled
        }

        # Cache for 1 hour
        cache.set(cache_key, report_data, 3600)

        return report_data

    def generate_account_analysis(
        self, account: Account, start_date: date, end_date: date
    ) -> Dict:
        """
        Generate detailed account analysis report showing all transactions.

        Args:
            account: The Account instance to analyze
            start_date: Start date of the period
            end_date: End date of the period

        Returns:
            dict: Account analysis with account info, opening_balance,
                  transactions list, totals, and closing_balance
        """
        # Get opening balance (balance before start_date)
        opening_balance = self._calculate_balance_before_date(account, start_date)

        # Get all transactions in the period
        ledger_entries = GeneralLedger.objects.filter(
            account=account,
            transaction_date__gte=start_date,
            transaction_date__lte=end_date
        ).select_related(
            'journal_entry', 'posted_by'
        ).order_by('transaction_date', 'posted_at')

        transactions = []
        running_balance = opening_balance
        total_debits = Decimal('0.00')
        total_credits = Decimal('0.00')

        for entry in ledger_entries:
            # Update running balance
            if account.account_type in ['asset', 'expense']:
                running_balance = running_balance + entry.debit_amount - entry.credit_amount
            else:  # liability, equity, income
                running_balance = running_balance + entry.credit_amount - entry.debit_amount

            transactions.append({
                'date': entry.transaction_date,
                'description': entry.description,
                'reference': entry.reference_number,
                'debit': entry.debit_amount,
                'credit': entry.credit_amount,
                'balance': running_balance,
                'posted_by': entry.posted_by.get_full_name() if entry.posted_by else 'System',
                'posted_at': entry.posted_at
            })

            total_debits += entry.debit_amount
            total_credits += entry.credit_amount

        closing_balance = running_balance

        report_data = {
            'account': {
                'code': account.code,
                'name': account.name,
                'type': account.account_type,
                'description': account.description
            },
            'period': {'start': start_date, 'end': end_date},
            'opening_balance': opening_balance,
            'transactions': transactions,
            'totals': {
                'total_debits': total_debits,
                'total_credits': total_credits,
                'net_change': total_debits - total_credits if account.account_type in ['asset', 'expense'] else total_credits - total_debits
            },
            'closing_balance': closing_balance
        }

        return report_data

    # Helper methods for report calculations

    def _calculate_balance(
        self, account: Account, as_of_date: date, branch: Optional = None
    ) -> Decimal:
        """
        Calculate account balance as of a specific date.
        Delegates to AccountingService for consistency.
        """
        return self.accounting_service.calculate_account_balance(
            account, as_of_date, branch
        )

    def _calculate_balance_before_date(
        self, account: Account, before_date: date, branch: Optional = None
    ) -> Decimal:
        """
        Calculate account balance before a specific date (for opening balances).
        """
        from datetime import timedelta
        day_before = before_date - timedelta(days=1)
        return self._calculate_balance(account, day_before, branch)

    def _group_income_accounts(
        self, income_accounts, start_date: date, end_date: date, branch: Optional
    ) -> Dict:
        """
        Group income accounts by category and calculate period activity.
        """
        groups = {
            'interest_income': {'accounts': [], 'total': Decimal('0.00')},
            'fee_income': {'accounts': [], 'total': Decimal('0.00')},
            'other_income': {'accounts': [], 'total': Decimal('0.00')},
        }

        for account in income_accounts:
            # Calculate period activity (credits - debits for income)
            period_activity = self._calculate_period_activity(
                account, start_date, end_date, branch
            )

            if period_activity != Decimal('0.00'):
                # Categorize by priority: subtype → code prefix → name keyword → catch-all
                subtype = (getattr(account, 'subtype', None) or '').lower()
                code = str(account.code or '')
                name_lower = account.name.lower()

                if subtype == 'interest_income':
                    category = 'interest_income'
                elif subtype in ('fee_income', 'commission_income'):
                    category = 'fee_income'
                elif subtype and subtype not in ('interest_income', 'fee_income', 'commission_income'):
                    # Any other explicit subtype → other_income
                    category = 'other_income'
                elif code.startswith('401'):
                    category = 'interest_income'
                elif code.startswith('402'):
                    category = 'fee_income'
                elif code.startswith('40'):
                    # 403x and above → other_income
                    category = 'other_income'
                elif 'interest' in name_lower:
                    category = 'interest_income'
                elif 'fee' in name_lower or 'commission' in name_lower:
                    category = 'fee_income'
                else:
                    category = 'other_income'

                groups[category]['accounts'].append({
                    'code': account.code,
                    'name': account.name,
                    'amount': period_activity
                })
                groups[category]['total'] += period_activity

        return groups

    def _group_expense_accounts(
        self, expense_accounts, start_date: date, end_date: date, branch: Optional
    ) -> Dict:
        """
        Group expense accounts by category and calculate period activity.

        6-category taxonomy (Requirements 3.2, 3.3, 3.4):
          interest_expense, loan_loss_provision, staff_costs,
          administrative_expenses, depreciation, other_expenses

        Classification priority:
          1. account.subtype  →  explicit mapping
          2. account code prefix  →  501x / 502x / 503x / 504x / 505x
          3. name keyword fallback
          4. catch-all: other_expenses

        Tax accounts (codes 5070–5071) are excluded here; they are displayed
        separately as a taxation section after operating profit (Requirement 3.4).
        """
        groups = {
            'interest_expense':       {'accounts': [], 'total': Decimal('0.00')},
            'loan_loss_provision':    {'accounts': [], 'total': Decimal('0.00')},
            'staff_costs':            {'accounts': [], 'total': Decimal('0.00')},
            'administrative_expenses': {'accounts': [], 'total': Decimal('0.00')},
            'depreciation':           {'accounts': [], 'total': Decimal('0.00')},
            'other_expenses':         {'accounts': [], 'total': Decimal('0.00')},
        }

        # Tax account codes that are excluded from expense grouping (shown separately)
        TAX_CODES = {'5070', '5071'}

        for account in expense_accounts:
            code = str(account.code or '').strip()

            # Exclude tax accounts — rendered separately after operating profit
            if code in TAX_CODES:
                continue

            # Calculate period activity (debits − credits for expense accounts)
            period_activity = self._calculate_period_activity(
                account, start_date, end_date, branch
            )

            if period_activity == Decimal('0.00'):
                continue

            # ── Priority 1: explicit subtype ──────────────────────────────
            subtype = (getattr(account, 'subtype', None) or '').lower().strip()
            name_lower = account.name.lower()

            if subtype == 'interest_expense':
                category = 'interest_expense'
            elif subtype in ('loan_loss_provision', 'ecl'):
                category = 'loan_loss_provision'
            elif subtype == 'staff_costs':
                category = 'staff_costs'
            elif subtype == 'administrative_expense':
                category = 'administrative_expenses'
            elif subtype == 'depreciation':
                category = 'depreciation'
            elif subtype and subtype not in (
                'interest_expense', 'loan_loss_provision', 'ecl',
                'staff_costs', 'administrative_expense', 'depreciation',
            ):
                # Any other explicit subtype (e.g. operating_expense, transport,
                # financial_expenses, loan_portfolio …) falls to code-prefix check
                # so we don't force it to other_expenses prematurely.
                category = None

            else:
                category = None

            # ── Priority 2: account code prefix ──────────────────────────
            if category is None:
                if code.startswith('501'):
                    category = 'interest_expense'
                elif code.startswith('502'):
                    category = 'loan_loss_provision'
                elif code.startswith('503'):
                    category = 'staff_costs'
                elif code.startswith('504'):
                    category = 'administrative_expenses'
                elif code.startswith('505'):
                    category = 'depreciation'
                else:
                    category = None  # proceed to name keywords

            # ── Priority 3: name keyword fallback ─────────────────────────
            if category is None:
                if 'interest' in name_lower:
                    category = 'interest_expense'
                elif 'provision' in name_lower or 'ecl' in name_lower or (
                    'loss' in name_lower and 'loan' in name_lower
                ):
                    category = 'loan_loss_provision'
                elif 'staff' in name_lower or 'salary' in name_lower or 'wages' in name_lower:
                    category = 'staff_costs'
                elif (
                    'admin' in name_lower or 'rent' in name_lower
                    or 'utilities' in name_lower
                ):
                    category = 'administrative_expenses'
                elif (
                    'depreciation' in name_lower
                    or 'amortisation' in name_lower
                    or 'amortization' in name_lower
                ):
                    category = 'depreciation'
                else:
                    # ── Priority 4: catch-all ─────────────────────────────
                    category = 'other_expenses'

            groups[category]['accounts'].append({
                'account_code': account.code,
                'code': account.code,
                'name': account.name,
                'amount': period_activity,
            })
            groups[category]['total'] += period_activity

        return groups

    def _calculate_period_activity(
        self, account: Account, start_date: date, end_date: date, branch: Optional
    ) -> Decimal:
        """
        Calculate net activity for an account during a period.

        Delegates exclusively to AccountingService.calculate_account_balance so
        the General Ledger remains the single source of truth for all reports.
        Period activity = closing_balance(end_date) - opening_balance(start_date - 1 day).

        The sign is already correct because calculate_account_balance returns the
        natural balance for the account type:
          - Assets/Expenses: positive = debit balance
          - Liabilities/Equity/Income: positive = credit balance

        Requirements: 2.2, 9.1, 9.2
        """
        from datetime import timedelta

        closing = self.accounting_service.calculate_account_balance(
            account, end_date, branch
        )
        opening = self.accounting_service.calculate_account_balance(
            account, start_date - timedelta(days=1), branch
        )
        return closing - opening

    def _calculate_working_capital_changes(
        self, start_date: date, end_date: date, branch: Optional
    ) -> List[Dict]:
        """
        Calculate changes in working capital accounts for cash flow.
        """
        changes = []

        # Get current asset accounts (excluding cash)
        current_assets = Account.objects.filter(
            account_type='asset',
            subtype='current_asset',
            is_active=True
        ).exclude(
            Q(name__icontains='cash') | Q(name__icontains='bank')
        )

        for account in current_assets:
            change = self._calculate_period_activity(account, start_date, end_date, branch)
            if change != Decimal('0.00'):
                # Increase in asset is use of cash (negative for cash flow)
                changes.append({
                    'description': f'Change in {account.name}',
                    'amount': -change
                })

        # Get current liability accounts
        current_liabilities = Account.objects.filter(
            account_type='liability',
            subtype='current_liability',
            is_active=True
        )

        for account in current_liabilities:
            change = self._calculate_period_activity(account, start_date, end_date, branch)
            if change != Decimal('0.00'):
                # Increase in liability is source of cash (positive for cash flow)
                changes.append({
                    'description': f'Change in {account.name}',
                    'amount': change
                })

        return changes

    def _get_fixed_asset_activity(
        self, start_date: date, end_date: date, branch: Optional
    ) -> Dict:
        """
        Calculate fixed asset purchases and sales for investing activities.
        For cash flow, we need actual cash transactions, not net changes that include depreciation.
        """
        fixed_assets = Account.objects.filter(
            account_type='asset',
            subtype='fixed_asset',
            is_active=True
        )

        items = []
        total = Decimal('0.00')

        for account in fixed_assets:
            # Get gross purchases (debits) and sales (credits) separately
            ledger_filter = {
                'account': account,
                'transaction_date__gte': start_date,
                'transaction_date__lte': end_date
            }
            if branch:
                ledger_filter['branch'] = branch
            
            result = GeneralLedger.objects.filter(**ledger_filter).aggregate(
                total_debits=Sum('debit_amount'),
                total_credits=Sum('credit_amount')
            )
            
            purchases = result['total_debits'] or Decimal('0.00')
            sales_and_depreciation = result['total_credits'] or Decimal('0.00')
            
            # For cash flow, purchases are cash outflows (negative)
            if purchases > Decimal('0.00'):
                items.append({
                    'description': f'Purchase of {account.name}',
                    'amount': -purchases
                })
                total -= purchases
            
            # Sales are cash inflows (positive) - but exclude depreciation
            # In practice, distinguishing sales from depreciation is difficult without
            # checking the journal entry descriptions or linked accounts.
            # For now, we'll assume credits to fixed assets during the period are depreciation
            # (which are already added back in operating activities) unless they're actual sales.
            # This is a simplification that works for most cases.

        return {
            'items': items,
            'total': total
        }

    def _get_borrowing_activity(
        self, start_date: date, end_date: date, branch: Optional
    ) -> Dict:
        """
        Calculate borrowing and equity changes for financing activities.
        """
        # Get borrowing accounts
        borrowings = Account.objects.filter(
            account_type='liability',
            subtype='borrowings',
            is_active=True
        )

        # Get equity accounts
        equity = Account.objects.filter(
            account_type='equity',
            is_active=True
        )

        items = []
        total = Decimal('0.00')

        for account in borrowings:
            change = self._calculate_period_activity(account, start_date, end_date, branch)
            if change != Decimal('0.00'):
                # Positive change (credit) = new borrowing (source of cash)
                # Negative change (debit) = repayment (use of cash)
                items.append({
                    'description': f'{"New" if change > 0 else "Repayment of"} {account.name}',
                    'amount': change
                })
                total += change

        for account in equity:
            change = self._calculate_period_activity(account, start_date, end_date, branch)
            if change != Decimal('0.00'):
                # Positive change (credit) = capital contribution (source of cash)
                # Negative change (debit) = dividend/withdrawal (use of cash)
                items.append({
                    'description': f'{"Capital contribution" if change > 0 else "Withdrawal from"} {account.name}',
                    'amount': change
                })
                total += change

        return {
            'items': items,
            'total': total
        }

    def _get_depreciation_expense(
        self, start_date: date, end_date: date, branch: Optional
    ) -> Decimal:
        """
        Get total depreciation expense for the period.
        """
        # Find depreciation expense accounts
        depreciation_accounts = Account.objects.filter(
            account_type='expense',
            is_active=True
        ).filter(
            Q(name__icontains='depreciation') | Q(name__icontains='amortization')
        )

        total = Decimal('0.00')
        for account in depreciation_accounts:
            total += self._calculate_period_activity(account, start_date, end_date, branch)

        return total

    def _get_cash_balance(
        self, as_of_date: date, branch: Optional, beginning: bool = False
    ) -> Decimal:
        """
        Get total cash balance across all cash/bank accounts.
        """
        from datetime import timedelta

        # If getting beginning balance, use day before
        if beginning:
            as_of_date = as_of_date - timedelta(days=1)

        # Get all cash and bank accounts
        cash_accounts = Account.objects.filter(
            account_type='asset',
            is_active=True
        ).filter(
            Q(name__icontains='cash') | 
            Q(name__icontains='bank') | 
            Q(name__icontains='m-pesa') |
            Q(name__icontains='mpesa')
        )

        total_cash = Decimal('0.00')
        for account in cash_accounts:
            total_cash += self._calculate_balance(account, as_of_date, branch)

        return total_cash

    def _get_loan_portfolio_change(
        self, start_date: date, end_date: date, branch: Optional
    ) -> Decimal:
        """
        Calculate change in loan portfolio for operating activities.
        For microfinance institutions, loans are part of operating activities.
        Increase in loans (disbursements) = use of cash (negative)
        Decrease in loans (repayments) = source of cash (positive)
        """
        loan_portfolio_accounts = Account.objects.filter(
            account_type='asset',
            subtype='loan_portfolio',
            is_active=True
        )
        
        total_change = Decimal('0.00')
        for account in loan_portfolio_accounts:
            change = self._calculate_period_activity(account, start_date, end_date, branch)
            # Increase in loan portfolio (asset increase) is use of cash, so negative
            total_change -= change
        
        return total_change
