import { graphql } from "@/gql";
import { VenueFragment, VenueRentalIndexFragment } from "@/gql/graphql";
import { getClient } from "@/app/client";
import { VenueList } from "@/components/venues/VenueList";
import { PageHeader } from "@/components/general/PageHeader";
import { BgPig } from "@/components/general/BgPig";
import { PageContent } from "@/components/general/PageContent";

const VenueRentalIndexDefinition = graphql(`
  fragment VenueRentalIndex on VenueRentalIndex {
    ... on VenueRentalIndex {
      title
      lead
      body {
        ...Blocks
      }
    }
  }
`);

export default async function Page() {
  const venueRentalIndexQuery = graphql(`
    query venueRentalIndex {
      index: venueRentalIndex {
        ... on VenueRentalIndex {
          ...VenueRentalIndex
        }
      }
      venues: pages(contentType: "venues.VenuePage") {
        ... on VenuePage {
          ...Venue
        }
      }
    }
  `);
  const { data, error } = await getClient().query(venueRentalIndexQuery, {});
  const index = (data?.index ?? []) as VenueRentalIndexFragment;
  const venues = (data?.venues ?? []) as VenueFragment[];
  const bookableVenues = venues.filter((venue) => venue.showAsBookable);

  return (
    <>
      <main className="site-main" id="main">
        <PageHeader heading={index.title} lead={index.lead} />
        {index.body && <PageContent blocks={index.body} />}
        <VenueList venues={bookableVenues} heading="Våre lokaler" />
      </main>
      <BgPig type="key" />
    </>
  );
}
