import { Hash } from "lucide-react";

import { ListPageCard } from "@/app/dashboard/_components";
import { ErrorBanner } from "@/components/shared/error-banner";
import {
  masterFiltersToBackend,
  parseMasterListQuery,
} from "@/lib/server/parse-master-list-search-params";

import { SecurityTickerHeaderActions } from "./_components/list-header-actions";
import { SecurityTickersTable } from "./_components/security-tickers-table";
import { parseSecurityTickerFiltersFromSearchParams } from "./_components/schema";
import { fetchBackendSecurityTickers } from "./_lib/security-ticker-server-api";

type SecurityTickerPageProps = {
  searchParams: Promise<Record<string, string | undefined>>;
};

export default async function SecurityTickerPage({ searchParams }: SecurityTickerPageProps) {
  const query = await searchParams;
  const { page, pageSize, filters } = parseMasterListQuery(
    query,
    parseSecurityTickerFiltersFromSearchParams,
  );
  const { rows, errorMessage, totalCount, pageCount } = await fetchBackendSecurityTickers({
    page,
    pageSize,
    filters: masterFiltersToBackend(filters, {
      masterName: "master_name",
      parentName: "parent_name",
    }),
  });

  return (
    <ListPageCard
      icon={Hash}
      title="Security ticker"
      breadcrumb={[{ label: "Master" }, { label: "Security ticker" }]}
      description="Manage ticker symbols linked to security names, with status and priority."
      actions={<SecurityTickerHeaderActions data={rows} />}
    >
      <ErrorBanner message={errorMessage} className="mb-4" />
      <SecurityTickersTable
        data={rows}
        initialFilters={filters}
        pagination={{ page, pageSize, totalCount, pageCount }}
      />
    </ListPageCard>
  );
}
