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67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
"""
Payment App Statement Generator for Phase 3.

Generates synthetic training data for PhonePe, GPay, and Paytm
statement formats with proper prefixes and entity labeling.

Supported Apps:
    - PhonePe: [PHONEPE] prefix
    - GPay: [GPAY] prefix  
    - Paytm: [PAYTM] prefix

Example:
    >>> from scripts.generate_payment_app_data import generate_all
    >>> result = generate_all(samples_per_app=300)

Author: Ranjit Behera
"""

import json
import random
from datetime import datetime, timedelta
from pathlib import Path
from typing import List, Dict, Any, Tuple

# Seed for reproducibility
random.seed(42)

# PhonePe statement formats
PHONEPE_FORMATS = [
    # Transaction history format
    "{date} | {type_text} | {merchant} | ₹{amount} | {status}",
    "{date} {time} | {merchant} | {type_text} Rs.{amount} | Txn ID: {ref}",
    "PhonePe: {type_text} of ₹{amount} to {merchant} on {date}. UPI Ref: {ref}",
    "{date} - {merchant}@ybl - ₹{amount} - {status} - Ref: {ref}",
    "Transaction: {type_text} | Amount: ₹{amount} | To: {merchant} | {date}",
    "{type_text}: ₹{amount} | {merchant} | {date} {time} | ID: {ref}",
]

# GPay statement formats
GPAY_FORMATS = [
    # Google Pay export format
    "{date},{merchant},{amount},{type_text},{status},{upi_id},{ref}",
    "Google Pay: {type_text} of ₹{amount} to {merchant}. {date}. Ref {ref}",
    "{date} | {merchant} | ₹{amount} {type_text} | UPI: {upi_id} | {ref}",
    "You {action} ₹{amount} {direction} {merchant}. {date}. UPI Ref: {ref}. -Google Pay",
    "GPay Transaction: {date} | {merchant} | {type_text} ₹{amount} | Ref: {ref}",
    "{date} {time} - {type_text} - {merchant} - Rs {amount} - {ref}",
]

# Paytm statement formats
PAYTM_FORMATS = [
    # Paytm history format
    "{date} | {merchant} | {type_text} | ₹{amount} | {wallet_balance}",
    "Paytm: {type_text} of Rs.{amount} to {merchant}. {date}. Order ID: {ref}",
    "{date} {time} | {type_text} ₹{amount} | {merchant} | Paytm | Ref: {ref}",
    "You {action} Rs.{amount} to {merchant} using Paytm on {date}. ID: {ref}",
    "Transaction: {date} | {merchant} | Rs {amount} | Type: {type_text} | {status}",
    "Paytm Wallet: {type_text} Rs.{amount} | {merchant} | Balance: ₹{wallet_balance} | {date}",
]

