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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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
# Copyright (C) 2026 Mattia Zatelli
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

"""
Endpoints for BIM project management

These endpoints handle the complete lifecycle of BIM projects:
- IFC file upload with validation
- Processing status monitoring
- Project metadata management
- Access to processed information (elements, spaces, quantities)

All endpoints are protected by authentication and isolated per tenant.
"""
import logging
import hashlib
from typing import List
from fastapi import APIRouter, Depends, File, UploadFile, Form, HTTPException, Query, status, Request, BackgroundTasks
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, and_, or_
from sqlalchemy.orm import selectinload

from app.core.database import get_async_session
from app.models import Project, ProjectStatus
from app.schemas.project import (
    ProjectUploadRequest, ProjectUploadResponse, ProjectStatusResponse,
    ProjectSummaryResponse, ProjectListRequest, ProjectListResponse,
    ProjectQuantitiesResponse, ProjectSpacesResponse,
    UploadInitRequest, UploadInitResponse, FinalizeUploadResponse,
)
from app.schemas.common import ErrorResponse, SuccessResponse
from app.core.exceptions import ProjectNotFoundError, FileUploadError
from app.core.s3 import S3StorageService, get_s3_client

from app.core.config import get_settings
from app.core.redis import RedisStream

logger = logging.getLogger(__name__)
settings = get_settings()
router = APIRouter()


async def get_project_or_404(
    project_id: str,
    session: AsyncSession,
    tenant_id: str
) -> Project:
    """Retrieve project by ID or raise 404 error

    Utility function to retrieve projects with validation:
    - Converts external ID (prj_123) to internal ID
    - Verifies tenant ownership for data isolation
    - Raises ProjectNotFoundError if not found

    Args:
        project_id: External project ID format (e.g. prj_123)
        session: Async database session
        tenant_id: Tenant ID for data isolation

    Returns:
        Project: Found project instance

    Raises:
        ProjectNotFoundError: If project not found or not accessible
    """
    # Convert external ID to internal numeric ID
    try:
        internal_id = int(project_id.replace('prj_', ''))
    except ValueError:
        raise ProjectNotFoundError(project_id)

    # Query project with multi-tenant security filters
    query = select(Project).where(
        and_(
            Project.id == internal_id,      # Filter by specific ID
            Project.tenant_id == tenant_id  # IMPORTANT: tenant isolation
        )
    )
    result = await session.execute(query)
    project = result.scalar_one_or_none()

    if not project:
        raise ProjectNotFoundError(project_id)

    return project


