import Fastify from 'fastify';
import mercurius from 'mercurius';

import { config } from './config.js';
import { prisma } from './prisma.js';
import { resolvers, schema } from './graphql/schema.js';
import {
  fetchTelegramPhoto,
  handleTelegramBotWebhook,
} from './auth/telegram-bot-login.js';

const app = Fastify({ logger: true });

app.register(mercurius, {
  schema,
  resolvers,
  graphiql: true,
  context: async (request) => {
    const initDataHeader = request.headers['x-telegram-init-data'];
    const loginDataHeader = request.headers['x-telegram-login-data'];
    const sessionTokenHeader = request.headers['x-mapflow-session-token'];
    return {
      telegramInitData: Array.isArray(initDataHeader) ? initDataHeader[0] : initDataHeader,
      telegramLoginData: Array.isArray(loginDataHeader) ? loginDataHeader[0] : loginDataHeader,
      mapflowSessionToken: Array.isArray(sessionTokenHeader) ? sessionTokenHeader[0] : sessionTokenHeader,
    };
  },
});

app.get('/health', async () => ({ ok: true }));

app.post('/telegram/webhook', async (request, reply) => {
  const secretHeader = request.headers['x-telegram-bot-api-secret-token'];
  const secretToken = Array.isArray(secretHeader) ? secretHeader[0] : secretHeader;
  await handleTelegramBotWebhook(request.body as never, secretToken);
  return reply.send({ ok: true });
});

app.get('/telegram/photo/:fileId', async (request, reply) => {
  const params = request.params as { fileId: string };
  const photo = await fetchTelegramPhoto(params.fileId);
  reply.header('content-type', photo.contentType);
  reply.header('cache-control', 'public, max-age=86400');
  reply.header('access-control-allow-origin', '*');
  reply.header('cross-origin-resource-policy', 'cross-origin');
  return reply.send(photo.bytes);
});

app.addHook('onClose', async () => {
  await prisma.$disconnect();
});

await app.listen({ host: config.host, port: config.port });
