#!/usr/bin/env python3
"""
Data models for ACH file processing.
Represents database records and transactions.
"""

from dataclasses import dataclass, asdict
from datetime import date, datetime
from decimal import Decimal
from typing import Optional


@dataclass
class NEFTOutwardRecord:
    """Represents a parsed NEFT Inward transaction mapped to DB columns."""

    txnind: str                  # VARCHAR2(2), default "DR"
    jrnl_id: str                 # VARCHAR2(4000), NOT NULL
    ref_no: str                  # VARCHAR2(400), NOT NULL
    txn_date: str                # VARCHAR2(100), NOT NULL
    txn_amt: Optional[Decimal]   # NUMBER(17,2)
    sender_ifsc: str             # VARCHAR2(400)
    reciever_ifsc: str           # VARCHAR2(400)
    sender_acct_no: str          # VARCHAR2(400)
    sender_acct_name: str        # VARCHAR2(400)    
    recvr_acct_no: str           # VARCHAR2(400)
    recvr_acct_name: str         # VARCHAR2(400)                      
    reject_code: str             # VARCHAR2(400)
    reject_reason: str           # VARCHAR2(400)
    benef_address: str           # VARCHAR2(400)
    msg_type: str                # VARCHAR2(400)
    bank_code: str
    sender_to_reciver_info: str

    def to_dict(self):
        """Convert to dictionary for DB insertion."""
        return {
            "TXNIND": self.txnind,
            "BANKCODE": self.bank_code,
            "JRNL_ID": self.jrnl_id,
            "REF_NO": self.ref_no,
            "TRAN_DATE": self.txn_date,
            "TXN_AMT": self.txn_amt,
            "SENDER_IFSC": self.sender_ifsc,
            "RECIEVER_IFSC": self.reciever_ifsc,
            "SENDER_ACCT_NO": self.sender_acct_no,
            "SENDER_NAME": self.sender_acct_name,            
            "RECVR_ACCT_NO": self.recvr_acct_no,
            "RECIEVER_NAME": self.recvr_acct_name,            
            "REJECT_CODE": self.reject_code,
            "REJECT_REASON": self.reject_reason,
            "BENEFICIARY_ADDRESS": self.benef_address,
            "MSG_TYPE": self.msg_type,
            "SENDER_TO_RECIVER_INFO": self.sender_to_reciver_info,
        }


@dataclass
class ProcessedFile:
    """Represents a processed file record for ach_processed_files table."""
    filename: str
    bankcode: str
    file_path: str
    transaction_count: int
    status: str = 'SUCCESS'
    error_message: Optional[str] = None
    processed_at: Optional[datetime] = None

    def to_dict(self):
        """Convert to dictionary for database insertion."""
        return {
            'filename': self.filename,
            'bankcode': self.bankcode,
            'file_path': self.file_path,
            'transaction_count': self.transaction_count,
            'status': self.status,
            'error_message': self.error_message,
            'processed_at': self.processed_at or datetime.now(),
        }
