File size: 8,926 Bytes
a282d4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
import os
import sys
import uuid
import random
from datetime import datetime, timedelta

# Add parent directory to path so we can import from app
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from app.database.database import SessionLocal, engine, SQLALCHEMY_DATABASE_URL
from app.database.models import (
    Base, User, Account, Transaction, Subscription,
    Goal, Investment, AIInsight, FraudLog, Notification, AnalyticsSnapshot
)

# Create tables
Base.metadata.create_all(bind=engine)

def seed_data():
    db = SessionLocal()

    print(f"Seeding into: {SQLALCHEMY_DATABASE_URL}")

    # Check if we already have users
    if db.query(User).count() > 0:
        print("Database already seeded.")
        db.close()
        return

    print("Seeding database...")

    personas = ["Saver", "Investor", "Impulsive Spender", "Minimalist", "Risk Taker"]

    merchants = ["Swiggy", "Amazon", "Netflix", "Uber", "Fuel", "Salary", "SIP",
                 "Starbucks", "Apple", "Walmart"]
    categories = ["Food", "Shopping", "Entertainment", "Transport", "Income",
                  "Investment", "Groceries", "Tech", "Utilities"]

    for persona in personas:
        try:
            user = User(
                email=f"{persona.lower().replace(' ', '_')}@example.com",
                password_hash="hashed_password",
                profile_data={"name": f"{persona} User", "phone": "+1234567890"},
                financial_personality=persona,
                ai_personalization_settings={"theme": "dark", "notifications": "all"}
            )
            db.add(user)
            db.flush()  # get user.id without committing

            # ── Accounts ──────────────────────────────────────────────────────
            checking = Account(
                user_id=user.id, type="checking",
                balance=random.uniform(1000.0, 10000.0), currency="USD"
            )
            savings = Account(
                user_id=user.id, type="savings",
                balance=random.uniform(5000.0, 50000.0), currency="USD"
            )
            db.add_all([checking, savings])
            db.flush()

            # ── Subscriptions ─────────────────────────────────────────────────
            # Active subscription (high usage)
            db.add(Subscription(
                user_id=user.id, merchant="Netflix", amount=15.99,
                billing_cycle="monthly", active=True,
                ai_usage_detection={"usage_frequency": "high", "recommendation": "keep"}
            ))
            # Unused subscription β€” triggers unused detection in subscriptions.py
            db.add(Subscription(
                user_id=user.id, merchant="Spotify", amount=9.99,
                billing_cycle="monthly", active=True,
                ai_usage_detection={"usage_frequency": "low", "recommendation": "cancel"}
            ))
            # Duplicate subscription β€” triggers duplicate detection in subscriptions.py
            # (second Netflix entry for the same user)
            db.add(Subscription(
                user_id=user.id, merchant="Netflix", amount=15.99,
                billing_cycle="monthly", active=True,
                ai_usage_detection={"usage_frequency": "medium", "recommendation": "review"}
            ))

            # ── Goals ─────────────────────────────────────────────────────────
            db.add(Goal(
                user_id=user.id, title="Emergency Fund",
                target_amount=10000.0,
                current_amount=random.uniform(1000.0, 5000.0),
                target_date=datetime.utcnow() + timedelta(days=365),
                ai_generated_plan={"monthly_saving_required": 500.0, "risk": "low"}
            ))

            # ── Investments ───────────────────────────────────────────────────
            db.add(Investment(
                user_id=user.id, asset_name="S&P 500", type="stock",
                amount_invested=random.uniform(1000.0, 10000.0),
                current_value=random.uniform(1100.0, 12000.0),
                portfolio_allocation=50.0,
                ai_risk_analysis={"risk_level": "medium", "recommendation": "hold"}
            ))

            # ── Transactions ──────────────────────────────────────────────────
            start_date = datetime.utcnow() - timedelta(days=90)

            # Monthly salary (3 months)
            for i in range(3):
                tx_date = start_date + timedelta(days=i * 30)
                db.add(Transaction(
                    account_id=checking.id, amount=5000.0, type="credit",
                    category="Income", timestamp=tx_date, merchant="Salary",
                    tags=["salary", "income"],
                    ai_generated_metadata={"is_recurring": True, "confidence": 0.99},
                    spending_emotion_label="neutral"
                ))

            # Regular expense transactions
            for _ in range(30):
                tx_date = start_date + timedelta(days=random.randint(0, 89))
                amount = random.uniform(10.0, 500.0)
                merchant = random.choice(merchants)

                if merchant == "Salary":
                    continue

                # Persona-based spending adjustments
                if user.financial_personality == "Saver" and amount > 200:
                    amount = random.uniform(10.0, 100.0)
                elif user.financial_personality == "Impulsive Spender":
                    amount = random.uniform(50.0, 800.0)

                tx = Transaction(
                    account_id=checking.id, amount=amount, type="debit",
                    category=random.choice(categories),
                    timestamp=tx_date, merchant=merchant,
                    tags=["expense"],
                    ai_generated_metadata={"category_confidence": 0.9},
                    spending_emotion_label=random.choice(["happy", "regret", "neutral", "essential"])
                )
                db.add(tx)
                db.flush()

                # Seed a fraud log for ~5% of transactions
                if random.random() < 0.05:
                    db.add(FraudLog(
                        transaction_id=tx.id,
                        risk_score=random.uniform(0.7, 0.99),
                        suspicious_activity_details="Unusual location and high amount for this merchant.",
                        status="pending"
                    ))

            # Late-night transaction β€” ensures behavior.py late-night detection fires
            late_night_date = start_date + timedelta(days=random.randint(1, 80),
                                                      hours=23, minutes=random.randint(0, 59))
            db.add(Transaction(
                account_id=checking.id,
                amount=random.uniform(50.0, 300.0),
                type="debit",
                category="Entertainment",
                timestamp=late_night_date,
                merchant="Online Store",
                tags=["late-night", "impulse"],
                ai_generated_metadata={"category_confidence": 0.85},
                spending_emotion_label="regret"
            ))

            # ── Supporting records ────────────────────────────────────────────
            db.add(AIInsight(
                user_id=user.id, type="cashflow",
                content=f"You are spending 20% more on {random.choice(categories)} this month."
            ))
            db.add(Notification(
                user_id=user.id, title="Weekly Summary",
                message="Your weekly financial summary is ready.", type="insight"
            ))
            db.add(AnalyticsSnapshot(
                user_id=user.id, date=datetime.utcnow(),
                total_balance=checking.balance + savings.balance,
                total_spending=2000.0, total_savings=3000.0,
                financial_score=random.uniform(60.0, 95.0),
                trends_json={"spending_trend": "down", "savings_trend": "up"}
            ))

            db.commit()
            print(f"  βœ“ Seeded user: {persona}")

        except Exception as e:
            db.rollback()
            print(f"  βœ— Failed to seed user '{persona}': {e}")

    db.close()
    print("Database seeded successfully!")

if __name__ == "__main__":
    seed_data()