File size: 30,170 Bytes
34367da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
#!/usr/bin/env python3
"""
🚀 EWS AUTODISCOVER HARVESTER - Step 2
Automatisk endpoint discovery + OAuth2 Modern Auth

Features:
- Autodiscover Exchange endpoint fra email domain
- OAuth2 med MSAL (Modern Authentication)
- Full mailbox access: emails, calendar, contacts
- Batch operations for effektiv harvest
- Neo4j Knowledge Graph integration
"""
import asyncio
import hashlib
import json
import re
from datetime import datetime, timedelta
from pathlib import Path
from typing import Optional, Dict, List, Any
from dataclasses import dataclass, asdict
import xml.etree.ElementTree as ET

import httpx
from neo4j import GraphDatabase

# ============================================================
# CONFIGURATION
# ============================================================

NEO4J_URI = "neo4j+s://054eff27.databases.neo4j.io"
NEO4J_USER = "neo4j"
NEO4J_PASSWORD = "Qrt37mkb0xBZ7_ts5tG1J70K2mVDGPMF2L7Njlm7cg8"

# Azure AD App Registration (oprettes i Azure Portal)
# For TDC: Brug eksisterende app eller opret ny
AZURE_CLIENT_ID = ""  # Udfyldes med din Azure App ID
AZURE_TENANT_ID = ""  # Udfyldes med TDC tenant ID
AZURE_CLIENT_SECRET = ""  # Optional: Kun for app-only auth

# EWS Endpoints (autodiscover finder den rigtige)
EWS_ENDPOINTS = [
    "https://outlook.office365.com/EWS/Exchange.asmx",
    "https://outlook.office.com/EWS/Exchange.asmx",
]

# Søgetermer
SEARCH_KEYWORDS = [
    "strategi", "cyber", "NIS2", "SOC", "MDR", "cloud", "Azure", "AI", 
    "Copilot", "Columbus", "ERP", "budget", "forecast", "kunde", "kontrakt",
    "rammeaftale", "SKI", "produkt", "CloudKey", "arkitektur", "roadmap"
]


# ============================================================
# DATA CLASSES
# ============================================================

@dataclass
class EmailItem:
    """Repræsenterer en email fra Exchange"""
    id: str
    subject: str
    sender: str
    sender_name: str
    received: str
    body_preview: str
    folder: str
    keywords: List[str]
    has_attachments: bool
    attachments: List[Dict]
    importance: str
    categories: str
    conversation_id: str = ""
    internet_message_id: str = ""


@dataclass 
class CalendarItem:
    """Repræsenterer en kalenderbegivenhed"""
    id: str
    subject: str
    organizer: str
    start: str
    end: str
    location: str
    attendees: List[str]
    body_preview: str
    is_recurring: bool


@dataclass
class ContactItem:
    """Repræsenterer en kontakt"""
    id: str
    display_name: str
    email: str
    company: str
    job_title: str
    department: str
    phone: str


# ============================================================
# AUTODISCOVER
# ============================================================

class EWSAutodiscover:
    """Automatisk Exchange endpoint discovery"""
    
    def __init__(self, email: str):
        self.email = email
        self.domain = email.split("@")[1]
        self.ews_url: Optional[str] = None
        
    async def discover(self) -> Optional[str]:
        """Find EWS endpoint via autodiscover"""
        print(f"🔍 Autodiscover for {self.domain}...")
        
        # Autodiscover URLs at prøve
        autodiscover_urls = [
            f"https://autodiscover.{self.domain}/autodiscover/autodiscover.xml",
            f"https://{self.domain}/autodiscover/autodiscover.xml",
            f"https://autodiscover.{self.domain}/Autodiscover/Autodiscover.xml",
            "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml",
        ]
        
        autodiscover_payload = f"""<?xml version="1.0" encoding="utf-8"?>
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006">
  <Request>
    <EMailAddress>{self.email}</EMailAddress>
    <AcceptableResponseSchema>http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema>
  </Request>
</Autodiscover>"""
        
        async with httpx.AsyncClient(timeout=30) as client:
            for url in autodiscover_urls:
                try:
                    print(f"   Prøver: {url}")
                    response = await client.post(
                        url,
                        content=autodiscover_payload,
                        headers={"Content-Type": "text/xml"}
                    )
                    
                    if response.status_code == 200:
                        # Parse XML response
                        ews_url = self._parse_autodiscover_response(response.text)
                        if ews_url:
                            print(f"   ✅ Fundet: {ews_url}")
                            self.ews_url = ews_url
                            return ews_url
                            
                except Exception as e:
                    continue
        
        # Fallback til Office 365
        print("   ⚠️ Autodiscover failed, bruger Office 365 default")
        self.ews_url = EWS_ENDPOINTS[0]
        return self.ews_url
    
    def _parse_autodiscover_response(self, xml_text: str) -> Optional[str]:
        """Parse autodiscover XML response"""
        try:
            root = ET.fromstring(xml_text)
            # Find EWS URL i response
            for elem in root.iter():
                if "EwsUrl" in elem.tag or "ASUrl" in elem.tag:
                    return elem.text
        except:
            pass
        return None


