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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Confluence Integration Adapter

Provides OAuth-based integration with Atlassian Confluence for knowledge base and wiki management.
"""

import logging
import os
import httpx
from typing import Dict, Any, List, Optional
from datetime import datetime, timedelta
from urllib.parse import urlencode

logger = logging.getLogger(__name__)


class ConfluenceAdapter:
    """
    Adapter for Confluence OAuth integration.

    Supports:
    - OAuth 2.0 authentication
    - Page and blog management
    - Space and content operations
    - Search and attachment handling
    """

    def __init__(self, db, workspace_id: str):
        self.db = db
        self.workspace_id = workspace_id
        self.service_name = "confluence"
        self.site_url = os.getenv("CONFLUENCE_SITE_URL")
        self.base_url = f"{self.site_url}/wiki/rest/api" if self.site_url else None

        # OAuth credentials from environment
        self.client_id = os.getenv("CONFLUENCE_CLIENT_ID")
        self.client_secret = os.getenv("CONFLUENCE_CLIENT_SECRET")
        self.redirect_uri = os.getenv("CONFLUENCE_REDIRECT_URI")

        # Token storage
        self._access_token: Optional[str] = None
        _refresh_token: Optional[str] = None
        self._token_expires_at: Optional[datetime] = None

    async def get_oauth_url(self) -> str:
        """
        Generate Confluence OAuth authorization URL.

        Returns:
            Authorization URL to redirect user to Confluence OAuth consent screen
        """
        if not self.site_url or not self.client_id:
            raise ValueError("Confluence site URL and client ID must be configured")

        # Confluence OAuth endpoint
        auth_url = f"{self.site_url}/wiki/rest/oauth2/latest/authorization"

        # Build authorization URL
        params = {
            "client_id": self.client_id,
            "response_type": "code",
            "redirect_uri": self.redirect_uri,
            "scope": "read:space-content write:space-content search:confluence",
            "state": self.workspace_id,  # Use workspace_id as state
        }

        auth_url_with_params = f"{auth_url}?{urlencode(params)}"

        logger.info(f"Generated Confluence OAuth URL for workspace {self.workspace_id}")
        return auth_url_with_params

    async def exchange_code_for_token(self, code: str) -> Dict[str, Any]:
        """
        Exchange OAuth authorization code for access token.

        Args:
            code: Authorization code from OAuth callback

        Returns:
            Token response with access_token, refresh_token, expires_in, etc.
        """
        if not self.site_url or not self.client_id or not self.client_secret:
            raise ValueError("Confluence OAuth credentials not configured")

        token_url = f"{self.site_url}/wiki/rest/oauth2/latest/token"

        data = {
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": self.redirect_uri,
            "client_id": self.client_id,
            "client_secret": self.client_secret,
        }

        try:
            async with httpx.AsyncClient() as client:
                response = await client.post(token_url, data=data)
                response.raise_for_status()

                token_data = response.json()

                # Store tokens
                self._access_token = token_data.get("access_token")
                _refresh_token = token_data.get("refresh_token")

                # Calculate token expiration
                if "expires_in" in token_data:
                    self._token_expires_at = datetime.now() + timedelta(
                        seconds=token_data["expires_in"]
                    )

                logger.info(f"Successfully obtained Confluence access token for workspace {self.workspace_id}")
                return token_data

        except httpx.HTTPStatusError as e:
            logger.error(f"Confluence token exchange failed: {e}")
            raise

    async def test_connection(self) -> bool:
        """
        Test the Confluence API connection.

        Returns:
            True if connection successful, False otherwise
        """
        if not self.base_url or not self._access_token:
            return False

        try:
            async with httpx.AsyncClient() as client:
                # Test by getting current user info
                response = await client.get(
                    f"{self.base_url}/user/current",
                    headers={
                        "Authorization": f"Bearer {self._access_token}"
                    }
                )
                response.raise_for_status()

                logger.info(f"Confluence connection test successful for workspace {self.workspace_id}")
                return True

        except Exception as e:
            logger.error(f"Confluence connection test failed: {e}")
            return False

    async def search_content(self, query: str, limit: int = 10,
                            space_key: str = None, type: str = "page") -> List[Dict[str, Any]]:
        """
        Search Confluence content using CQL (Confluence Query Language).

        Args:
            query: Search query string
            limit: Maximum number of results
            space_key: Space key to limit search
            type: Content type ("page", "blogpost")

        Returns:
            List of content objects
        """
        if not self.base_url or not self._access_token:
            raise ValueError("Confluence API not configured")

        try:
            # Build CQL query
            cql = f"type={type} and text ~ '{query}'"
            if space_key:
                cql = f"{cql} and space.key = '{space_key}'"

            async with httpx.AsyncClient() as client:
                response = await client.get(
                    f"{self.base_url}/content/search",
                    headers={
                        "Authorization": f"Bearer {self._access_token}"
                    },
                    params={
                        "cql": cql,
                        "limit": limit,
                        "expand": "space,version"
                    }
                )
                response.raise_for_status()

                data = response.json()
                contents = data.get("results", [])

                logger.info(f"Confluence search returned {len(contents)} results for workspace {self.workspace_id}")
                return contents

        except Exception as e:
            logger.error(f"Confluence search failed: {e}")
            raise

    async def get_page(self, page_id: str, expand: str = "body.storage,version,space") -> Dict[str, Any]:
        """
        Retrieve a specific Confluence page by ID.

        Args:
            page_id: Confluence page ID
            expand: Comma-separated list of properties to expand

        Returns:
            Page details with content
        """
        if not self.base_url or not self._access_token:
            raise ValueError("Confluence API not configured")

        try:
            async with httpx.AsyncClient() as client:
                response = await client.get(
                    f"{self.base_url}/content/{page_id}",
                    headers={
                        "Authorization": f"Bearer {self._access_token}"
                    },
                    params={"expand": expand}
                )
                response.raise_for_status()

                page = response.json()

                logger.info(f"Retrieved Confluence page {page_id} for workspace {self.workspace_id}")
                return page

        except Exception as e:
            logger.error(f"Failed to retrieve Confluence page {page_id}: {e}")
            raise

    async def create_page(self, space_key: str, title: str, content: str,
                         parent_id: str = None) -> Dict[str, Any]:
        """
        Create a new Confluence page.

        Args:
            space_key: Space key
            title: Page title
            content: Page content (storage format)
            parent_id: Parent page ID (optional)

        Returns:
            Created page object
        """
        if not self.base_url or not self._access_token:
            raise ValueError("Confluence API not configured")

        try:
            # Build page data
            page_data = {
                "type": "page",
                "title": title,
                "space": {"key": space_key},
                "body": {
                    "storage": {
                        "value": content,
                        "representation": "storage"
                    }
                }
            }

            if parent_id:
                page_data["ancestors"] = [{"id": parent_id}]

            async with httpx.AsyncClient() as client:
                response = await client.post(
                    f"{self.base_url}/content",
                    headers={
                        "Authorization": f"Bearer {self._access_token}",
                        "Content-Type": "application/json"
                    },
                    json=page_data
                )
                response.raise_for_status()

                page = response.json()

                logger.info(f"Created Confluence page {page.get('id')} for workspace {self.workspace_id}")
                return page

        except Exception as e:
            logger.error(f"Failed to create Confluence page: {e}")
            raise

    async def update_page(self, page_id: str, title: str = None,
                         content: str = None, version: int = None) -> Dict[str, Any]:
        """
        Update a Confluence page.

        Args:
            page_id: Page ID to update
            title: New page title
            content: New page content
            version: Version number (must increment)

        Returns:
            Updated page object
        """
        if not self.base_url or not self._access_token:
            raise ValueError("Confluence API not configured")

        try:
            # Get current page if version not provided
            if version is None:
                current_page = await self.get_page(page_id, expand="version")
                version = current_page.get("version", {}).get("number", 1) + 1

            # Build update data
            update_data = {
                "id": page_id,
                "type": "page",
                "version": {"number": version}
            }

            if title:
                update_data["title"] = title

            if content:
                update_data["body"] = {
                    "storage": {
                        "value": content,
                        "representation": "storage"
                    }
                }

            async with httpx.AsyncClient() as client:
                response = await client.put(
                    f"{self.base_url}/content/{page_id}",
                    headers={
                        "Authorization": f"Bearer {self._access_token}",
                        "Content-Type": "application/json"
                    },
                    json=update_data
                )
                response.raise_for_status()

                page = response.json()

                logger.info(f"Updated Confluence page {page_id} in workspace {self.workspace_id}")
                return page

        except Exception as e:
            logger.error(f"Failed to update Confluence page {page_id}: {e}")
            raise

    async def delete_page(self, page_id: str) -> bool:
        """
        Delete a Confluence page.

        Args:
            page_id: Page ID to delete

        Returns:
            True if successful
        """
        if not self.base_url or not self._access_token:
            raise ValueError("Confluence API not configured")

        try:
            async with httpx.AsyncClient() as client:
                response = await client.delete(
                    f"{self.base_url}/content/{page_id}",
                    headers={
                        "Authorization": f"Bearer {self._access_token}"
                    }
                )
                response.raise_for_status()

                logger.info(f"Deleted Confluence page {page_id} in workspace {self.workspace_id}")
                return True

        except Exception as e:
            logger.error(f"Failed to delete Confluence page {page_id}: {e}")
            return False

    async def get_spaces(self, limit: int = 25) -> List[Dict[str, Any]]:
        """
        Retrieve all Confluence spaces.

        Args:
            limit: Maximum number of results

        Returns:
            List of space objects
        """
        if not self.base_url or not self._access_token:
            raise ValueError("Confluence API not configured")

        try:
            async with httpx.AsyncClient() as client:
                response = await client.get(
                    f"{self.base_url}/space",
                    headers={
                        "Authorization": f"Bearer {self._access_token}"
                    },
                    params={
                        "limit": limit,
                        "expand": "description"
                    }
                )
                response.raise_for_status()

                data = response.json()
                spaces = data.get("results", [])

                logger.info(f"Retrieved {len(spaces)} Confluence spaces for workspace {self.workspace_id}")
                return spaces

        except Exception as e:
            logger.error(f"Failed to retrieve Confluence spaces: {e}")
            raise

    async def add_comment(self, page_id: str, text: str) -> Dict[str, Any]:
        """
        Add a comment to a Confluence page.

        Args:
            page_id: Page ID
            text: Comment text (storage format)

        Returns:
            Created comment object
        """
        if not self.base_url or not self._access_token:
            raise ValueError("Confluence API not configured")

        try:
            async with httpx.AsyncClient() as client:
                response = await client.post(
                    f"{self.base_url}/content/{page_id}/child/comment",
                    headers={
                        "Authorization": f"Bearer {self._access_token}",
                        "Content-Type": "application/json"
                    },
                    json={
                        "type": "comment",
                        "body": {
                            "storage": {
                                "value": text,
                                "representation": "storage"
                            }
                        }
                    }
                )
                response.raise_for_status()

                comment = response.json()

                logger.info(f"Added comment to Confluence page {page_id} in workspace {self.workspace_id}")
                return comment

        except Exception as e:
            logger.error(f"Failed to add comment to Confluence page {page_id}: {e}")
            raise

    async def get_attachments(self, page_id: str, limit: int = 25) -> List[Dict[str, Any]]:
        """
        Retrieve all attachments for a Confluence page.

        Args:
            page_id: Page ID
            limit: Maximum number of results

        Returns:
            List of attachment objects
        """
        if not self.base_url or not self._access_token:
            raise ValueError("Confluence API not configured")

        try:
            async with httpx.AsyncClient() as client:
                response = await client.get(
                    f"{self.base_url}/content/{page_id}/child/attachment",
                    headers={
                        "Authorization": f"Bearer {self._access_token}"
                    },
                    params={
                        "limit": limit,
                        "expand": "version"
                    }
                )
                response.raise_for_status()

                data = response.json()
                attachments = data.get("results", [])

                logger.info(f"Retrieved {len(attachments)} attachments for page {page_id}")
                return attachments

        except Exception as e:
            logger.error(f"Failed to retrieve attachments for page {page_id}: {e}")
            raise

    async def get_page_children(self, page_id: str, limit: int = 25) -> List[Dict[str, Any]]:
        """
        Retrieve all child pages of a Confluence page.

        Args:
            page_id: Parent page ID
            limit: Maximum number of results

        Returns:
            List of child page objects
        """
        if not self.base_url or not self._access_token:
            raise ValueError("Confluence API not configured")

        try:
            async with httpx.AsyncClient() as client:
                response = await client.get(
                    f"{self.base_url}/content/{page_id}/child/page",
                    headers={
                        "Authorization": f"Bearer {self._access_token}"
                    },
                    params={
                        "limit": limit,
                        "expand": "version"
                    }
                )
                response.raise_for_status()

                data = response.json()
                children = data.get("results", [])

                logger.info(f"Retrieved {len(children)} child pages for page {page_id}")
                return children

        except Exception as e:
            logger.error(f"Failed to retrieve child pages for {page_id}: {e}")
            raise