path = "/home/crogers2287/cfrmanager/src/app/documents/page.tsx"
with open(path, 'r') as f:
    content = f.read()

old = '''  return (
    <AppLayout>
      <div className="flex items-center justify-between mb-6">
        <div>
          <h2 className="text-2xl font-bold text-zinc-900 dark:text-zinc-100">
            Document Center
          </h2>
          <p className="text-sm text-zinc-500 dark:text-zinc-400 mt-1">
            Upload and organize receipts, leases, and important documents
          </p>
        </div>
        <DocumentUpload onUploaded={refresh} />
      </div>

      {loading ? (
        <div className="flex items-center justify-center py-12">
          <Loader2 className="h-8 w-8 animate-spin text-zinc-400" />
        </div>
      ) : documents.length === 0 ? (
        <Card>
          <CardContent className="text-center py-12">
            <File className="h-12 w-12 text-zinc-400 mx-auto mb-4" />
            <h3 className="text-lg font-medium mb-2">No documents yet</h3>
            <p className="text-zinc-600 dark:text-zinc-400 mb-4">
              Upload receipts, leases, and other important documents.
              <br />
              We&apos;ll automatically extract information using AI.
            </p>
            <DocumentUpload onUploaded={refresh} />
          </CardContent>
        </Card>
      ) : (
        <Tabs defaultValue="gallery" className="space-y-4">
          <TabsList>
            <TabsTrigger value="gallery" className="flex items-center gap-2">
              <Grid className="h-4 w-4" />
              Gallery
            </TabsTrigger>
            <TabsTrigger value="search" className="flex items-center gap-2">
              <Sparkles className="h-4 w-4" />
              AI Search
            </TabsTrigger>
          </TabsList>

          <TabsContent value="gallery">
            <DocumentGallery
              documents={documents}
              properties={properties}
              tenants={tenants}
              onDelete={handleDelete}
            />
          </TabsContent>

          <TabsContent value="search">
            <Card>
              <CardContent className="p-6">
                <SemanticSearch
                  documents={documents}
                  onResultClick={handleSearchResultClick}
                />
              </CardContent>
            </Card>
          </TabsContent>
        </Tabs>
      )}'''

