import { Handlers, PageProps } from "$fresh/server.ts";
import { Button } from "../../components/Button.tsx";
import { Content } from "../../components/Content.tsx";
import { SERVER_STATE } from "../../state/serverState.ts";

export const handler: Handlers = {
  async POST(req, _ctx) {
    const url = new URL(req.url);
    SERVER_STATE.acceptEULA();
    return Response.redirect(url.origin + '/setup/start');
  }
};

export default async function EULA({url}: PageProps) {
  // TODO: this does not respect instances and needs to once they are supported
  const eulaText = await Deno.readTextFileSync("./server/eula.txt");

  return (
    <div class="container p-8">
      <Content>
        <h2 class="text-2xl font-pixel">Minecraft EULA</h2>
        <p>
          This page is provided as a courtesy. The text below is the contents of
          "eula.txt," a file that is generated by the server the first time it
          runs. In order to run the server, you must agree to the EULA by
          changing "false" to "true."
        </p>
        <p>
          To make this easier, there is a button below. The one and only action
          this button takes is to change the value in the text file to true,
          this is not an agreement between you and CyborgGrizzly Games.
        </p>
        <p>
          If you are unsure of the contents of the EULA, you are encouraged to
          read it by following the link in the text file.
        </p>
        <pre class="p-2 bg-smoke-500 rounded-lg">{eulaText}</pre>
        <div class="mt-8 w-full">
          <form action={url.pathname} method="post">
            <Button class="ml-auto text-right block" type="submit">
              Accept EULA
            </Button>
          </form>
        </div>
      </Content>
    </div>
  );
}
