import { NextRequest, NextResponse } from 'next/server';

const BACKEND_URL = process.env.POS_BACKEND_URL ?? 'http://127.0.0.1:3000';

function apiPath(request: NextRequest): string {
  return request.nextUrl.pathname.replace(/^\/api\//, '');
}

function backendUrl(request: NextRequest): string {
  return `${BACKEND_URL}/${apiPath(request)}${request.nextUrl.search}`;
}

function requestHeaders(request: NextRequest, hasBody = false): Headers {
  const headers = new Headers();
  const cookie = request.headers.get('cookie');
  const terminalId = request.headers.get('x-terminal-id') ?? request.cookies.get('pos_terminal_id')?.value;

  if (cookie) headers.set('cookie', cookie);
  if (terminalId) headers.set('x-terminal-id', terminalId);
  if (hasBody) headers.set('content-type', request.headers.get('content-type') ?? 'application/json');

  return headers;
}

function proxyResponse(backendResponse: Response): NextResponse {
  const response = new NextResponse(backendResponse.body, {
    status: backendResponse.status,
    statusText: backendResponse.statusText,
  });

  const contentType = backendResponse.headers.get('content-type');
  const setCookie = backendResponse.headers.get('set-cookie');
  if (contentType) response.headers.set('content-type', contentType);
  if (setCookie) response.headers.set('set-cookie', setCookie);

  return response;
}

export async function GET(request: NextRequest) {
  try {
    const response = await fetch(backendUrl(request), {
      headers: requestHeaders(request),
      cache: 'no-store',
    });
    return proxyResponse(response);
  } catch {
    return NextResponse.json({ message: 'Backend no disponible' }, { status: 502 });
  }
}

export async function POST(request: NextRequest) {
  try {
    const body = await request.text();
    const response = await fetch(backendUrl(request), {
      method: 'POST',
      headers: requestHeaders(request, true),
      body,
      cache: 'no-store',
    });
    const bindingPayload = response.ok && apiPath(request) === 'pos/terminals/bind'
      ? await response.clone().json() as { terminalId?: string }
      : null;
    const proxied = proxyResponse(response);

    if (bindingPayload?.terminalId) {
      proxied.cookies.set('pos_terminal_id', bindingPayload.terminalId, {
        httpOnly: true,
        sameSite: 'lax',
        secure: process.env.NODE_ENV === 'production',
        maxAge: 365 * 24 * 60 * 60,
        path: '/',
      });
    }

    return proxied;
  } catch {
    return NextResponse.json({ message: 'Backend no disponible' }, { status: 502 });
  }
}
