#!/usr/bin/env python3
"""
Data models for RTGS 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 RTGSInwardRecord:
    """Represents a parsed NEFT Inward transaction mapped to DB columns."""

    txnind: str                  # VARCHAR2(2), default "CR"
    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)
    remitter_detail: str         # VARCHAR2(400)
    remitter_info: str           # VARCHAR2(400)
    recvr_acct_no: str           # VARCHAR2(400)
    recvr_acct_name: str         # VARCHAR2(400)
    status: str                  # VARCHAR2(400)
    reject_code: str             # VARCHAR2(400)              
    benef_address: str           # VARCHAR2(400)
    msg_type: str                # VARCHAR2(400)
    bank_code: str
    creditor_amt: 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,
            "TXN_DATE": self.txn_date,
            "AMOUNT": self.txn_amt,
            "SENDER_IFSC": self.sender_ifsc,
            "RECIEVER_IFSC": self.reciever_ifsc,
            "REMMITER_ACCT": self.sender_acct_no,
            "REMMITER_NAME": self.sender_acct_name,
            "REMMITER_ADDRS": self.remitter_detail,
            "REMMITER_INFO": self.remitter_info,
            "BENF_ACCT_NO": self.recvr_acct_no,
            "BENF_NAME": self.recvr_acct_name,
            "STATUS": self.status,
            "REJECT_CODE": self.reject_code,            
            "BENF_ADDRS": self.benef_address,
            "MSG_TYP": self.msg_type,
            "CREDITOR_AMT": self.creditor_amt,
        }


@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(),
        }
