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
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
"""
Knowledge Base API
知識タイル一覧表示と検証マーク機構
"""
from fastapi import APIRouter, Depends, HTTPException, Query, UploadFile, File
from fastapi.responses import FileResponse, StreamingResponse
from typing import List, Optional
from datetime import datetime
import os
import json
import io
from sqlalchemy.orm import Session
import logging
from uuid import uuid4

from backend.app.middleware.auth import get_current_user_optional, get_current_user, User
from backend.app.config import settings
from backend.app.database.session import get_db
from backend.app.services.knowledge_service import KnowledgeService, get_knowledge_service
from backend.app.schemas.knowledge import KnowledgeTile, KnowledgeListResponse, KnowledgeDetailResponse, EditRequest, VerificationMark
from backend.app.utils.iath_encoder import IathEncoder

logger = logging.getLogger(__name__)

router = APIRouter()


@router.get("/", response_model=KnowledgeListResponse)
async def list_knowledge_tiles(
    domain_id: Optional[str] = Query(None, description="ドメインでフィルタ"),
    verification_type: Optional[str] = Query(None, description="検証タイプでフィルタ"),
    search: Optional[str] = Query(None, description="検索クエリ"),
    page: int = Query(1, ge=1, description="ページ番号"),
    page_size: int = Query(20, ge=1, le=100, description="ページサイズ"),
    db: Session = Depends(get_db),
    service: KnowledgeService = Depends(get_knowledge_service)
):
    tiles_orm, total_count = service.list_tiles(
        db=db, page=page, page_size=page_size, 
        domain_id=domain_id, verification_type=verification_type, search=search
    )
    
    tiles_pydantic = [KnowledgeTile.from_orm(t) for t in tiles_orm]

    return KnowledgeListResponse(
        tiles=tiles_pydantic,
        total_count=total_count,
        page=page,
        page_size=page_size,
        has_more=(page * page_size) < total_count
    )


@router.get("/{tile_id}", response_model=KnowledgeDetailResponse)
async def get_knowledge_tile(
    tile_id: str,
    db: Session = Depends(get_db),
    service: KnowledgeService = Depends(get_knowledge_service)
):
    tile_orm = service.get_tile(db, tile_id=tile_id)

    if not tile_orm:
        raise HTTPException(status_code=404, detail="Knowledge tile not found")
    
    tile_pydantic = KnowledgeTile.from_orm(tile_orm)

    return KnowledgeDetailResponse(
        tile=tile_pydantic,
        full_content=tile_orm.content,
        # TODO: Implement sources, related_tiles, and edit_history from DB
        sources=[],
        related_tiles=[],
        edit_history=[]
    )


@router.put("/{tile_id}", response_model=KnowledgeTile)
async def update_knowledge_tile(
    tile_id: str,
    request: EditRequest,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db),
    service: KnowledgeService = Depends(get_knowledge_service)
):
    updated_tile_orm = service.update_tile(
        db=db, tile_id=tile_id, content=request.content, user=current_user
    )

    if not updated_tile_orm:
        raise HTTPException(status_code=404, detail="Knowledge tile not found")

    return KnowledgeTile.from_orm(updated_tile_orm)


@router.get("/coordinates")
async def get_coordinates_for_3d_visualization(
    domain_id: Optional[str] = Query(None, description="特定ドメインのみ取得"),
    db: Session = Depends(get_db),
    service: KnowledgeService = Depends(get_knowledge_service)
):
    """
    3D可視化用の座標データを取得
    座標を持つタイルのみを返し、必要最小限の情報のみを含める
    """
    # Fetch all tiles (large page size to get all)
    tiles_orm, _ = service.list_tiles(db=db, page_size=10000, domain_id=domain_id)

    # Filter tiles that have coordinates and extract minimal data
    coordinates_data = []
    for tile in tiles_orm:
        if tile.coordinates:  # Only include tiles with coordinates
            coordinates_data.append({
                "tile_id": tile.id,
                "topic": tile.topic,
                "domain_id": tile.domain_id,
                "coordinates": tile.coordinates,  # [x, y, z, c, g, v]
                "confidence_score": tile.confidence_score,
                "verification_type": tile.verification_type
            })

    return {
        "tiles": coordinates_data,
        "count": len(coordinates_data),
        "domain_id": domain_id or "all"
    }


