import { List } from "@mantine/core";
import { useLingui } from "@lingui/react/macro";
import ListLink from "@/components/list-link";
import {
  DatabaseIcon,
  TreeStructureIcon,
  TrophyIcon,
  MedalIcon,
  CrownIcon,
  ListIcon,
} from "@phosphor-icons/react";
import ListButton from "@/components/list-button";
import { migrateBadgeProgress } from "@/features/badges/server";
import { useState } from "react";

const AdminPage = () => {
  const { t } = useLingui();
  const [isMigrating, setIsMigrating] = useState(false);

  const handleMigrateBadges = async () => {
    if (isMigrating) return;

    setIsMigrating(true);
    await migrateBadgeProgress();
    setIsMigrating(false);
  };

  return (
    <List p="0">
      <ListLink
        label={t`Manage Tournaments`}
        Icon={TrophyIcon}
        to="/admin/tournaments"
      />
      <ListLink
        label={t`Award Badges`}
        Icon={CrownIcon}
        to="/admin/badges"
      />
      <ListButton
        label={t`Migrate Badge Progress`}
        Icon={MedalIcon}
        onClick={handleMigrateBadges}
        loading={isMigrating}
      />
      <ListLink
        label={t`Activities`}
        Icon={ListIcon}
        to="/admin/activities"
      />
      <ListButton
        label={t`Open Pocketbase`}
        Icon={DatabaseIcon}
        onClick={() =>
          window.location.replace(process.env.POCKETBASE_URL! + "/_/")
        }
      />
      <ListLink
        label={t`Bracket Preview`}
        Icon={TreeStructureIcon}
        to="/admin/preview"
      />
    </List>
  );
};

export default AdminPage;
