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
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
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
439
440
441
442
443
444
445
446
447
448
449
import requests
import json
from typing import Optional, Dict, Any
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

API_URL = "https://0xarchit-citytrack.hf.space"

class CityTrackAPIClient:
    def __init__(self, base_url: str = API_URL):
        self.base_url = base_url.rstrip("/")
        self.session = requests.Session()
        self.access_token: Optional[str] = None

    def admin_login(self, email: str, password: str, expected_role: Optional[str] = "admin") -> Dict[str, Any]:
        try:
            payload: Dict[str, Any] = {
                "email": email,
                "password": password,
                "expected_role": expected_role,
            }
            response = self.session.post(
                f"{self.base_url}/admin/login",
                json=payload,
                headers={"Content-Type": "application/json"},
            )
            response.raise_for_status()
            data = response.json()
            token = data.get("access_token")
            if not token:
                return {"error": "Missing access_token", "response": data}
            self.access_token = token
            self.session.headers.update({"Authorization": f"Bearer {token}"})
            return data
        except Exception as e:
            logger.error(f"Admin login failed: {e}")
            return {"error": str(e)}
    
    def health_check(self) -> Dict[str, Any]:
        """Check API health status"""
        try:
            response = self.session.get(f"{self.base_url}/health/health")
            response.raise_for_status()
            return response.json()
        except Exception as e:
            logger.error(f"Health check failed: {e}")
            return {"status": "error", "message": str(e)}
    
    def get_issues(self, limit: int = 10, skip: int = 0) -> Dict[str, Any]:
        """Fetch all issues"""
        try:
            response = self.session.get(
                f"{self.base_url}/issues",
                params={"limit": limit, "skip": skip}
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            logger.error(f"Failed to fetch issues: {e}")
            return {"error": str(e)}
    
    def get_issue_by_id(self, issue_id: str) -> Dict[str, Any]:
        """Fetch a specific issue"""
        try:
            response = self.session.get(f"{self.base_url}/issues/{issue_id}")
            response.raise_for_status()
            return response.json()
        except Exception as e:
            logger.error(f"Failed to fetch issue {issue_id}: {e}")
            return {"error": str(e)}
    
    def create_issue(self, payload: Dict[str, Any]) -> Dict[str, Any]:
        """Create a new issue"""
        try:
            response = self.session.post(
                f"{self.base_url}/issues",
                json=payload,
                headers={"Content-Type": "application/json"}
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            logger.error(f"Failed to create issue: {e}")
            return {"error": str(e)}
    
    def update_issue(self, issue_id: str, payload: Dict[str, Any]) -> Dict[str, Any]:
        """Update an existing issue"""
        try:
            response = self.session.put(
                f"{self.base_url}/issues/{issue_id}",
                json=payload,
                headers={"Content-Type": "application/json"}
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            logger.error(f"Failed to update issue {issue_id}: {e}")
            return {"error": str(e)}
    
    def get_departments(self) -> Dict[str, Any]:
        """Fetch all departments"""
        try:
            response = self.session.get(f"{self.base_url}/admin/departments")
            response.raise_for_status()
            return response.json()
        except Exception as e:
            logger.error(f"Failed to fetch departments: {e}")
            return {"error": str(e)}

    def create_department(self, name: str, code: str, description: Optional[str] = None,
                          categories: Optional[str] = None, default_sla_hours: int = 48,
                          escalation_email: Optional[str] = None) -> Dict[str, Any]:
        try:
            payload: Dict[str, Any] = {
                "name": name,
                "code": code,
                "description": description,
                "categories": categories,
                "default_sla_hours": default_sla_hours,
                "escalation_email": escalation_email,
            }
            response = self.session.post(
                f"{self.base_url}/admin/departments",
                json=payload,
                headers={"Content-Type": "application/json"},
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            logger.error(f"Failed to create department {code}: {e}")
            return {"error": str(e)}
    
    def get_workers(self, department_id: Optional[str] = None) -> Dict[str, Any]:
        """Fetch all workers or workers from a specific department"""
        try:
            params = {}
            if department_id:
                params["department_id"] = department_id
            
            response = self.session.get(
                f"{self.base_url}/admin/members",
                params=params
            )
            response.raise_for_status()
            data = response.json()
            if isinstance(data, list):
                return [m for m in data if isinstance(m, dict) and m.get("role") == "worker"]
            return data
        except Exception as e:
            logger.error(f"Failed to fetch workers: {e}")
            return {"error": str(e)}
    
    def get_worker_tasks(self) -> Dict[str, Any]:
        """Fetch tasks assigned to the current worker (from JWT)"""
        try:
            response = self.session.get(f"{self.base_url}/worker/tasks")
            response.raise_for_status()
            return response.json()
        except Exception as e:
            logger.error(f"Failed to fetch worker tasks: {e}")
            return {"error": str(e)}
    
    def assign_issue_to_worker(self, issue_id: str, worker_id: str) -> Dict[str, Any]:
        """Assign an issue to a worker"""
        try:
            response = self.session.post(
                f"{self.base_url}/issues/{issue_id}/assign",
                json={"worker_id": worker_id},
                headers={"Content-Type": "application/json"}
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            logger.error(f"Failed to assign issue: {e}")
            return {"error": str(e)}
    
    def resolve_issue(self, issue_id: str, payload: Dict[str, Any]) -> Dict[str, Any]:
        """Mark an issue as resolved"""
        try:
            response = self.session.put(
                f"{self.base_url}/issues/{issue_id}/resolve",
                json=payload,
                headers={"Content-Type": "application/json"}
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            logger.error(f"Failed to resolve issue {issue_id}: {e}")
            return {"error": str(e)}
    
    def get_issue_stats(self) -> Dict[str, Any]:
        """Get issue statistics"""
        try:
            response = self.session.get(f"{self.base_url}/admin/stats")
            response.raise_for_status()
            return response.json()
        except Exception as e:
            logger.error(f"Failed to fetch stats: {e}")
            return {"error": str(e)}
    
    def get_heatmap_data(self, city: Optional[str] = None) -> Dict[str, Any]:
        """Get heatmap data for geospatial visualization"""
        try:
            params = {}
            if city:
                params["city"] = city
            
            response = self.session.get(
                f"{self.base_url}/admin/stats/heatmap",
                params=params
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            logger.error(f"Failed to fetch heatmap data: {e}")
            return {"error": str(e)}
    
    def create_worker(self, department_id: str, name: str, email: str, 
                     locality: str, city: str = "Himachal Pradesh", 
                     max_workload: int = 10, password: str = "12345678",
                     phone: Optional[str] = None, role: str = "worker") -> Dict[str, Any]:
        try:
            payload = {
                "department_id": department_id,
                "name": name,
                "email": email,
                "phone": phone,
                "role": role,
                "city": city,
                "locality": locality,
                "max_workload": max_workload,
                "password": password,
            }
            response = self.session.post(
                f"{self.base_url}/admin/members",
                json=payload,
                headers={"Content-Type": "application/json"}
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            logger.error(f"Failed to create worker {name}: {e}")
            return {"error": str(e)}
    
    def bulk_create_workers(self, department_code: str, department_id: str, 
                           worker_names: list, locations: list) -> Dict[str, Any]:
        """Bulk create workers with Gmail alias format: zrxarchit+<name>.<department>@gmail.com"""
        created_workers = []
        failed_workers = []
        
        if len(worker_names) != len(locations):
            return {"error": "Number of names and locations must match"}
        
        phone_prefix_by_department = {
            "PWD": "101",
            "SANITATION": "202",
            "TRAFFIC": "303",
        }

        for idx, name in enumerate(worker_names):
            location = locations[idx % len(locations)]
            name_parts = name.lower().replace(" ", ".")
            email = f"zrxarchit+{name_parts}.{department_code.lower()}@gmail.com"
            prefix = phone_prefix_by_department.get(department_code.upper(), "999")
            phone = f"9{prefix}{idx + 1:06d}"
            
            response = self.create_worker(
                department_id=department_id,
                name=name,
                email=email,
                locality=location,
                phone=phone,
                password="12345678",
                role="worker",
            )
            
            if "error" not in response:
                created_workers.append({
                    "name": name,
                    "email": email,
                    "locality": location,
                    "status": "created"
                })
                logger.info(f"✓ Created: {name} ({email}) - {location}")
            else:
                failed_workers.append({
                    "name": name,
                    "email": email,
                    "error": response.get("error")
                })
                logger.warning(f"✗ Failed: {name} ({email}) - {response.get('error')}")
        
        return {
            "department": department_code,
            "total": len(worker_names),
            "created": len(created_workers),
            "failed": len(failed_workers),
            "workers": created_workers,
            "failures": failed_workers
        }


def main():
    """Example usage of the API client"""
    print("=" * 80)
    print("CityTrack API Client - Worker Bulk Creation")
    print("=" * 80)
    print(f"Base URL: {API_URL}")
    print("=" * 80)
    
    client = CityTrackAPIClient()
    
    NEARBY_LOCATIONS = [
        "Una", "Haroli", "Amb", "Kasauli", "Baddi",
        "Nalagarh", "Solan", "Parwanoo", "Kalka", "Kurali"
    ]
    
    PWD_WORKERS = [
        "Ramesh Kumar", "Sukesh Singh", "Harish Patel", "Vikram Sharma", "Ajay Kumar",
        "Rajesh Tiwari", "Manoj Singh", "Arjun Verma", "Deepak Yadav", "Sandeep Gupta"
    ]
    
    SANITATION_WORKERS = [
        "Suresh Singh", "Mohan Lal", "Ravi Kumar", "Anita Devi", "Asha Sharma",
        "Priya Singh", "Meera Patel", "Kavya Reddy", "Neha Verma", "Pooja Kumari"
    ]
    
    TRAFFIC_WORKERS = [
        "Priya Sharma", "Anil Kumar", "Bhavna Singh", "Nitin Patel", "Sanjay Verma",
        "Rohit Sharma", "Dinesh Kumar", "Sachin Singh", "Amit Patel", "Vishal Reddy"
    ]
    
    print("\n[1] Health Check:")
    health = client.health_check()
    print(json.dumps(health, indent=2))

    print("\n[2] Admin Login:")
    login = client.admin_login(email="zrxarchit@gmail.com", password="12345678", expected_role="admin")
    if "error" in login:
        print(json.dumps(login, indent=2))
        return
    print(json.dumps({"token_type": login.get("token_type"), "user": login.get("user")}, indent=2))
    
    print("\n[3] Fetching Departments:")
    departments_response = client.get_departments()
    
    departments_map = {}
    if isinstance(departments_response, list):
        for dept in departments_response:
            departments_map[dept["code"]] = dept["id"]
            print(f"  - {dept['name']} (Code: {dept['code']}, ID: {dept['id']})")
    elif isinstance(departments_response, dict) and "departments" in departments_response:
        for dept in departments_response["departments"]:
            departments_map[dept["code"]] = dept["id"]
            print(f"  - {dept['name']} (Code: {dept['code']}, ID: {dept['id']})")
    else:
        print("  Error fetching departments:", departments_response)
        return
    
    if not departments_map:
        print("  No departments found. Creating baseline departments...")
        baseline = [
            ("Public Works Department", "PWD"),
            ("Sanitation Department", "SANITATION"),
            ("Traffic Department", "TRAFFIC"),
        ]
        for name, code in baseline:
            created = client.create_department(name=name, code=code)
            if "error" in created:
                print(json.dumps({"department": code, "result": created}, indent=2))

        departments_response = client.get_departments()
        departments_map = {}
        if isinstance(departments_response, list):
            for dept in departments_response:
                departments_map[dept["code"]] = dept["id"]
                print(f"  - {dept['name']} (Code: {dept['code']}, ID: {dept['id']})")
        elif isinstance(departments_response, dict) and "departments" in departments_response:
            for dept in departments_response["departments"]:
                departments_map[dept["code"]] = dept["id"]
                print(f"  - {dept['name']} (Code: {dept['code']}, ID: {dept['id']})")
        else:
            print("  Error fetching departments:", departments_response)
            return

        if not departments_map:
            print("  ERROR: Failed to create/fetch departments.")
            return
    
    print("\n[4] Creating PWD Workers (10 workers):")
    print("-" * 80)
    if "PWD" in departments_map:
        pwd_result = client.bulk_create_workers(
            department_code="PWD",
            department_id=departments_map["PWD"],
            worker_names=PWD_WORKERS,
            locations=NEARBY_LOCATIONS
        )
        print(json.dumps(pwd_result, indent=2))
    else:
        print("  ERROR: PWD department not found")
    
    print("\n[5] Creating Sanitation Workers (10 workers):")
    print("-" * 80)
    if "SANITATION" in departments_map:
        sanitation_result = client.bulk_create_workers(
            department_code="SANITATION",
            department_id=departments_map["SANITATION"],
            worker_names=SANITATION_WORKERS,
            locations=NEARBY_LOCATIONS
        )
        print(json.dumps(sanitation_result, indent=2))
    else:
        print("  ERROR: SANITATION department not found")
    
    print("\n[6] Creating Traffic Workers (10 workers):")
    print("-" * 80)
    if "TRAFFIC" in departments_map:
        traffic_result = client.bulk_create_workers(
            department_code="TRAFFIC",
            department_id=departments_map["TRAFFIC"],
            worker_names=TRAFFIC_WORKERS,
            locations=NEARBY_LOCATIONS
        )
        print(json.dumps(traffic_result, indent=2))
    else:
        print("  ERROR: TRAFFIC department not found")
    
    print("\n[7] Fetching All Workers:")
    workers = client.get_workers()
    if isinstance(workers, list):
        print(f"  Total workers: {len(workers)}")
        for worker in workers[:5]:
            print(f"    - {worker.get('name')} ({worker.get('email')})")
        if len(workers) > 5:
            print(f"    ... and {len(workers) - 5} more")
    else:
        print(json.dumps(workers, indent=2)[:500] + "...")
    
    print("\n" + "=" * 80)
    print("Bulk Worker Creation Complete!")
    print("=" * 80)


if __name__ == "__main__":
    main()