# ============================================================
# OAUTH2 AUTHENTICATION
# ============================================================

class OAuth2Auth:
    """OAuth2 authentication for EWS"""
    
    def __init__(self, client_id: str, tenant_id: str, client_secret: str = ""):
        self.client_id = client_id
        self.tenant_id = tenant_id
        self.client_secret = client_secret
        self.access_token: Optional[str] = None
        self.token_expires: Optional[datetime] = None
        
    async def get_token_interactive(self) -> str:
        """Get token via interactive browser login"""
        try:
            import msal
            
            app = msal.PublicClientApplication(
                self.client_id,
                authority=f"https://login.microsoftonline.com/{self.tenant_id}"
            )
            
            # Scopes for EWS
            scopes = ["https://outlook.office365.com/EWS.AccessAsUser.All"]
            
            # Prøv silent først (cached token)
            accounts = app.get_accounts()
            if accounts:
                result = app.acquire_token_silent(scopes, account=accounts[0])
                if result and "access_token" in result:
                    self.access_token = result["access_token"]
                    return self.access_token
            
            # Interactive login
            print("🔐 Åbner browser for login...")
            result = app.acquire_token_interactive(scopes=scopes)
            
            if "access_token" in result:
                self.access_token = result["access_token"]
                print("   ✅ Token hentet!")
                return self.access_token
            else:
                raise Exception(f"Token error: {result.get('error_description', 'Unknown')}")
                
        except ImportError:
            print("⚠️ MSAL ikke installeret. Kør: pip install msal")
            raise
    
    async def get_token_device_flow(self) -> str:
        """Get token via device code flow (headless)"""
        try:
            import msal
            
            app = msal.PublicClientApplication(
                self.client_id,
                authority=f"https://login.microsoftonline.com/{self.tenant_id}"
            )
            
            scopes = ["https://outlook.office365.com/EWS.AccessAsUser.All"]
            
            # Start device flow
            flow = app.initiate_device_flow(scopes=scopes)
            
            if "user_code" not in flow:
                raise Exception("Device flow failed")
            
            print("\n" + "=" * 60)
            print("🔐 DEVICE CODE LOGIN")
            print("=" * 60)
            print(f"   Gå til: {flow['verification_uri']}")
            print(f"   Indtast kode: {flow['user_code']}")
            print("=" * 60 + "\n")
            
            result = app.acquire_token_by_device_flow(flow)
            
            if "access_token" in result:
                self.access_token = result["access_token"]
                print("   ✅ Token hentet!")
                return self.access_token
            else:
                raise Exception(f"Token error: {result.get('error_description')}")
                
        except ImportError:
            print("⚠️ MSAL ikke installeret. Kør: pip install msal")
            raise


# ============================================================
# EWS CLIENT
# ============================================================