@router.post("/upload", response_model=ProjectUploadResponse)
async def upload_project(
    request: Request,
    background_tasks: BackgroundTasks,
    file: UploadFile = File(...),
    project_name: str = Form(None),
    description: str = Form(None),
    region: str = Form("us"),
    session: AsyncSession = Depends(get_async_session)
):
    """
    Upload IFC file for processing

    This endpoint handles IFC file upload with full validation:

    **Validations performed:**
    - File extension (.ifc, .ifczip)
    - Maximum size (500MB)
    - Non-empty file
    - SHA256 hash to detect duplicates

    **Process:**
    1. File and metadata validation
    2. Upload to S3 storage
    3. Database record creation
    4. Start async processing (Celery)
    5. Immediate return with project ID

    **Async processing:**
    - IFC parsing with ifcopenshell
    - Element and metadata extraction
    - Geometry and property calculation
    - Query index generation

    Args:
        file: IFC file to process
        project_name: Optional project name
        description: Optional description
        region: Region for local compliance

    Returns:
        ProjectUploadResponse: Project ID and initial status

    Raises:
        FileUploadError: If file is invalid
        QuotaExceededError: If tenant limits exceeded
    """
    # Retrieve authentication info from middleware
    tenant_id = getattr(request.state, "tenant_id", "unknown")
    api_key_hash = getattr(request.state, "api_key_hash", None)
    request_id = getattr(request.state, "request_id", "unknown")

    logger.info(f"Starting project upload for tenant {tenant_id}: {file.filename}")

    # === FILE VALIDATION ===

    # Check filename presence
    if not file.filename:
        raise FileUploadError("Missing filename")

    # Sanitize filename: reject null bytes, path separators, excessive length
    if '\x00' in file.filename or '/' in file.filename or '\\' in file.filename:
        raise FileUploadError("Invalid filename")
    if len(file.filename) > 255:
        raise FileUploadError("Filename too long (max 255 characters)")

    # Check supported file extension using pathlib for safe extraction
    from pathlib import PurePosixPath
    file_ext = PurePosixPath(file.filename).suffix.lower()
    allowed_extensions = settings.ALLOWED_IFC_EXTENSIONS
    if file_ext not in allowed_extensions:
        raise FileUploadError(f"Unsupported file type. Supported: {', '.join(allowed_extensions)}")

    # Check file size
    max_size = settings.MAX_UPLOAD_SIZE
    file.file.seek(0, 2)  # Move pointer to end
    file_size = file.file.tell()
    file.file.seek(0)  # Back to beginning

    if file_size > max_size:
        from fastapi import status
        raise FileUploadError(
            f"File too large (maximum {max_size // (1024 * 1024)}MB)",
            status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
        )

    if file_size == 0:
        raise FileUploadError("Empty file not allowed")

    # Validate IFC magic bytes (IFC files start with "ISO-10303-21")
    magic_header = file.file.read(64)
    file.file.seek(0)
    if file_ext == '.ifc' and not magic_header.strip().startswith(b'ISO-10303-21'):
        raise FileUploadError("File content does not match IFC format")

    try:
        # Generate project ID and prepare for upload
        project_name = project_name or file.filename.rsplit('.', 1)[0]

        # Calculate file hash before creating project record
        file.file.seek(0)  # Reset to beginning
        file_content = file.file.read()
        file.file.seek(0)  # Reset for later use
        file_hash = hashlib.sha256(file_content).hexdigest()
        actual_size = len(file_content)

        # Check if a project with this file hash already exists for this tenant
        existing_query = select(Project).where(
            and_(
                Project.tenant_id == tenant_id,
                Project.file_hash == file_hash
            )
        )
        existing_result = await session.execute(existing_query)
        existing_project = existing_result.scalar_one_or_none()

        if existing_project:
            # File already exists - return the existing project info
            logger.info(f"File with hash {file_hash[:8]} already exists for tenant {tenant_id} as project {existing_project.id}")

            # Touch last access for the existing project
            existing_project.touch_last_access()
            await session.commit()

            project_public_id = f"prj_{existing_project.id}"

            return ProjectUploadResponse(
                project_id=project_public_id,
                status=existing_project.status,
                message=f"File already exists as project {project_public_id}",
                file_info={
                    "filename": file.filename,
                    "size": actual_size,
                    "content_type": file.content_type or "application/octet-stream",
                    "checksum": file_hash
                },
                estimated_processing_time=0 if existing_project.is_ready() else 300
            )

        # Generate S3 object key using UUID (doesn't depend on DB ID)
        import uuid
        file_uuid = str(uuid.uuid4())
        object_key = f"projects/{file_uuid}/files/{file.filename}"

        # Create project record with pre-calculated S3 key
        project = Project(
            tenant_id=tenant_id,
            api_key_hash=api_key_hash,
            name=project_name,
            description=description,
            original_filename=file.filename,
            file_size=actual_size,
            file_hash=file_hash,
            status=ProjectStatus.QUEUED,
            s3_bucket="bim-files",  # From config
            s3_key=object_key,  # Set the S3 key before database insertion
            s3_region=region
        )

        session.add(project)
        await session.flush()  # Get the ID
        await session.refresh(project)

        project_public_id = f"prj_{project.id}"

        # Upload file content to S3. `settings` is already imported at
        # the top of the module; redefining it locally turned every
        # earlier reference (allowed_extensions, max_size) into an
        # UnboundLocalError because Python promoted `settings` to a
        # function-local name for the whole frame.
        s3_client = get_s3_client()

        s3_client.put_object(
            Bucket=settings.S3_BUCKET,
            Key=object_key,
            Body=file_content,
            ContentType=file.content_type or "application/octet-stream",
            Metadata={
                'project-id': project_public_id,
                'filename': file.filename,
                'sha256': file_hash
            }
        )

        # Enqueue processing task (using Celery directly)
        from app.tasks.ifc_processing import process_ifc_file
        task_data = {
            "project_id": project_public_id,
            "tenant_id": tenant_id,
            "s3_bucket": project.s3_bucket,
            "s3_key": object_key,
            "filename": file.filename,
            "file_size": actual_size,
            "file_hash": file_hash,
            "request_id": request_id
        }

        try:
            task_result = process_ifc_file.delay(task_data)
            logger.info(f"Started IFC processing task {task_result.id} for project {project_public_id}")
        except Exception as e:
            logger.error(f"Failed to start IFC processing task: {e}")
            # Continue anyway - project is uploaded

        await session.commit()

        logger.info(f"Project {project_public_id} uploaded successfully")

        return ProjectUploadResponse(
            project_id=project_public_id,
            status=ProjectStatus.QUEUED,
            message="File uploaded successfully, processing started",
            file_info={
                "filename": file.filename,
                "size": actual_size,
                "content_type": file.content_type or "application/octet-stream",
                "checksum": file_hash
            },
            estimated_processing_time=300  # 5 minutes estimate
        )

    except Exception as e:
        await session.rollback()
        logger.error(f"Upload failed for tenant {tenant_id}: {e}")
        if isinstance(e, (FileUploadError, HTTPException)):
            raise
        raise FileUploadError("Upload failed. Please try again.")


