import { onRequest } from 'firebase-functions/v2/https';
import { logger } from 'firebase-functions/v2';
import * as cookie from 'cookie';
import { corsMiddlewareHandler } from '../config'; // or wherever it's defined

export const logout = onRequest(
  { region: 'asia-northeast3' },
  async (req, res) => {
    return corsMiddlewareHandler(req, res, async () => {
      logger.info('logout: CORS‑enabled function called');

      if (req.method !== 'POST') {
        res.status(405).send({ message: 'Method Not Allowed' });
        return;
      }

      const expiredCookie = cookie.serialize('__session', '', {
        httpOnly: true,
        secure: true,
        maxAge: 0,
        path: '/',
      });

      res.setHeader('Set-Cookie', expiredCookie);
      res.status(200).json({ success: true });
    });
  },
);
