import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import random

def create_sample_hims_data():
    """Create sample HIMS collection report data"""
    
    # Generate sample data for 7 days
    dates = [(datetime.now() - timedelta(days=i)).strftime('%Y-%m-%d') for i in range(7)]
    
    locations = [
        'SRIKARA MAIN',
        'SRIKARA BRANCH 1', 
        'SRIKARA BRANCH 2',
        'SRIKARA BRANCH 3',
        'SRIKARA BRANCH 4',
        'SRIKARA BRANCH 5',
        'SRIKARA BRANCH 6'
    ]
    
    payment_modes = ['Credit Card', 'Debit Card', 'UPI', 'eWallet', 'Net Banking']
    
    data = []
    for date in dates:
        for location in locations:
            for payment_mode in payment_modes:
                # Generate random transaction data
                txn_count = random.randint(10, 50)
                avg_amount = random.randint(500, 2000)
                total_amount = txn_count * avg_amount + random.randint(-5000, 5000)
                
                for i in range(txn_count):
                    data.append({
                        'Bill Dt': date,
                        'Unit/Location': location,
                        'Payment Mode Name': payment_mode,
                        'Amount': random.randint(100, 3000),
                        'Umr No': f'UMR{random.randint(100000, 999999)}',
                        'Patient Name': f'Patient{random.randint(1000, 9999)}',
                        'MomentsPay Matched': random.choice(['YES', 'NO'])
                    })
    
    df = pd.DataFrame(data)
    df.to_excel('SrikaraintegratedHIS.xlsx', index=False)
    print(f"Sample HIMS data created: {len(df)} records")
    return df

def create_sample_axis_data():
    """Create sample Axis bank settlement data"""
    
    dates = [(datetime.now() - timedelta(days=i)).strftime('%Y-%m-%d') for i in range(7)]
    
    data = []
    for date in dates:
        # Create multiple settlement records per day
        for i in range(random.randint(50, 150)):
            gross_amount = random.randint(500, 3000)
            mdr_rate = random.uniform(0.015, 0.025)  # 1.5% to 2.5%
            mdr = gross_amount * mdr_rate
            gst_rate = 0.18  # 18% GST on MDR
            gst = mdr * gst_rate
            emi = random.choice([0, 0, 0, random.randint(10, 50)])  # EMI charges occasionally
            
            data.append({
                'PROCESS_DATE': date,
                'GROSS_AMT': gross_amount,
                'MDR': mdr,
                'GST_AMT': gst,
                'EMI_AMT': emi,
                'NET_AMT': gross_amount - mdr - gst - emi,
                'CARD_NO': f'****{random.randint(1000, 9999)}',
                'transaction_id': f'TXN{random.randint(100000, 999999)}',
                'AUTH_AMOUNT': gross_amount
            })
    
    df = pd.DataFrame(data)
    df.to_excel('MomentsPaySrikara_Bank_new.xlsx', index=False)
    print(f"Sample Axis bank data created: {len(df)} records")
    return df

def create_sample_paytm_data():
    """Create sample Paytm settlement data"""
    
    dates = [(datetime.now() - timedelta(days=i)).strftime('%Y-%m-%d') for i in range(7)]
    
    data = []
    for date in dates:
        # Create multiple settlement records per day
        for i in range(random.randint(30, 100)):
            amount = random.randint(100, 2000)
            commission_rate = random.uniform(0.01, 0.02)  # 1% to 2%
            commission = amount * commission_rate
            gst_rate = 0.18  # 18% GST on commission
            gst = commission * gst_rate
            
            data.append({
                'date_added': date,
                'total_amount': amount,
                'commission': commission,
                'gst': gst,
                'net_amount': amount - commission - gst,
                'uhid': f'UHID{random.randint(10000, 99999)}',
                'email': f'patient{random.randint(1000, 9999)}@example.com',
                'payment_mode': random.choice(['UPI', 'Wallet', 'Net Banking'])
            })
    
    df = pd.DataFrame(data)
    df.to_csv('Srikaramomentpay06-06.csv', index=False)
    print(f"Sample Paytm data created: {len(df)} records")
    return df

def create_sample_data():
    """Create all sample data files"""
    
    print("Creating sample data files for testing...")
    print("=" * 50)
    
    # Create sample data
    hims_df = create_sample_hims_data()
    axis_df = create_sample_axis_data()
    paytm_df = create_sample_paytm_data()
    
    print("=" * 50)
    print("Sample data creation complete!")
    print()
    print("Files created:")
    print("1. SrikaraintegratedHIS.xlsx - HIMS collection report")
    print("2. MomentsPaySrikara_Bank_new.xlsx - Axis bank settlement")
    print("3. Srikaramomentpay06-06.csv - Paytm settlement")
    print()
    print("You can now run the settlement comparison script:")
    print("python SrikaraSettlementMain.py")
    
    return hims_df, axis_df, paytm_df

if __name__ == "__main__":
    create_sample_data()