# ---------------------------------------------------------------------------
# Presigned-upload flow (direct-to-R2)
#
# The proxied /upload endpoint above is limited by the request body size of
# whatever sits between the client and us (Vercel hobby tier caps at ~4.5
# MB). For real-world IFC files this is too tight. The two endpoints below
# implement an S3-style two-step upload:
#
#   1. POST /projects/upload-init  -> returns a presigned PUT URL for R2
#      The client uploads the file straight to R2; the bytes never touch
#      our API or any proxy.
#   2. POST /projects/{id}/finalize -> the client tells us it's done, we
#      head_object to verify and enqueue the Celery processing task.
# ---------------------------------------------------------------------------

@router.post("/upload-init", response_model=UploadInitResponse)
async def init_upload(
    body: UploadInitRequest,
    request: Request,
    session: AsyncSession = Depends(get_async_session),
):
    """Validate metadata, create a placeholder project row, and return a
    presigned PUT URL the client uses to upload the IFC directly to R2.
    """
    tenant_id = getattr(request.state, "tenant_id", "unknown")
    api_key_hash = getattr(request.state, "api_key_hash", None)

    # Validate filename
    if not body.filename or '\x00' in body.filename or '/' in body.filename or '\\' in body.filename:
        raise FileUploadError("Invalid filename")
    if len(body.filename) > 255:
        raise FileUploadError("Filename too long (max 255 characters)")

    from pathlib import PurePosixPath
    file_ext = PurePosixPath(body.filename).suffix.lower()
    if file_ext not in settings.ALLOWED_IFC_EXTENSIONS:
        raise FileUploadError(
            f"Unsupported file type. Supported: {', '.join(settings.ALLOWED_IFC_EXTENSIONS)}"
        )

    # Validate size against the configured cap
    if body.file_size > settings.MAX_UPLOAD_SIZE:
        from fastapi import status as http_status
        raise FileUploadError(
            f"File too large (maximum {settings.MAX_UPLOAD_SIZE // (1024 * 1024)}MB)",
            status_code=http_status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
        )

    # Generate a fresh S3 key
    import uuid
    file_uuid = str(uuid.uuid4())
    object_key = f"projects/{file_uuid}/files/{body.filename}"

    # Insert placeholder project row. file_hash is filled in finalize().
    project = Project(
        tenant_id=tenant_id,
        api_key_hash=api_key_hash,
        name=body.project_name or body.filename,
        description=body.description,
        original_filename=body.filename,
        file_size=body.file_size,
        file_hash="",  # populated in /finalize after head_object
        status=ProjectStatus.QUEUED,
        s3_bucket=settings.S3_BUCKET,
        s3_key=object_key,
        s3_region=body.region or settings.S3_REGION,
    )
    session.add(project)
    await session.commit()
    await session.refresh(project)

    project_public_id = f"prj_{project.id}"

    # Mint the presigned PUT URL. The client MUST send a matching
    # Content-Type header on the upload — boto3 includes the
    # ContentType in the signature.
    content_type = body.content_type or "application/octet-stream"
    s3_client = get_s3_client()
    upload_url = s3_client.generate_presigned_url(
        "put_object",
        Params={
            "Bucket": settings.S3_BUCKET,
            "Key": object_key,
            "ContentType": content_type,
        },
        ExpiresIn=3600,
    )

    from datetime import datetime, timedelta, timezone
    expires_at = (datetime.now(timezone.utc) + timedelta(seconds=3600)).isoformat()

    logger.info(
        f"Initialised upload for tenant {tenant_id}: {body.filename} "
        f"({body.file_size} bytes) -> {project_public_id}"
    )

    return UploadInitResponse(
        project_id=project_public_id,
        upload_url=upload_url,
        upload_method="PUT",
        content_type=content_type,
        expires_at=expires_at,
    )


