import { Logger, todayString } from "@/utils";

import { chatAssign, type ChatActionArgs } from "./chat-flow-actions";
import { beginPendingReply } from "./chat-machine.helpers";

const log = new Logger("StoresChatChatMediaFlow");

export const appendGuestUserImageAction = chatAssign(
  ({ context, event }: ChatActionArgs) => {
    if (event.type !== "ChatSendImage") return {};

    const today = todayString();
    log.debug("[chat-machine] appendGuestUserImage", {
      oldMessagesCount: context.messages.length,
      quotaMode: "server controlled",
    });
    return {
      messages: [
        ...context.messages,
        {
          content: "[Image]",
          isFromAI: false,
          date: today,
          imageUrl: event.imageBase64,
        },
      ],
      ...beginPendingReply(context),
      upgradePromptVisible: false,
      upgradeReason: null,
    };
  },
);

export const appendUserImageAction = chatAssign(
  ({ context, event }: ChatActionArgs) => {
    if (event.type !== "ChatSendImage") return {};

    const today = todayString();
    log.debug("[chat-machine] appendUserImage", {
      oldMessagesCount: context.messages.length,
    });

    return {
      messages: [
        ...context.messages,
        {
          content: "[Image]",
          isFromAI: false,
          date: today,
          imageUrl: event.imageBase64,
        },
      ],
      ...beginPendingReply(context),
      upgradePromptVisible: false,
      upgradeReason: null,
    };
  },
);