new = '''  // Group documents by property for the folder view
  const docsByProperty = properties.map((property) => ({
    property,
    docs: documents.filter((d) => d.propertyId === property.id),
  }));
  const unassignedDocs = documents.filter((d) => !d.propertyId);

  const categoryLabel: Record<string, string> = {
    lease: "Leases",
    receipt: "Receipts",
    identity: "Identity / ID",
    maintenance: "Maintenance",
    utility: "Utilities",
    insurance: "Insurance",
    tax: "Tax",
    other: "Other",
  };

  return (
    <AppLayout>
      <div className="flex items-center justify-between mb-6">
        <div>
          <h2 className="text-2xl font-bold text-zinc-900 dark:text-zinc-100">
            Document Center
          </h2>
          <p className="text-sm text-zinc-500 dark:text-zinc-400 mt-1">
            Upload and organize receipts, leases, and important documents
          </p>
        </div>
        <DocumentUpload onUploaded={refresh} />
      </div>

      {loading ? (
        <div className="flex items-center justify-center py-12">
          <Loader2 className="h-8 w-8 animate-spin text-zinc-400" />
        </div>
      ) : (
        <Tabs defaultValue="byProperty" className="space-y-4">
          <TabsList>
            <TabsTrigger value="byProperty" className="flex items-center gap-2">
              <File className="h-4 w-4" />
              By Property
            </TabsTrigger>
            <TabsTrigger value="gallery" className="flex items-center gap-2">
              <Grid className="h-4 w-4" />
              All Files
            </TabsTrigger>
            <TabsTrigger value="search" className="flex items-center gap-2">
              <Sparkles className="h-4 w-4" />
              AI Search
            </TabsTrigger>
          </TabsList>

          {/* ── By Property Folder View ── */}
          <TabsContent value="byProperty" className="space-y-6">
            {docsByProperty.map(({ property, docs }) => {
              const byCategory = Object.entries(
                docs.reduce<Record<string, typeof docs>>((acc, doc) => {
                  const cat = doc.category || "other";
                  if (!acc[cat]) acc[cat] = [];
                  acc[cat].push(doc);
                  return acc;
                }, {})
              );

              return (
                <Card key={property.id}>
                  <CardContent className="p-0">
                    <div className="flex items-center justify-between px-5 py-3 border-b bg-zinc-50 dark:bg-zinc-900 rounded-t-lg">
                      <div>
                        <h3 className="font-semibold text-zinc-900 dark:text-zinc-100">{property.address}</h3>
                        <p className="text-xs text-zinc-500">{property.city}, {property.state} — {docs.length} file{docs.length !== 1 ? "s" : ""}</p>
                      </div>
                      <DocumentUpload onUploaded={refresh} propertyId={property.id} />
                    </div>

                    {docs.length === 0 ? (
                      <p className="text-sm text-zinc-400 px-5 py-6">No documents filed yet.</p>
                    ) : (
                      <div className="divide-y">
                        {byCategory.map(([cat, catDocs]) => (
                          <div key={cat} className="px-5 py-3">
                            <p className="text-xs font-semibold text-zinc-400 uppercase tracking-wider mb-2">
                              {categoryLabel[cat] ?? cat}
                            </p>
                            <div className="space-y-1">
                              {catDocs.map((doc) => (
                                <div key={doc.id} className="flex items-center justify-between text-sm py-1 hover:bg-zinc-50 dark:hover:bg-zinc-800 px-2 rounded">
                                  <span className="text-zinc-700 dark:text-zinc-300 truncate">{doc.fileName}</span>
                                  <span className="text-xs text-zinc-400 ml-4 shrink-0">{formatFileSize(doc.fileSize)}</span>
                                </div>
                              ))}
                            </div>
                          </div>
                        ))}
                      </div>
                    )}
                  </CardContent>
                </Card>
              );
            })}

            {unassignedDocs.length > 0 && (
              <Card>
                <CardContent className="p-0">
                  <div className="px-5 py-3 border-b bg-zinc-50 dark:bg-zinc-900 rounded-t-lg">
                    <h3 className="font-semibold text-zinc-500">Unassigned</h3>
                    <p className="text-xs text-zinc-400">{unassignedDocs.length} file{unassignedDocs.length !== 1 ? "s" : ""} not linked to a property</p>
                  </div>
                  <div className="px-5 py-3 space-y-1">
                    {unassignedDocs.map((doc) => (
                      <div key={doc.id} className="flex items-center justify-between text-sm py-1">
                        <span className="text-zinc-700 dark:text-zinc-300 truncate">{doc.fileName}</span>
                        <span className="text-xs text-zinc-400 ml-4 shrink-0">{formatFileSize(doc.fileSize)}</span>
                      </div>
                    ))}
                  </div>
                </CardContent>
              </Card>
            )}
          </TabsContent>

          {/* ── All Files Gallery ── */}
          <TabsContent value="gallery">
            {documents.length === 0 ? (
              <Card>
                <CardContent className="text-center py-12">
                  <File className="h-12 w-12 text-zinc-400 mx-auto mb-4" />
                  <h3 className="text-lg font-medium mb-2">No documents yet</h3>
                  <p className="text-zinc-600 dark:text-zinc-400 mb-4">
                    Upload receipts, leases, and other important documents.
                  </p>
                  <DocumentUpload onUploaded={refresh} />
                </CardContent>
              </Card>
            ) : (
              <DocumentGallery
                documents={documents}
                properties={properties}
                tenants={tenants}
                onDelete={handleDelete}
              />
            )}
          </TabsContent>

          <TabsContent value="search">
            <Card>
              <CardContent className="p-6">
                <SemanticSearch
                  documents={documents}
                  onResultClick={handleSearchResultClick}
                />
              </CardContent>
            </Card>
          </TabsContent>
        </Tabs>
      )}'''

if old in content:
    content = content.replace(old, new)
    with open(path, 'w') as f:
        f.write(content)
    print("documents/page.tsx: Fixed")
else:
    print("Pattern not found — checking length...")
    # Try to find where the return starts
    idx = content.find("  return (\n    <AppLayout>")
    print(f"Return block found at index: {idx}")