@router.post("/{project_public_id}/finalize", response_model=FinalizeUploadResponse)
async def finalize_upload(
    project_public_id: str,
    request: Request,
    session: AsyncSession = Depends(get_async_session),
):
    """Confirm a presigned upload landed in R2 and enqueue processing."""
    tenant_id = getattr(request.state, "tenant_id", "unknown")
    request_id = getattr(request.state, "request_id", "unknown")

    if not project_public_id.startswith("prj_"):
        raise ProjectNotFoundError(project_public_id)
    try:
        project_id = int(project_public_id[4:])
    except ValueError:
        raise ProjectNotFoundError(project_public_id)

    query = select(Project).where(
        and_(Project.id == project_id, Project.tenant_id == tenant_id)
    )
    result = await session.execute(query)
    project = result.scalar_one_or_none()
    if not project:
        raise ProjectNotFoundError(project_public_id)

    # Verify the file actually landed in R2.
    s3_client = get_s3_client()
    try:
        head = s3_client.head_object(Bucket=project.s3_bucket, Key=project.s3_key)
    except Exception as e:
        logger.error(f"Finalize failed: head_object for {project.s3_key}: {e}")
        raise FileUploadError(
            "We can't see the uploaded file in storage. Did the PUT succeed?"
        )

    actual_size = int(head.get("ContentLength", 0))
    etag = head.get("ETag", "").strip('"')

    project.file_size = actual_size
    # ETag is per-storage object identifier; not a true SHA256, but unique
    # per object for our dedup checks.
    if not project.file_hash:
        project.file_hash = etag
    project.touch_last_access()

    # Enqueue Celery processing
    from app.tasks.ifc_processing import process_ifc_file
    task_data = {
        "project_id": project_public_id,
        "tenant_id": tenant_id,
        "s3_bucket": project.s3_bucket,
        "s3_key": project.s3_key,
        "filename": project.original_filename,
        "file_size": actual_size,
        "file_hash": project.file_hash,
        "request_id": request_id,
    }
    try:
        task_result = process_ifc_file.delay(task_data)
        logger.info(f"Queued IFC processing {task_result.id} for {project_public_id}")
    except Exception as e:
        logger.error(f"Failed to enqueue processing task for {project_public_id}: {e}")

    await session.commit()

    return FinalizeUploadResponse(
        project_id=project_public_id,
        status=project.status,
        file_size=actual_size,
        estimated_processing_time=300,
    )


