import type { Context } from "hono";
import { insertAdminLog } from "./adminLog.helper";
import {
  fetchFromPythonApi,
  proxyToPythonApi,
} from "@/lib/chatbot/python-api.client";

export const createPromptHandler = async (c: Context) => {
  const coreDB = c.get("datastore");
  const body = await c.req.json();
  const { response, payload, ok } = await proxyToPythonApi(
    c,
    "/api/v1/admin-console/chatbot/prompts",
    "POST",
    undefined,
    body,
  );

  if (ok && payload.data) {
    await insertAdminLog(coreDB, {
      entity_type: "prompt",
      entity_id: payload.data.id,
      action: "create",
      performed_by: c.get("adminEmail") ?? "admin",
      before_data: null,
      after_data: payload.data,
    });
  }

  return response;
};

export const listPromptsHandler = async (c: Context) => {
  const start = c.req.query("start") ?? "0";
  const limit = c.req.query("limit") ?? "10";
  // List views typically don't need logging per-request
  const { response } = await proxyToPythonApi(
    c,
    "/api/v1/admin-console/chatbot/prompts",
    "GET",
    { start, limit }
  );
  return response;
};

export const getPromptByIdHandler = async (c: Context) => {
  const { id } = c.req.param();
  const { response } = await proxyToPythonApi(
    c,
    `/api/v1/admin-console/chatbot/prompts/${id}`,
    "GET",
  );
  return response;
};

export const updatePromptHandler = async (c: Context) => {
  const coreDB = c.get("datastore");
  const { id } = c.req.param();
  const body = await c.req.json();
  const performedBy = c.get("adminEmail") ?? "admin";
  // 1. Fetch the "before" state for logging
  const beforeState = await fetchFromPythonApi(
    c,
    `/api/v1/admin-console/chatbot/prompts/${id}`,
  );

  // 2. Proxy the update request to the Python service
  const { response, payload, ok } = await proxyToPythonApi(
    c,
    `/api/v1/admin-console/chatbot/prompts/${id}`,
    "PUT",
    undefined,
    body,
  );
  // 3. If successful, insert the admin log
  if (ok && payload.data) {
    await insertAdminLog(coreDB, {
      entity_type: "prompt",
      entity_id: id,
      action: "update",
      performed_by: performedBy,
      before_data: beforeState.success ? beforeState.data : null,
      after_data: payload.data,
    });
  }

  // 4. Return the response to the client
  return response;
};

export const deletePromptHandler = async (c: Context) => {
  const coreDB = c.get("datastore");
  const { id } = c.req.param();
  const performedBy = c.get("adminEmail") ?? "admin";

  // 1. Fetch the "before" state for logging
  const beforeState = await fetchFromPythonApi(
    c,
    `/api/v1/admin-console/chatbot/prompts/${id}`,
  );

  const { response, ok } = await proxyToPythonApi(
    c,
    `/api/v1/admin-console/chatbot/prompts/${id}`,
    "DELETE",
  );

  if (ok && beforeState.success) {
    await insertAdminLog(coreDB, {
      entity_type: "prompt",
      entity_id: id,
      action: "delete",
      performed_by: performedBy,
      before_data: beforeState.data,
      after_data: null,
    });
  }

  return response;
};
