import calendarStyles from "react-big-calendar/lib/css/react-big-calendar.css";
import eventStyles from "../styles/events-team.css";
import { Calendar, dayjsLocalizer } from "react-big-calendar";
import dayjs from "dayjs";
import { type LinksFunction } from "@remix-run/cloudflare";
import {
  Accordion,
  AccordionButton,
  AccordionIcon,
  AccordionItem,
  AccordionPanel,
  Box,
  Container,
} from "@chakra-ui/react";
import { useLoaderData } from "@remix-run/react";
import { useState } from "react";

export const links: LinksFunction = () => {
  return [
    { href: calendarStyles, rel: "stylesheet" },
    { href: eventStyles, rel: "stylesheet" },
  ];
};

export async function loader({ context }: { context: RequestContext }) {
  if (!context.data.current_user)
    throw new Response(null, {
      status: 401,
    });

  if (
    ![1 << 3, 1 << 4, 1 << 12].find(
      (p) => context.data.current_user.permissions & p,
    )
  )
    throw new Response(null, {
      status: 403,
    });

  const now = new Date();
  const eventsData = await context.env.D1.prepare(
    "SELECT answer, approved, created_by, day, details, id, month, pending, performed_at, reached_minimum_player_count, type, year FROM events WHERE month = ? AND year = ? ORDER BY day;",
  )
    .bind(now.getUTCMonth() + 1, now.getUTCFullYear())
    .all();

  if (eventsData.error)
    throw new Response(null, {
      status: 500,
    });

  const calendarData = eventsData.results.map((e) => {
    const dayDate = new Date(
      e.year as number,
      (e.month as number) - 1,
      e.day as number,
    );
    return {
      id: e.id,
      title: (e.type as string).toUpperCase(),
      allDay: true,
      start: dayDate,
      end: dayDate,
    };
  });

  return {
    calendarData,
    eventList: eventsData.results,
  };
}

export default function () {
  const data = useLoaderData<typeof loader>();
  const [selectedDate, setDate] = useState(new Date());

  return (
    <Container maxW="container.lg" h="600px">
      <Calendar
        events={data?.eventList}
        localizer={dayjsLocalizer(dayjs)}
        onSelectSlot={(s) => {
          setDate(s.slots.at(0) as Date);
        }}
        style={{ height: 500 }}
      />
      <Accordion id="events-accordion" mt="16px">
        <AccordionItem>
          <h2>
            <AccordionButton>
              <Box as="span" flex="1" textAlign="left">
                Fact of the Day
              </Box>
              <AccordionIcon />
            </AccordionButton>
          </h2>
          <AccordionPanel pb={4}></AccordionPanel>
        </AccordionItem>
        <AccordionItem>
          <h2>
            <AccordionButton>
              <Box as="span" flex="1" textAlign="left">
                Gamenight
              </Box>
              <AccordionIcon />
            </AccordionButton>
          </h2>
          <AccordionPanel pb={4}></AccordionPanel>
        </AccordionItem>
        <AccordionItem>
          <h2>
            <AccordionButton>
              <Box as="span" flex="1" textAlign="left">
                Riddle of the Week
              </Box>
            </AccordionButton>
          </h2>
          <AccordionPanel pb={4}></AccordionPanel>
        </AccordionItem>
        <AccordionItem>
          <h2>
            <AccordionButton>
              <Box as="span" flex="1" textAlign="left">
                Question of the Day
              </Box>
            </AccordionButton>
          </h2>
          <AccordionPanel pb={4}></AccordionPanel>
        </AccordionItem>
      </Accordion>
    </Container>
  );
}