@router.get("/{project_id}/status", response_model=ProjectStatusResponse)
async def get_project_status(
    project_id: str,
    request: Request,
    session: AsyncSession = Depends(get_async_session)
):
    """
    Get project processing status
    """
    tenant_id = getattr(request.state, "tenant_id", "unknown")
    project = await get_project_or_404(project_id, session, tenant_id)

    # Touch last access for tiered storage
    project.touch_last_access()
    await session.flush()

    # For processing projects, check Redis for real-time progress
    progress_info = project.get_progress_info()
    if project.is_processing():
        try:
            from app.core.redis import get_redis
            import json as _json
            redis = get_redis()
            cached_progress = await redis.get(f"project_progress:{project_id}")
            if cached_progress:
                real_time = _json.loads(cached_progress)
                progress_info["progress"]["percent"] = real_time["percent"]
                progress_info["progress"]["stage"] = real_time["stage"]
        except Exception:
            pass  # Fall back to DB progress

    return ProjectStatusResponse(
        project_id=project_id,
        name=project.name,
        status=project.status,
        progress=progress_info.get("progress") if progress_info else None,
        error=progress_info.get("error"),
        ifc_schema=project.ifc_schema,
        ifc_version=project.ifc_version,
        element_count=project.element_count,
        created_at=project.created_at,
        processed_at=project.processed_at,
        last_queried_at=project.last_queried_at
    )


@router.get("/{project_id}/summary", response_model=ProjectSummaryResponse)
async def get_project_summary(
    project_id: str,
    request: Request,
    session: AsyncSession = Depends(get_async_session)
):
    """
    Get project summary with statistics
    """
    tenant_id = getattr(request.state, "tenant_id", "unknown")
    project = await get_project_or_404(project_id, session, tenant_id)

    # Touch last access for tiered storage
    project.touch_last_access()
    await session.flush()

    if not project.is_ready():
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=f"Project is not ready (status: {project.status})"
        )

    # Build element statistics
    element_stats = None
    if project.element_count and project.element_count > 0:
        element_stats = {
            "total_elements": project.element_count,
            "by_type": project.element_types or {},
            "by_level": {},  # Would need to query elements table
            "by_material": {},  # Would need to query elements table
            "with_geometry": 0,  # Would need to calculate
            "with_properties": 0,  # Would need to calculate
            "with_materials": 0  # Would need to calculate
        }

    # Build spatial information
    spatial_info = None
    if project.project_info:
        spatial_info = {
            "site_name": project.site_info.get("site_name") if project.site_info else None,
            "building_name": project.site_info.get("building_name") if project.site_info else None,
            "levels": [],  # Would need to extract from elements
            "bounding_box": project.bounding_box,
            "gross_floor_area": {
                "value": project.gross_floor_area,
                "unit": "m2"
            } if project.gross_floor_area else None,
            "building_height": {
                "value": project.building_height,
                "unit": "m"
            } if project.building_height else None
        }

    return ProjectSummaryResponse(
        project_id=project_id,
        name=project.name,
        status=project.status,
        original_filename=project.original_filename,
        file_size=project.file_size,
        ifc_schema=project.ifc_schema,
        element_stats=element_stats,
        spatial_info=spatial_info,
        created_at=project.created_at,
        processed_at=project.processed_at,
        last_queried_at=project.last_queried_at
    )


