export const dynamic = "force-dynamic"

import { Metadata } from "next"
import { ManualsDashboardClient } from "@/components/manuals-dashboard-client"
import { PublicInset, PublicPageHeader } from "@/components/public-surface"
import { getConvexManualDashboard } from "@/lib/convex-service"
import { businessConfig } from "@/lib/seo-config"
import { readFileSync } from "fs"
import { join } from "path"

export const metadata: Metadata = {
  title: "Manual Processing Dashboard | Rocky Mountain Vending",
  description:
    "Comprehensive dashboard showing processed manual data, statistics, and analytics",
  alternates: {
    canonical: `${businessConfig.website}/manuals/dashboard`,
  },
  robots: {
    index: false,
    follow: false,
  },
}

interface DashboardData {
  missingManuals: any
  qaData: any[]
  metadata: any[]
  structuredData: any[]
  semanticIndex: any
  acquisitionList: any
  nameMapping: any[]
}

async function loadDashboardData(): Promise<DashboardData> {
  const projectRoot = join(process.cwd(), "..")

  try {
    const missingManuals = JSON.parse(
      readFileSync(join(projectRoot, "missing_manuals_report.json"), "utf-8")
    )

    const qaData = JSON.parse(
      readFileSync(join(projectRoot, "manuals_qa_comprehensive.json"), "utf-8")
    )

    const metadata = JSON.parse(
      readFileSync(join(projectRoot, "manuals_enhanced_metadata.json"), "utf-8")
    )

    const structuredData = JSON.parse(
      readFileSync(join(projectRoot, "manuals_structured_data.jsonld"), "utf-8")
    )

    const semanticIndex = JSON.parse(
      readFileSync(join(projectRoot, "manuals_semantic_index.json"), "utf-8")
    )

    const acquisitionList = JSON.parse(
      readFileSync(join(projectRoot, "manual_acquisition_list.json"), "utf-8")
    )

    const nameMapping = JSON.parse(
      readFileSync(join(projectRoot, "manual_name_mapping.json"), "utf-8")
    )

    return {
      missingManuals,
      qaData,
      metadata,
      structuredData,
      semanticIndex,
      acquisitionList,
      nameMapping,
    }
  } catch (error) {
    console.error("Error loading dashboard data:", error)
    return {
      missingManuals: { summary: {} },
      qaData: [],
      metadata: [],
      structuredData: [],
      semanticIndex: {},
      acquisitionList: { total_items: 0 },
      nameMapping: [],
    }
  }
}

export default async function ManualsDashboardPage() {
  const data = (await getConvexManualDashboard()) || (await loadDashboardData())

  return (
    <div className="container mx-auto px-4 py-8 md:py-12">
      <PublicPageHeader
        eyebrow="Manual Operations"
        title="Manual Processing Dashboard"
        description="Operational overview of the manuals catalog, processing metadata, and gap analysis."
      >
        <PublicInset className="inline-flex w-fit rounded-full px-4 py-2 text-sm text-muted-foreground shadow-none">
          Admin-style data view for manual operations.
        </PublicInset>
      </PublicPageHeader>

      <div className="mt-8">
        <ManualsDashboardClient data={data} />
      </div>
    </div>
  )
}