@router.get("/export/json")
async def export_db_json(
    domain_id: Optional[str] = Query(None, description="特定ドメインのみエクスポート"),
    db: Session = Depends(get_db),
    service: KnowledgeService = Depends(get_knowledge_service)
):
    # Fetch all tiles for export
    tiles_orm, _ = service.list_tiles(db=db, page_size=10000, domain_id=domain_id) # A large page size to get all
    tiles_pydantic = [KnowledgeTile.from_orm(t).dict() for t in tiles_orm]

    export_data = {
        "metadata": {
            "export_date": datetime.now().isoformat(),
            "source": "NullAI Knowledge Base",
            "domain_filter": domain_id or "all",
            "tile_count": len(tiles_pydantic)
        },
        "tiles": tiles_pydantic
    }

    json_str = json.dumps(export_data, indent=2, ensure_ascii=False, default=str)

    return StreamingResponse(
        io.BytesIO(json_str.encode('utf-8')),
        media_type="application/json",
        headers={
            "Content-Disposition": f"attachment; filename=null_ai_knowledge_{datetime.now().strftime('%Y%m%d')}.json"
        }
    )


@router.get("/export/iath")
async def export_db_iath(
    domain_id: Optional[str] = Query(None, description="特定ドメインのみエクスポート"),
    precision: str = Query("standard", description="精度レベル: standard または high_precision"),
    db: Session = Depends(get_db),
    service: KnowledgeService = Depends(get_knowledge_service)
):
    """
    .iath形式でエクスポート (dendritic-memory-editor互換)
    JSON version 2形式で出力します

    precision:
    - standard: List Viewからの標準エクスポート
    - high_precision: 3D Space Viewからの高精度エクスポート
    """
    logger.info(f"Exporting to .iath format (domain={domain_id}, precision={precision})")

    # Fetch all tiles for export
    tiles_orm, _ = service.list_tiles(db=db, page_size=10000, domain_id=domain_id)

    # Map domain_id to domain name
    domain_name_map = {
        "medical": "Medical",
        "ai_fundamentals": "AI Fundamentals",
        "logic_reasoning": "Logic & Reasoning",
        "computer_science_theory": "Computer Science",
        "engineering": "Engineering",
        "philosophy": "Philosophy",
        "law": "Law"
    }
    domain_name = domain_name_map.get(domain_id, "General") if domain_id else "General"

    # Map domain_id to domain code
    domain_code_map = {
        "medical": 1,
        "ai_fundamentals": 2,
        "logic_reasoning": 3,
        "computer_science_theory": 4,
        "engineering": 5,
        "philosophy": 6,
        "law": 7
    }
    domain_code = domain_code_map.get(domain_id, 1) if domain_id else 1

    # Convert tiles to .iath JSON format (version 2)
    iath_tiles = []
    for tile in tiles_orm:
        # Parse coordinates
        coordinates = tile.coordinates
        if not coordinates or len(coordinates) < 3:
            # Generate basic coordinates if missing
            confidence = tile.confidence_score if tile.confidence_score else 0.5
            coordinates = [
                50.0 + (hash(tile.id) % 100) / 2,  # x: pseudo-random positioning
                50.0 + (hash(tile.id[::-1]) % 100) / 2,  # y: pseudo-random positioning
                10.0 + confidence * 20  # z: based on confidence
            ]
            logger.warning(f"Tile {tile.id} missing coordinates, generated: {coordinates}")

        # Extract x, y, z from coordinates
        x = round(coordinates[0], 2) if len(coordinates) > 0 else 50.0
        y = round(coordinates[1], 2) if len(coordinates) > 1 else 50.0
        z = round(coordinates[2], 2) if len(coordinates) > 2 else 10.0

        # Map verification types to dendritic-memory-editor compatible values
        verification_map = {
            "ai": "pending_review",
            "community": "community",
            "expert": "expert_verified",
            "multi_expert": "expert_verified",
            "none": "pending_review"
        }
        verification_status = verification_map.get(tile.verification_type, "pending_review")

        # Map author marks to dendritic-memory-editor compatible values
        author_mark_map = {
            "ai": "community",
            "community": "community",
            "expert": "expert",
            "multi_expert": "expert",
            "none": "community"
        }
        author_mark = author_mark_map.get(tile.verification_type, "community")

        # Generate author_id if not present (dendritic-memory-editor expects this field)
        author_id = tile.author_id if hasattr(tile, 'author_id') and tile.author_id else "system"

        # Build .iath compatible tile structure (version 2 format)
        iath_tile = {
            "id": tile.id,
            "title": tile.topic,
            "coordinates": {
                "x": x,
                "y": y,
                "z": z
            },
            "content": tile.content,
            "verification_status": verification_status,
            "created_at": tile.created_at.strftime("%Y-%m-%d %H:%M:%S") if tile.created_at else datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            "updated_at": tile.updated_at.strftime("%Y-%m-%d %H:%M:%S") if tile.updated_at else datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            "author_id": author_id,
            "author_mark": author_mark
        }

        iath_tiles.append(iath_tile)

    # Create version 2 JSON format
    export_data = {
        "version": 2,
        "header": {
            "domain_code": domain_code,
            "tile_count": len(iath_tiles),
            "created_at": datetime.now().isoformat() + "Z",
            "domain": domain_name
        },
        "tiles": iath_tiles
    }

    # Convert to JSON
    json_str = json.dumps(export_data, ensure_ascii=False, indent=None)
    logger.info(f"Successfully created .iath JSON with {len(iath_tiles)} tiles")

    # Generate filename
    domain_suffix = f"_{domain_name.replace(' ', '_')}" if domain_id else ""
    filename = f"{domain_name.replace(' ', '_')}_tiles_{int(datetime.now().timestamp() * 1000)}.iath"

    return StreamingResponse(
        io.BytesIO(json_str.encode('utf-8')),
        media_type="application/json",
        headers={
            "Content-Disposition": f"attachment; filename={filename}"
        }
    )


