File size: 4,390 Bytes
1fce3ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Seed data for development and testing
Creates initial data in the database
"""

from sqlalchemy.orm import Session
from app.core.security import get_password_hash
from app.models.user import User, UserRole
from app.models.client import Client
from app.core.database import SessionLocal


def init_admin_user(db: Session) -> User:
    """Create default admin user"""
    admin = db.query(User).filter(User.email == "admin@legaltech.com").first()
    
    if not admin:
        admin = User(
            email="admin@legaltech.com",
            full_name="System Administrator",
            full_name_ar="مدير النظام",
            hashed_password=get_password_hash("admin123"),
            role=UserRole.ADMIN,
            is_active=True,
            is_superuser=True
        )
        db.add(admin)
        db.commit()
        db.refresh(admin)
        print("✅ Created admin user: admin@legaltech.com / admin123")
    else:
        print("ℹ️  Admin user already exists")
    
    return admin


def init_sample_users(db: Session):
    """Create sample users for testing"""
    users = [
        {
            "email": "lawyer@legaltech.com",
            "full_name": "Ahmed AlSalem",
            "full_name_ar": "أحمد السالم",
            "password": "lawyer123",
            "role": UserRole.LAWYER
        },
        {
            "email": "staff@legaltech.com",
            "full_name": "Sara AlOtaibi",
            "full_name_ar": "سارة العتيبي",
            "password": "staff123",
            "role": UserRole.STAFF
        },
        {
            "email": "client@legaltech.com",
            "full_name": "Mohammed AlQahtani",
            "full_name_ar": "محمد القحطاني",
            "password": "client123",
            "role": UserRole.CLIENT
        }
    ]
    
    for user_data in users:
        existing = db.query(User).filter(User.email == user_data["email"]).first()
        if not existing:
            user = User(
                email=user_data["email"],
                full_name=user_data["full_name"],
                full_name_ar=user_data["full_name_ar"],
                hashed_password=get_password_hash(user_data["password"]),
                role=user_data["role"],
                is_active=True
            )
            db.add(user)
            print(f"✅ Created user: {user_data['email']}")
    
    db.commit()


def init_sample_clients(db: Session):
    """Create sample clients"""
    clients = [
        {
            "full_name": "Abdullah AlMutairi",
            "full_name_ar": "عبدالله المطيري",
            "email": "abdullah@example.com",
            "phone": "+966501234567",
            "national_id": "1234567890",
            "address": "Riyadh, Saudi Arabia"
        },
        {
            "full_name": "Fatima AlHarbi",
            "full_name_ar": "فاطمة الحربي",
            "email": "fatima@example.com",
            "phone": "+966507654321",
            "national_id": "2345678901",
            "address": "Jeddah, Saudi Arabia"
        }
    ]
    
    for client_data in clients:
        existing = db.query(Client).filter(Client.email == client_data["email"]).first()
        if not existing:
            client = Client(**client_data)
            db.add(client)
            print(f"✅ Created client: {client_data['full_name']}")
    
    db.commit()


def seed_database():
    """
    Main function to seed the database
    Run this to populate database with initial data
    """
    print("=" * 60)
    print("SEEDING DATABASE")
    print("=" * 60)
    
    db = SessionLocal()
    
    try:
        # Create admin and sample users
        init_admin_user(db)
        init_sample_users(db)
        
        # Create sample clients
        init_sample_clients(db)
        
        print("=" * 60)
        print("✅ DATABASE SEEDING COMPLETE")
        print("=" * 60)
        print("\nLogin credentials:")
        print("  Admin:  admin@legaltech.com / admin123")
        print("  Lawyer: lawyer@legaltech.com / lawyer123")
        print("  Staff:  staff@legaltech.com / staff123")
        print("  Client: client@legaltech.com / client123")
        print()
        
    except Exception as e:
        print(f"❌ Error seeding database: {e}")
        db.rollback()
    finally:
        db.close()


if __name__ == "__main__":
    seed_database()