class EWSClient:
    """Exchange Web Services client"""
    
    SOAP_NS = {
        "soap": "http://schemas.xmlsoap.org/soap/envelope/",
        "t": "http://schemas.microsoft.com/exchange/services/2006/types",
        "m": "http://schemas.microsoft.com/exchange/services/2006/messages",
    }
    
    def __init__(self, ews_url: str, access_token: str):
        self.ews_url = ews_url
        self.access_token = access_token
        self.client = httpx.AsyncClient(timeout=60)
        
    def _headers(self) -> Dict:
        return {
            "Authorization": f"Bearer {self.access_token}",
            "Content-Type": "text/xml; charset=utf-8",
        }
    
    async def _soap_request(self, body: str) -> str:
        """Send SOAP request til EWS"""
        envelope = f"""<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
               xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2016"/>
  </soap:Header>
  <soap:Body>
    {body}
  </soap:Body>
</soap:Envelope>"""
        
        response = await self.client.post(
            self.ews_url,
            content=envelope,
            headers=self._headers()
        )
        
        if response.status_code != 200:
            raise Exception(f"EWS Error {response.status_code}: {response.text[:500]}")
        
        return response.text

    async def get_folder_id(self, folder_name: str = "inbox") -> Optional[str]:
        """Hent folder ID"""
        folder_map = {
            "inbox": "inbox",
            "sent": "sentitems", 
            "drafts": "drafts",
            "deleted": "deleteditems",
            "calendar": "calendar",
            "contacts": "contacts",
        }
        
        distinguished_folder = folder_map.get(folder_name.lower(), folder_name)
        
        body = f"""<m:GetFolder>
  <m:FolderShape>
    <t:BaseShape>IdOnly</t:BaseShape>
  </m:FolderShape>
  <m:FolderIds>
    <t:DistinguishedFolderId Id="{distinguished_folder}"/>
  </m:FolderIds>
</m:GetFolder>"""
        
        response = await self._soap_request(body)
        
        # Parse folder ID
        try:
            root = ET.fromstring(response)
            for elem in root.iter():
                if "FolderId" in elem.tag:
                    return elem.get("Id")
        except:
            pass
        return None
    
    async def find_items(
        self,
        folder: str = "inbox",
        max_items: int = 100,
        days_back: int = 365,
        keywords: List[str] = None
    ) -> List[EmailItem]:
        """Find emails i en folder"""
        
        # Dato filter
        since_date = (datetime.now() - timedelta(days=days_back)).strftime("%Y-%m-%dT00:00:00Z")
        
        # Byg restriction (søgefilter)
        restriction = f"""<m:Restriction>
  <t:IsGreaterThanOrEqualTo>
    <t:FieldURI FieldURI="item:DateTimeReceived"/>
    <t:FieldURIOrConstant>
      <t:Constant Value="{since_date}"/>
    </t:FieldURIOrConstant>
  </t:IsGreaterThanOrEqualTo>
</m:Restriction>"""
        
        body = f"""<m:FindItem Traversal="Shallow">
  <m:ItemShape>
    <t:BaseShape>Default</t:BaseShape>
    <t:AdditionalProperties>
      <t:FieldURI FieldURI="item:Subject"/>
      <t:FieldURI FieldURI="item:DateTimeReceived"/>
      <t:FieldURI FieldURI="message:From"/>
      <t:FieldURI FieldURI="item:HasAttachments"/>
      <t:FieldURI FieldURI="item:Importance"/>
      <t:FieldURI FieldURI="item:Categories"/>
      <t:FieldURI FieldURI="message:ConversationId"/>
      <t:FieldURI FieldURI="message:InternetMessageId"/>
    </t:AdditionalProperties>
  </m:ItemShape>
  {restriction}
  <m:ParentFolderIds>
    <t:DistinguishedFolderId Id="{folder}"/>
  </m:ParentFolderIds>
  <m:IndexedPageItemView MaxEntriesReturned="{max_items}" Offset="0" BasePoint="Beginning"/>
</m:FindItem>"""
        
        response = await self._soap_request(body)
        return self._parse_email_items(response, folder, keywords or SEARCH_KEYWORDS)
    
    def _parse_email_items(self, xml_text: str, folder: str, keywords: List[str]) -> List[EmailItem]:
        """Parse email items fra XML"""
        items = []
        
        try:
            root = ET.fromstring(xml_text)
            
            for message in root.iter():
                if not message.tag.endswith("Message"):
                    continue
                
                # Extract fields
                subject = ""
                sender = ""
                sender_name = ""
                received = ""
                has_attachments = False
                importance = "Normal"
                categories = ""
                item_id = ""
                conversation_id = ""
                internet_message_id = ""
                
                for child in message:
                    tag = child.tag.split("}")[-1] if "}" in child.tag else child.tag
                    
                    if tag == "ItemId":
                        item_id = child.get("Id", "")
                    elif tag == "Subject":
                        subject = child.text or ""
                    elif tag == "DateTimeReceived":
                        received = child.text or ""
                    elif tag == "From":
                        for mailbox in child.iter():
                            mtag = mailbox.tag.split("}")[-1] if "}" in mailbox.tag else mailbox.tag
                            if mtag == "EmailAddress":
                                sender = mailbox.text or ""
                            elif mtag == "Name":
                                sender_name = mailbox.text or ""
                    elif tag == "HasAttachments":
                        has_attachments = child.text == "true"
                    elif tag == "Importance":
                        importance = child.text or "Normal"
                    elif tag == "Categories":
                        categories = ",".join([c.text for c in child if c.text])
                    elif tag == "ConversationId":
                        conversation_id = child.get("Id", "")
                    elif tag == "InternetMessageId":
                        internet_message_id = child.text or ""
                
                # Match keywords
                subject_lower = subject.lower()
                matched_keywords = [kw for kw in keywords if kw.lower() in subject_lower]
                
                if matched_keywords or not keywords:
                    items.append(EmailItem(
                        id=item_id,
                        subject=subject,
                        sender=sender,
                        sender_name=sender_name,
                        received=received,
                        body_preview="",  # Hentes separat hvis nødvendigt
                        folder=folder,
                        keywords=matched_keywords,
                        has_attachments=has_attachments,
                        attachments=[],
                        importance=importance,
                        categories=categories,
                        conversation_id=conversation_id,
                        internet_message_id=internet_message_id
                    ))
        except Exception as e:
            print(f"   ⚠️ Parse error: {e}")
        
        return items
    
    async def get_item_body(self, item_id: str) -> str:
        """Hent email body"""
        body = f"""<m:GetItem>
  <m:ItemShape>
    <t:BaseShape>Default</t:BaseShape>
    <t:AdditionalProperties>
      <t:FieldURI FieldURI="item:Body"/>
    </t:AdditionalProperties>
  </m:ItemShape>
  <m:ItemIds>
    <t:ItemId Id="{item_id}"/>
  </m:ItemIds>
</m:GetItem>"""
        
        response = await self._soap_request(body)
        
        try:
            root = ET.fromstring(response)
            for elem in root.iter():
                if elem.tag.endswith("Body"):
                    text = elem.text or ""
                    # Strip HTML
                    text = re.sub(r'<[^>]+>', '', text)
                    return text[:2000]
        except:
            pass
        return ""
    
    async def get_calendar_items(self, days_forward: int = 30, days_back: int = 7) -> List[CalendarItem]:
        """Hent kalender events"""
        start = (datetime.now() - timedelta(days=days_back)).strftime("%Y-%m-%dT00:00:00Z")
        end = (datetime.now() + timedelta(days=days_forward)).strftime("%Y-%m-%dT23:59:59Z")
        
        body = f"""<m:FindItem Traversal="Shallow">
  <m:ItemShape>
    <t:BaseShape>Default</t:BaseShape>
  </m:ItemShape>
  <m:CalendarView MaxEntriesReturned="100" StartDate="{start}" EndDate="{end}"/>
  <m:ParentFolderIds>
    <t:DistinguishedFolderId Id="calendar"/>
  </m:ParentFolderIds>
</m:FindItem>"""
        
        response = await self._soap_request(body)
        return self._parse_calendar_items(response)
    
    def _parse_calendar_items(self, xml_text: str) -> List[CalendarItem]:
        """Parse calendar items"""
        items = []
        
        try:
            root = ET.fromstring(xml_text)
            
            for cal_item in root.iter():
                if not cal_item.tag.endswith("CalendarItem"):
                    continue
                
                item_id = ""
                subject = ""
                organizer = ""
                start = ""
                end = ""
                location = ""
                attendees = []
                is_recurring = False
                
                for child in cal_item:
                    tag = child.tag.split("}")[-1] if "}" in child.tag else child.tag
                    
                    if tag == "ItemId":
                        item_id = child.get("Id", "")
                    elif tag == "Subject":
                        subject = child.text or ""
                    elif tag == "Start":
                        start = child.text or ""
                    elif tag == "End":
                        end = child.text or ""
                    elif tag == "Location":
                        location = child.text or ""
                    elif tag == "IsRecurring":
                        is_recurring = child.text == "true"
                    elif tag == "Organizer":
                        for mailbox in child.iter():
                            if mailbox.tag.endswith("EmailAddress"):
                                organizer = mailbox.text or ""
                
                items.append(CalendarItem(
                    id=item_id,
                    subject=subject,
                    organizer=organizer,
                    start=start,
                    end=end,
                    location=location,
                    attendees=attendees,
                    body_preview="",
                    is_recurring=is_recurring
                ))
        except Exception as e:
            print(f"   ⚠️ Calendar parse error: {e}")
        
        return items
    
    async def close(self):
        await self.client.aclose()



