import {
  Catch,
  ExceptionFilter,
  ArgumentsHost,
  HttpException,
  Logger,
} from "@nestjs/common";
import type { Request, Response } from "express";
import { ZodError } from "zod";
import { ApplicationError } from "./application-error.js";

/**
 * 全局错误过滤器（对齐 02-architecture-design.md §6.9）。
 *
 * 错误处理顺序：
 * 1. ApplicationError → 按 statusCode + code 返回 ActionState
 * 2. ZodError → 400 + BFF_PARENT_VALIDATION_ERROR
 * 3. HttpException → 透传 NestJS 异常
 * 4. 未知 Error → 500 + BFF_PARENT_INTERNAL_ERROR + traceId
 *
 * traceId 来源：x-request-id 头或新生成。
 */
@Catch()
export class GlobalErrorFilter implements ExceptionFilter {
  private readonly logger = new Logger(GlobalErrorFilter.name);

  catch(exception: unknown, host: ArgumentsHost): void {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const request = ctx.getRequest<Request>();

    const traceIdHeader = request.headers["x-request-id"];
    const traceId =
      typeof traceIdHeader === "string" ? traceIdHeader : "unknown";

    let statusCode = 500;
    let body: Record<string, unknown>;

    if (exception instanceof ApplicationError) {
      exception.traceId = traceId;
      statusCode = exception.statusCode;
      body = exception.toJSON();
    } else if (exception instanceof ZodError) {
      statusCode = 400;
      body = {
        success: false,
        error: {
          code: "BFF_PARENT_VALIDATION_ERROR",
          message: "Validation failed",
          details: exception.flatten(),
          traceId,
        },
      };
    } else if (exception instanceof HttpException) {
      statusCode = exception.getStatus();
      const res = exception.getResponse();
      const message = this.extractHttpMessage(res, exception);
      body = {
        success: false,
        error: {
          code: "HTTP_ERROR",
          message,
          traceId,
        },
      };
    } else {
      this.logger.error(
        `Unhandled exception: ${exception}`,
        exception instanceof Error ? exception.stack : undefined,
      );
      body = {
        success: false,
        error: {
          code: "BFF_PARENT_INTERNAL_ERROR",
          message: "An unexpected error occurred",
          traceId,
        },
      };
    }

    response.status(statusCode).json(body);
  }

  private extractHttpMessage(
    res: string | object,
    exception: HttpException,
  ): string {
    if (typeof res === "string") {
      return res;
    }
    if (res && typeof res === "object" && "message" in res) {
      const msg = (res as { message: unknown }).message;
      return typeof msg === "string" ? msg : exception.message;
    }
    return exception.message;
  }
}