@router.patch("/{tile_id}/domain")
async def update_tile_domain(
    tile_id: str,
    domain_id: str = Query(..., description="新しいドメインID"),
    db: Session = Depends(get_db),
    service: KnowledgeService = Depends(get_knowledge_service)
):
    """
    知識タイルのドメインを更新 (List Viewからのドメイン編集機能)
    """
    tile_orm = service.get_tile(db, tile_id=tile_id)

    if not tile_orm:
        raise HTTPException(status_code=404, detail="Knowledge tile not found")

    # Update domain
    tile_orm.domain_id = domain_id
    tile_orm.updated_at = datetime.utcnow()
    db.commit()
    db.refresh(tile_orm)

    logger.info(f"Updated tile {tile_id} domain to {domain_id}")

    return KnowledgeTile.from_orm(tile_orm)


@router.post("/import/iath")
async def import_iath_file(
    file: UploadFile = File(...),
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db),
    service: KnowledgeService = Depends(get_knowledge_service)
):
    """
    .iath ファイルをインポートしてデータベースに保存

    - JSON version 2 形式の .iath ファイルをサポート
    - 既存のタイルIDがある場合は更新、ない場合は新規作成
    - エラーが発生したタイルは個別にログに記録
    """
    try:
        # ファイル内容を読み込む
        content = await file.read()
        iath_data = json.loads(content.decode('utf-8'))

        # .iath フォーマットを検証
        if not iath_data.get("version") or not iath_data.get("header") or not iath_data.get("tiles"):
            raise HTTPException(
                status_code=400,
                detail="Invalid .iath file format. Required fields: version, header, tiles"
            )

        if not isinstance(iath_data["tiles"], list):
            raise HTTPException(
                status_code=400,
                detail="Invalid .iath file format. 'tiles' must be an array"
            )

        # ヘッダー情報を取得
        header = iath_data["header"]
        domain_name = header.get("domain", "General")

        # ドメイン名からドメインIDにマッピング
        domain_name_to_id = {
            "Medical": "medical",
            "AI Fundamentals": "ai_fundamentals",
            "Logic & Reasoning": "logic_reasoning",
            "Computer Science": "computer_science_theory",
            "Engineering": "engineering",
            "Philosophy": "philosophy",
            "Law": "law",
            "General": "general"
        }
        domain_id = domain_name_to_id.get(domain_name, "general")

        imported_count = 0
        updated_count = 0
        error_count = 0
        errors = []

        logger.info(f"Starting import of {len(iath_data['tiles'])} tiles from domain: {domain_name}")

        # 各タイルをインポート
        for tile_data in iath_data["tiles"]:
            try:
                tile_id = tile_data.get("id")
                if not tile_id:
                    tile_id = str(uuid4())

                # コンテンツを取得
                content_text = tile_data.get("content", "")
                topic = tile_data.get("title", "")

                if not content_text and not topic:
                    error_msg = f"Tile {tile_id}: No content or title found"
                    logger.warning(error_msg)
                    errors.append(error_msg)
                    error_count += 1
                    continue

                # 座標を取得
                coords = tile_data.get("coordinates", {})
                coordinates = [
                    coords.get("x", 50.0),
                    coords.get("y", 50.0),
                    coords.get("z", 50.0)
                ]

                # verification_status を verification_type にマッピング
                verification_map = {
                    "pending_review": "ai",
                    "community": "community",
                    "expert_verified": "expert",
                    "multi_expert": "multi_expert"
                }
                verification_status = tile_data.get("verification_status", "pending_review")
                verification_type = verification_map.get(verification_status, "ai")

                # author_mark を検証
                author_mark = tile_data.get("author_mark", "community")
                if author_mark == "expert":
                    verification_type = "expert"

                # 既存のタイルをチェック
                existing_tile = service.get_tile(db, tile_id=tile_id)

                if existing_tile:
                    # 既存タイルを更新
                    existing_tile.domain_id = domain_id
                    existing_tile.topic = topic or existing_tile.topic
                    existing_tile.content = content_text
                    existing_tile.coordinates = coordinates
                    existing_tile.verification_type = verification_type
                    existing_tile.updated_at = datetime.utcnow()

                    db.commit()
                    db.refresh(existing_tile)

                    updated_count += 1
                    logger.info(f"Updated tile {tile_id}")
                else:
                    # 新規タイルを作成
                    from backend.app.database.models import KnowledgeTileModel

                    new_tile = KnowledgeTileModel(
                        id=tile_id,
                        domain_id=domain_id,
                        topic=topic or "Imported",
                        content=content_text,
                        coordinates=coordinates,
                        verification_type=verification_type,
                        confidence_score=0.8,
                        author_id=current_user.id if current_user else None,
                        created_at=datetime.utcnow(),
                        updated_at=datetime.utcnow()
                    )

                    db.add(new_tile)
                    db.commit()
                    db.refresh(new_tile)

                    imported_count += 1
                    logger.info(f"Imported new tile {tile_id}")

            except Exception as tile_error:
                error_msg = f"Tile {tile_data.get('id', 'unknown')}: {str(tile_error)}"
                logger.error(error_msg, exc_info=True)
                errors.append(error_msg)
                error_count += 1
                # 次のタイルに続行
                continue

        return {
            "success": True,
            "domain": domain_name,
            "total_tiles": len(iath_data["tiles"]),
            "imported": imported_count,
            "updated": updated_count,
            "errors": error_count,
            "error_details": errors if errors else None,
            "message": f"Successfully processed {imported_count + updated_count} tiles from {domain_name}"
        }

    except json.JSONDecodeError as e:
        logger.error(f"JSON decode error: {e}", exc_info=True)
        raise HTTPException(
            status_code=400,
            detail=f"Invalid JSON format: {str(e)}"
        )
    except Exception as e:
        logger.error(f"Import error: {e}", exc_info=True)
        raise HTTPException(
            status_code=500,
            detail=f"Failed to import .iath file: {str(e)}"
        )