# ============================================================
# MAIN HARVESTER
# ============================================================

class EWSAutodiscoverHarvester:
    """Main harvester med autodiscover og EWS"""
    
    def __init__(self, email: str, client_id: str, tenant_id: str):
        self.email = email
        self.client_id = client_id
        self.tenant_id = tenant_id
        self.autodiscover = EWSAutodiscover(email)
        self.auth = OAuth2Auth(client_id, tenant_id)
        self.ews_client: Optional[EWSClient] = None
        self.neo4j = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USER, NEO4J_PASSWORD))
        
        self.stats = {
            "emails_found": 0,
            "emails_matched": 0,
            "calendar_items": 0,
            "contacts": 0,
            "saved_to_neo4j": 0
        }
        
        self.output_dir = Path("data/ews_harvest")
        self.output_dir.mkdir(parents=True, exist_ok=True)
    
    async def connect(self) -> bool:
        """Forbind til Exchange"""
        print("\n" + "=" * 60)
        print("🚀 EWS AUTODISCOVER HARVESTER")
        print("=" * 60)
        
        # Step 1: Autodiscover
        ews_url = await self.autodiscover.discover()
        if not ews_url:
            print("❌ Kunne ikke finde EWS endpoint")
            return False
        
        # Step 2: OAuth2 token
        print("\n🔐 Henter OAuth2 token...")
        try:
            # Prøv device flow først (virker headless)
            token = await self.auth.get_token_device_flow()
        except Exception as e:
            print(f"   Device flow failed: {e}")
            try:
                # Fallback til interactive
                token = await self.auth.get_token_interactive()
            except Exception as e2:
                print(f"❌ Authentication failed: {e2}")
                return False
        
        # Step 3: Opret EWS client
        self.ews_client = EWSClient(ews_url, token)
        print("✅ Forbundet til Exchange!")
        
        return True
    
    def save_email_to_neo4j(self, email: EmailItem):
        """Gem email i Neo4j"""
        content_hash = hashlib.md5(f"{email.subject}:{email.id}".encode()).hexdigest()
        
        with self.neo4j.session() as session:
            session.run("""
                MERGE (e:ExchangeEmail {contentHash: $hash})
                ON CREATE SET
                    e.itemId = $id,
                    e.subject = $subject,
                    e.sender = $sender,
                    e.senderName = $senderName,
                    e.received = $received,
                    e.bodyPreview = $body,
                    e.folder = $folder,
                    e.keywords = $keywords,
                    e.hasAttachments = $hasAtt,
                    e.importance = $importance,
                    e.conversationId = $convId,
                    e.harvestedAt = datetime(),
                    e.harvestMethod = 'EWS_Autodiscover'
                ON MATCH SET
                    e.lastSeen = datetime()
                
                MERGE (ds:DataSource {name: 'TDC_Exchange_EWS'})
                ON CREATE SET ds.type = 'exchange_ews'
                MERGE (e)-[:HARVESTED_FROM]->(ds)
            """,
                hash=content_hash,
                id=email.id,
                subject=email.subject[:500],
                sender=email.sender,
                senderName=email.sender_name,
                received=email.received,
                body=email.body_preview,
                folder=email.folder,
                keywords=email.keywords,
                hasAtt=email.has_attachments,
                importance=email.importance,
                convId=email.conversation_id
            )
            
            # Keyword relationships
            for kw in email.keywords:
                session.run("""
                    MERGE (k:SearchKeyword {name: $kw})
                    WITH k
                    MATCH (e:ExchangeEmail {contentHash: $hash})
                    MERGE (e)-[:MATCHES_KEYWORD]->(k)
                """, kw=kw, hash=content_hash)
        
        self.stats["saved_to_neo4j"] += 1
    
    def save_calendar_to_neo4j(self, item: CalendarItem):
        """Gem kalender event i Neo4j"""
        content_hash = hashlib.md5(f"{item.subject}:{item.start}".encode()).hexdigest()
        
        with self.neo4j.session() as session:
            session.run("""
                MERGE (c:CalendarEvent {contentHash: $hash})
                ON CREATE SET
                    c.itemId = $id,
                    c.subject = $subject,
                    c.organizer = $organizer,
                    c.start = $start,
                    c.end = $end,
                    c.location = $location,
                    c.isRecurring = $recurring,
                    c.harvestedAt = datetime()
                
                MERGE (ds:DataSource {name: 'TDC_Exchange_Calendar'})
                MERGE (c)-[:HARVESTED_FROM]->(ds)
            """,
                hash=content_hash,
                id=item.id,
                subject=item.subject[:500],
                organizer=item.organizer,
                start=item.start,
                end=item.end,
                location=item.location,
                recurring=item.is_recurring
            )
    
    async def harvest_emails(self, folders: List[str] = None, days_back: int = 365):
        """Harvest emails fra folders"""
        if not folders:
            folders = ["inbox", "sentitems"]
        
        all_emails = []
        
        for folder in folders:
            print(f"\n📂 Scanner {folder}...")
            
            try:
                items = await self.ews_client.find_items(
                    folder=folder,
                    max_items=500,
                    days_back=days_back,
                    keywords=SEARCH_KEYWORDS
                )
                
                self.stats["emails_found"] += len(items)
                
                # Filtrer på keywords
                matched = [item for item in items if item.keywords]
                self.stats["emails_matched"] += len(matched)
                
                print(f"   Fundet: {len(items)}, Matched: {len(matched)}")
                
                # Hent body for matched items (begrænset)
                for i, item in enumerate(matched[:50]):
                    if i % 10 == 0:
                        print(f"   Henter body {i+1}/{min(len(matched), 50)}...", end="\r")
                    
                    body = await self.ews_client.get_item_body(item.id)
                    item.body_preview = body
                    
                    # Gem i Neo4j
                    self.save_email_to_neo4j(item)
                    all_emails.append(item)
                    
            except Exception as e:
                print(f"   ⚠️ Error: {e}")
        
        return all_emails
    
    async def harvest_calendar(self, days_forward: int = 30, days_back: int = 7):
        """Harvest kalender events"""
        print(f"\n📅 Scanner kalender ({days_back}d tilbage, {days_forward}d frem)...")
        
        try:
            items = await self.ews_client.get_calendar_items(days_forward, days_back)
            self.stats["calendar_items"] = len(items)
            
            print(f"   Fundet: {len(items)} events")
            
            for item in items:
                self.save_calendar_to_neo4j(item)
            
            return items
            
        except Exception as e:
            print(f"   ⚠️ Error: {e}")
            return []
    
    async def run(self, days_back: int = 365):
        """Kør fuld harvest"""
        if not await self.connect():
            return
        
        # Harvest emails
        emails = await self.harvest_emails(
            folders=["inbox", "sentitems"],
            days_back=days_back
        )
        
        # Harvest calendar
        calendar = await self.harvest_calendar()
        
        # Gem JSON output
        output_file = self.output_dir / f"ews_harvest_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
        with open(output_file, 'w', encoding='utf-8') as f:
            json.dump({
                "timestamp": datetime.now().isoformat(),
                "email": self.email,
                "stats": self.stats,
                "emails": [asdict(e) for e in emails],
                "calendar": [asdict(c) for c in calendar]
            }, f, indent=2, ensure_ascii=False)
        
        # Summary
        print("\n" + "=" * 60)
        print("📊 HARVEST COMPLETE")
        print("=" * 60)
        print(f"   📧 Emails fundet:    {self.stats['emails_found']}")
        print(f"   ✅ Emails matched:   {self.stats['emails_matched']}")
        print(f"   📅 Calendar events:  {self.stats['calendar_items']}")
        print(f"   💾 Saved to Neo4j:   {self.stats['saved_to_neo4j']}")
        print(f"\n   📁 Output: {output_file}")
        print("=" * 60)
        
        # Cleanup
        if self.ews_client:
            await self.ews_client.close()
        self.neo4j.close()


# ============================================================
# MAIN
# ============================================================

async def main():
    import argparse
    
    parser = argparse.ArgumentParser(description="EWS Autodiscover Harvester")
    parser.add_argument("--email", required=True, help="Din TDC email adresse")
    parser.add_argument("--client-id", required=True, help="Azure AD App Client ID")
    parser.add_argument("--tenant-id", required=True, help="Azure AD Tenant ID")
    parser.add_argument("--days", type=int, default=365, help="Dage tilbage (default: 365)")
    args = parser.parse_args()
    
    harvester = EWSAutodiscoverHarvester(
        email=args.email,
        client_id=args.client_id,
        tenant_id=args.tenant_id
    )
    
    await harvester.run(args.days)


if __name__ == "__main__":
    asyncio.run(main())