"""
Transaction type analysis module.
"""

import pandas as pd
from app.config import MODEL_CONFIG

class TransactionTypeAnalyzer:
    def __init__(self, df):
        self.df = df
        self.trx_df = None

    def flag_salary_type_transactions(self):
        """Flag transactions that match salary criteria based on type and subtype."""
        self.df['is_salary_type'] = (
            ((self.df['trx_type'] == 'T') | (self.df['trx_type'] == 'C')) &
            ((self.df['trx_subtype'] == 'BI') | (self.df['trx_subtype'] == 'I') |
             (self.df['trx_subtype'] == 'BS') | (self.df['trx_subtype'] == 'CI')) &
            (self.df['initiated_by'] == 'C') &
            (self.df['amount'] > 0)
        )
        
        self.trx_df = self.df.copy()
        return self.df

    def is_salary_earner_by_type(self, group, min_transactions=None, threshold=None):
        """Determine if an account likely belongs to a salary earner."""
        if min_transactions is None:
            min_transactions = MODEL_CONFIG['min_transactions']
        if threshold is None:
            threshold = MODEL_CONFIG['threshold']

        if len(group) < min_transactions:
            return False
        valid_ratio = group['is_salary_type'].mean()
        return valid_ratio >= threshold

    def get_salary_type_data(self):
        """Get transactions identified as salary type."""
        if self.trx_df is None:
            self.flag_salary_type_transactions()
        
        return self.trx_df[self.trx_df['is_salary_type']] 