# Merchants by category
MERCHANTS_BY_CATEGORY = {
    "food": [
        "Swiggy", "Zomato", "Dominos", "McDonalds", "KFC", "Pizza Hut",
        "Burger King", "Starbucks", "Cafe Coffee Day", "Subway",
        "Behrouz Biryani", "Faasos", "Box8", "EatFit", "Haldirams"
    ],
    "shopping": [
        "Amazon", "Flipkart", "Myntra", "Ajio", "Nykaa", "Meesho",
        "Snapdeal", "Shopclues", "Tata Cliq", "FirstCry",
        "Bewakoof", "Urbanic", "Shein", "H&M", "Zara"
    ],
    "grocery": [
        "BigBasket", "Zepto", "Blinkit", "Dunzo", "JioMart",
        "Amazon Fresh", "Swiggy Instamart", "DMart Ready",
        "Grofers", "Nature's Basket", "Spencer's", "More Supermarket"
    ],
    "transport": [
        "Uber", "Ola", "Rapido", "BluSmart", "IRCTC",
        "RedBus", "AbhiBus", "MakeMyTrip", "Goibibo", "Yatra",
        "Cleartrip", "EaseMyTrip", "IndiGo", "SpiceJet", "Air India"
    ],
    "bills": [
        "Airtel", "Jio", "Vodafone Idea", "BSNL", "ACT Fibernet",
        "Tata Power", "Adani Electricity", "MSEB", "BESCOM",
        "Mahanagar Gas", "Indraprastha Gas", "Gujarat Gas"
    ],
    "entertainment": [
        "Netflix", "Amazon Prime", "Hotstar", "Zee5", "SonyLiv",
        "Spotify", "Gaana", "JioSaavn", "Apple Music", "YouTube Premium",
        "BookMyShow", "PVR", "INOX", "Carnival Cinemas"
    ],
    "recharge": [
        "Airtel Prepaid", "Jio Prepaid", "Vi Prepaid", "BSNL Mobile",
        "Airtel DTH", "Tata Play", "Dish TV", "d2h", "Sun Direct"
    ],
    "transfer": [
        "Self Transfer", "Rahul Kumar", "Priya Sharma", "Amit Singh",
        "Neha Patel", "Vikram Reddy", "Bank Transfer", "UPI Transfer"
    ],
    "investment": [
        "Zerodha", "Groww", "Upstox", "Angel One", "5paisa",
        "Coin by Zerodha", "Kuvera", "INDmoney", "ET Money",
        "Paytm Money", "PhonePe Mutual Funds", "Scripbox"
    ],
    "insurance": [
        "LIC", "HDFC Life", "ICICI Pru", "SBI Life", "Max Life",
        "Bajaj Allianz", "Tata AIA", "PolicyBazaar", "Digit Insurance"
    ],
}

# UPI IDs by app
UPI_SUFFIXES = {
    "phonepe": ["@ybl", "@ibl", "@axl"],
    "gpay": ["@okaxis", "@okhdfcbank", "@okicici", "@oksbi"],
    "paytm": ["@paytm", "@pthdfc", "@ptaxis", "@ptsbi"],
}

# Status options
STATUSES = ["Success", "Successful", "Completed", "Done", "Processed"]
FAILED_STATUSES = ["Failed", "Declined", "Cancelled", "Pending"]


def random_date(days_back: int = 180) -> Tuple[str, str]:
    """Generate random date and time."""
    days_ago = random.randint(0, days_back)
    dt = datetime.now() - timedelta(days=days_ago)
    
    date_formats = [
        "%d-%m-%Y", "%d/%m/%Y", "%d %b %Y", "%d %b, %Y",
        "%Y-%m-%d", "%d-%m-%y", "%b %d, %Y"
    ]
    time_formats = ["%H:%M", "%I:%M %p", "%H:%M:%S"]
    
    date_str = dt.strftime(random.choice(date_formats))
    time_str = dt.strftime(random.choice(time_formats))
    
    return date_str, time_str


def random_amount(category: str = None) -> str:
    """Generate random amount based on category."""
    ranges = {
        "food": (50, 2000),
        "shopping": (200, 15000),
        "grocery": (100, 5000),
        "transport": (50, 5000),
        "bills": (200, 10000),
        "entertainment": (99, 1500),
        "recharge": (100, 2000),
        "transfer": (500, 50000),
        "investment": (500, 50000),
        "insurance": (1000, 30000),
    }
    
    min_val, max_val = ranges.get(category, (50, 10000))
    amount = random.uniform(min_val, max_val)
    
    if random.random() < 0.4:
        return f"{amount:,.2f}"
    else:
        return f"{int(amount):,}"


def random_ref(prefix: str = "") -> str:
    """Generate random reference number."""
    length = random.choice([10, 12, 14, 16])
    ref = ''.join(str(random.randint(0, 9)) for _ in range(length))
    return f"{prefix}{ref}" if prefix else ref


def random_wallet_balance() -> str:
    """Generate random wallet balance."""
    balance = random.uniform(100, 10000)
    return f"{balance:,.2f}"