@router.get("", response_model=ProjectListResponse)
async def list_projects(
    request: Request,
    status_filter: ProjectStatus = None,
    search: str = Query(None, max_length=200),
    limit: int = Query(50, ge=1, le=100),
    cursor: str = None,
    session: AsyncSession = Depends(get_async_session)
):
    """
    List projects for the authenticated tenant
    """
    tenant_id = getattr(request.state, "tenant_id", "unknown")

    # Build query
    query = select(Project).where(Project.tenant_id == tenant_id)

    # Apply filters
    if status_filter:
        query = query.where(Project.status == status_filter)

    if search:
        search_term = f"%{search.lower()}%"
        query = query.where(
            or_(
                Project.name.ilike(search_term),
                Project.description.ilike(search_term),
                Project.original_filename.ilike(search_term)
            )
        )

    # Apply cursor pagination
    if cursor:
        try:
            cursor_id = int(cursor)
            query = query.where(Project.id > cursor_id)
        except ValueError:
            pass  # Invalid cursor, ignore

    # Apply limit and ordering
    query = query.order_by(Project.created_at.desc()).limit(limit + 1)

    # Execute query
    result = await session.execute(query)
    projects = result.scalars().all()

    # Check if there are more results
    has_next = len(projects) > limit
    if has_next:
        projects = projects[:-1]  # Remove the extra item

    # Generate next cursor
    next_cursor = None
    if has_next and projects:
        next_cursor = str(projects[-1].id)

    # Convert to response format
    project_items = []
    for project in projects:
        project_items.append({
            "project_id": f"prj_{project.id}",
            "name": project.name,
            "status": project.status,
            "element_count": project.element_count,
            "file_size": project.file_size,
            "created_at": project.created_at,
            "processed_at": project.processed_at,
            "tags": []  # Would need tags field in model
        })

    return ProjectListResponse(
        projects=project_items,
        pagination={
            "limit": limit,
            "cursor": cursor,
            "has_next": has_next,
            "next_cursor": next_cursor,
            "total_count": None  # Could be expensive to calculate
        }
    )


@router.get("/{project_id}/quantities", response_model=ProjectQuantitiesResponse)
async def get_project_quantities(
    project_id: str,
    request: Request,
    session: AsyncSession = Depends(get_async_session)
):
    """
    Get project quantities (Bill of Quantities)
    """
    tenant_id = getattr(request.state, "tenant_id", "unknown")
    project = await get_project_or_404(project_id, session, tenant_id)

    # Touch last access for tiered storage
    project.touch_last_access()
    await session.flush()

    if not project.is_ready():
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=f"Project is not ready (status: {project.status})"
        )

    # This would typically involve complex calculations
    # For now, return a basic structure
    return ProjectQuantitiesResponse(
        project_id=project_id,
        generated_at=project.processed_at or project.created_at,
        items=[
            {
                "code": "WALLS_CONCRETE",
                "description": "Concrete walls",
                "unit": "m3",
                "quantity": 150.0
            },
            {
                "code": "WINDOWS",
                "description": "Windows",
                "unit": "m2",
                "quantity": 250.0
            }
        ],
        totals={
            "total_concrete": {"value": 150.0, "unit": "m3"},
            "total_openings": {"value": 250.0, "unit": "m2"}
        },
        methodology="IFC-based calculation",
        accuracy_level="medium"
    )


@router.get("/{project_id}/spaces", response_model=ProjectSpacesResponse)
async def get_project_spaces(
    project_id: str,
    request: Request,
    session: AsyncSession = Depends(get_async_session)
):
    """
    Get project spaces information
    """
    tenant_id = getattr(request.state, "tenant_id", "unknown")
    project = await get_project_or_404(project_id, session, tenant_id)

    # Touch last access for tiered storage
    project.touch_last_access()
    await session.flush()

    if not project.is_ready():
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=f"Project is not ready (status: {project.status})"
        )

    # This would query the elements table for spaces
    # For now, return a basic structure
    return ProjectSpacesResponse(
        project_id=project_id,
        spaces=[
            {
                "id": "sp_1",
                "name": "Office A1",
                "usage": "office",
                "area_sqm": 25.5,
                "level": "Level 1"
            }
        ],
        total_spaces=1,
        total_area={"value": 25.5, "unit": "m2"},
        by_usage={"office": 1},
        by_level={"Level 1": 1}
    )


