import { Controller, Get, HttpException, HttpStatus } from "@nestjs/common";
import { sql } from "drizzle-orm";
import { db } from "../../config/database.js";

const SERVICE_NAME = "content";

@Controller()
export class HealthController {
  @Get("healthz")
  liveness(): { status: string; service: string; timestamp: string } {
    return {
      status: "ok",
      service: SERVICE_NAME,
      timestamp: new Date().toISOString(),
    };
  }

  @Get("readyz")
  async readiness(): Promise<{
    status: string;
    service: string;
    timestamp: string;
  }> {
    try {
      await db.execute(sql`SELECT 1`);
      return {
        status: "ok",
        service: SERVICE_NAME,
        timestamp: new Date().toISOString(),
      };
    } catch (error) {
      throw new HttpException(
        {
          status: "error",
          service: SERVICE_NAME,
          timestamp: new Date().toISOString(),
          error:
            error instanceof Error ? error.message : "database unreachable",
        },
        HttpStatus.SERVICE_UNAVAILABLE,
      );
    }
  }
}