def generate_phonepe_row() -> Dict[str, Any]:
    """Generate a PhonePe statement row."""
    category = random.choice(list(MERCHANTS_BY_CATEGORY.keys()))
    merchant = random.choice(MERCHANTS_BY_CATEGORY[category])
    is_credit = category == "transfer" and random.random() < 0.3
    
    date_str, time_str = random_date()
    amount = random_amount(category)
    ref = random_ref()
    status = random.choice(STATUSES)
    upi_suffix = random.choice(UPI_SUFFIXES["phonepe"])
    
    type_text = "Received" if is_credit else "Paid"
    
    template = random.choice(PHONEPE_FORMATS)
    raw_text = template.format(
        date=date_str,
        time=time_str,
        merchant=merchant,
        amount=amount,
        type_text=type_text,
        status=status,
        ref=ref,
        upi_id=f"{merchant.lower().replace(' ', '')}{upi_suffix}"
    )
    
    entities = {
        "date": date_str,
        "amount": amount.replace(",", ""),
        "type": "credit" if is_credit else "debit",
        "merchant": merchant.lower(),
        "category": category,
        "reference": ref,
        "status": status.lower(),
    }
    
    return {
        "app": "phonepe",
        "prefix": "[PHONEPE]",
        "raw_text": raw_text,
        "labeled": True,
        "entities": entities
    }


def generate_gpay_row() -> Dict[str, Any]:
    """Generate a GPay statement row."""
    category = random.choice(list(MERCHANTS_BY_CATEGORY.keys()))
    merchant = random.choice(MERCHANTS_BY_CATEGORY[category])
    is_credit = category == "transfer" and random.random() < 0.3
    
    date_str, time_str = random_date()
    amount = random_amount(category)
    ref = random_ref()
    status = random.choice(STATUSES)
    upi_suffix = random.choice(UPI_SUFFIXES["gpay"])
    upi_id = f"{merchant.lower().replace(' ', '')}{upi_suffix}"
    
    type_text = "Credit" if is_credit else "Debit"
    action = "received" if is_credit else "paid"
    direction = "from" if is_credit else "to"
    
    template = random.choice(GPAY_FORMATS)
    raw_text = template.format(
        date=date_str,
        time=time_str,
        merchant=merchant,
        amount=amount,
        type_text=type_text,
        status=status,
        ref=ref,
        upi_id=upi_id,
        action=action,
        direction=direction
    )
    
    entities = {
        "date": date_str,
        "amount": amount.replace(",", ""),
        "type": "credit" if is_credit else "debit",
        "merchant": merchant.lower(),
        "category": category,
        "reference": ref,
    }
    
    return {
        "app": "gpay",
        "prefix": "[GPAY]",
        "raw_text": raw_text,
        "labeled": True,
        "entities": entities
    }


def generate_paytm_row() -> Dict[str, Any]:
    """Generate a Paytm statement row."""
    category = random.choice(list(MERCHANTS_BY_CATEGORY.keys()))
    merchant = random.choice(MERCHANTS_BY_CATEGORY[category])
    is_credit = category == "transfer" and random.random() < 0.3
    
    date_str, time_str = random_date()
    amount = random_amount(category)
    ref = random_ref("ORD")
    status = random.choice(STATUSES)
    wallet_balance = random_wallet_balance()
    
    type_text = "Credit" if is_credit else "Debit"
    action = "received" if is_credit else "sent"
    
    template = random.choice(PAYTM_FORMATS)
    raw_text = template.format(
        date=date_str,
        time=time_str,
        merchant=merchant,
        amount=amount,
        type_text=type_text,
        status=status,
        ref=ref,
        wallet_balance=wallet_balance,
        action=action
    )
    
    entities = {
        "date": date_str,
        "amount": amount.replace(",", ""),
        "type": "credit" if is_credit else "debit",
        "merchant": merchant.lower(),
        "category": category,
        "reference": ref,
    }
    
    if "Wallet" in template:
        entities["wallet_balance"] = wallet_balance.replace(",", "")
    
    return {
        "app": "paytm",
        "prefix": "[PAYTM]",
        "raw_text": raw_text,
        "labeled": True,
        "entities": entities
    }