@router.get("/{project_id}/download")
async def get_project_download_url(
    project_id: str,
    request: Request,
    session: AsyncSession = Depends(get_async_session)
):
    """
    Get a presigned URL to download the original IFC file.
    Used by the 3D viewer to load the model in the browser.
    """
    tenant_id = getattr(request.state, "tenant_id", "unknown")
    project = await get_project_or_404(project_id, session, tenant_id)

    if not project.s3_key:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="File not available")

    url = S3StorageService.generate_presigned_url(
        project_id=project_id,
        filename=project.original_filename,
        expiration=3600,
        http_method='GET'
    )

    return {
        "url": url,
        "filename": project.original_filename,
        "file_size": project.file_size,
        "expires_in": 3600
    }


@router.post("/{project_id}/reprocess", response_model=SuccessResponse)
async def reprocess_project(
    project_id: str,
    request: Request,
    background_tasks: BackgroundTasks,
    session: AsyncSession = Depends(get_async_session)
):
    """
    Reprocess a project by putting it back in the queue
    """
    tenant_id = getattr(request.state, "tenant_id", "unknown")
    api_key_hash = getattr(request.state, "api_key_hash", None)
    request_id = getattr(request.state, "request_id", "unknown")

    project = await get_project_or_404(project_id, session, tenant_id)

    try:
        # Reset project status to queued
        project.status = ProjectStatus.QUEUED
        project.progress_info = None
        project.error_info = None
        project.processed_at = None

        # Clear processing results but keep original file info
        project.element_count = None
        project.element_types = None
        project.has_geometry = None
        project.has_properties = None
        project.has_materials = None
        project.has_spaces = None
        project.ifc_schema = None
        project.ifc_version = None
        project.project_info = None
        project.site_info = None
        project.bounding_box = None
        project.gross_floor_area = None
        project.building_height = None

        # Store values we need before any session operations
        s3_bucket = project.s3_bucket
        s3_key = project.s3_key
        original_filename = project.original_filename
        file_size = project.file_size
        file_hash = project.file_hash
        updated_at = project.updated_at

        # Commit the database changes first
        await session.commit()

        # Create task data for Celery
        task_data = {
            "project_id": project_id,
            "tenant_id": tenant_id,
            "s3_bucket": s3_bucket,
            "s3_key": s3_key,
            "filename": original_filename,
            "file_size": file_size,
            "file_hash": file_hash,
            "request_id": f"{request_id}_reprocess"
        }

        # Schedule Celery task in background to avoid async conflicts
        def start_celery_task():
            from app.tasks.ifc_processing import process_ifc_file
            try:
                task_result = process_ifc_file.delay(task_data)
                logger.info(f"Started IFC processing task {task_result.id} for project {project_id}")
            except Exception as e:
                logger.error(f"Failed to start IFC processing task: {e}")

        background_tasks.add_task(start_celery_task)
        logger.info(f"Project {project_id} requeued for processing by tenant {tenant_id}")

        return SuccessResponse(
            message=f"Project {project_id} has been requeued for processing",
            data={
                "project_id": project_id,
                "status": "queued",
                "estimated_processing_time": 300
            }
        )

    except Exception as e:
        await session.rollback()
        logger.error(f"Failed to reprocess project {project_id}: {e}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Failed to reprocess project"
        )


@router.delete("/{project_id}", response_model=SuccessResponse)
async def delete_project(
    project_id: str,
    request: Request,
    background_tasks: BackgroundTasks,
    session: AsyncSession = Depends(get_async_session)
):
    """
    Delete a project and all associated data
    """
    tenant_id = getattr(request.state, "tenant_id", "unknown")
    project = await get_project_or_404(project_id, session, tenant_id)

    try:
        # Delete files from S3
        if project.s3_key:
            background_tasks.add_task(
                S3StorageService.delete_file,
                project_id,
                project.original_filename
            )

        # Delete project from database (cascade will handle related records)
        await session.delete(project)
        await session.commit()

        logger.info(f"Project {project_id} deleted for tenant {tenant_id}")

        return SuccessResponse(
            message=f"Project {project_id} deleted successfully",
            data={"project_id": project_id}
        )

    except Exception as e:
        await session.rollback()
        logger.error(f"Failed to delete project {project_id}: {e}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail="Failed to delete project"
        )