def generate_all(
    samples_per_app: int = 300,
    output_dir: str = "data/training"
) -> Dict[str, Any]:
    """
    Generate complete training dataset for all payment apps.
    
    Args:
        samples_per_app: Number of samples per app.
        output_dir: Output directory for JSONL files.
    
    Returns:
        Summary dictionary with stats.
    """
    generators = {
        "phonepe": generate_phonepe_row,
        "gpay": generate_gpay_row,
        "paytm": generate_paytm_row,
    }
    
    all_samples = []
    
    for app, generator in generators.items():
        for _ in range(samples_per_app):
            sample = generator()
            all_samples.append(sample)
    
    # Shuffle
    random.shuffle(all_samples)
    
    # Convert to training format with app-specific prefix
    training_data = []
    for sample in all_samples:
        prefix = sample["prefix"]
        prompt = f"{prefix} Extract financial entities from this payment app statement:\n\n{sample['raw_text']}"
        completion = json.dumps(sample["entities"], indent=2)
        training_data.append({
            "prompt": prompt,
            "completion": completion,
            "app": sample["app"]  # Keep for analysis
        })
    
    # Split train/valid
    split_idx = int(len(training_data) * 0.9)
    train_data = training_data[:split_idx]
    valid_data = training_data[split_idx:]
    
    # Save files
    output_path = Path(output_dir)
    output_path.mkdir(parents=True, exist_ok=True)
    
    train_file = output_path / "payment_apps_train.jsonl"
    valid_file = output_path / "payment_apps_valid.jsonl"
    
    # Remove app field before saving (just for tracking)
    for filepath, data in [(train_file, train_data), (valid_file, valid_data)]:
        with open(filepath, 'w') as f:
            for item in data:
                save_item = {"prompt": item["prompt"], "completion": item["completion"]}
                f.write(json.dumps(save_item) + '\n')
    
    # Save raw samples for reference
    samples_file = output_path / "payment_apps_samples.json"
    with open(samples_file, 'w') as f:
        json.dump(all_samples, f, indent=2)
    
    # Stats by app
    app_counts = {}
    for sample in all_samples:
        app = sample["app"]
        app_counts[app] = app_counts.get(app, 0) + 1
    
    return {
        "total_samples": len(all_samples),
        "train_samples": len(train_data),
        "valid_samples": len(valid_data),
        "by_app": app_counts,
        "train_file": str(train_file),
        "valid_file": str(valid_file),
        "samples_file": str(samples_file)
    }


def main():
    """Generate Phase 3 training data."""
    print("💳 Generating Phase 3: Payment App Statement Data")
    print("=" * 60)
    
    result = generate_all(samples_per_app=300)
    
    print(f"\n✅ Generated {result['total_samples']} samples")
    print(f"\n📱 By App:")
    for app, count in result['by_app'].items():
        prefix = {"phonepe": "[PHONEPE]", "gpay": "[GPAY]", "paytm": "[PAYTM]"}[app]
        print(f"   {app.upper():10} {prefix:12} {count} samples")
    
    print(f"\n📊 Split:")
    print(f"   Train: {result['train_samples']} samples")
    print(f"   Valid: {result['valid_samples']} samples")
    
    print(f"\n📁 Files created:")
    print(f"   {result['train_file']}")
    print(f"   {result['valid_file']}")
    print(f"   {result['samples_file']}")
    
    # Show sample
    print("\n📋 Sample entries:")
    with open(result['train_file']) as f:
        for i, line in enumerate(f):
            if i >= 3:
                break
            sample = json.loads(line)
            print(f"\n   [{i+1}] {sample['prompt'][:80]}...")


if __name__ == "__main__":
    main()