import type { DoneActorEvent, ErrorActorEvent } from "xstate";

import type { PrivateAlbumUnlockResponse } from "@/data/dto/private-room";

import {
  getPendingAlbum,
  patchAlbumInList,
  removeAlbum,
} from "../helper/albums";
import {
  toPaywallRequest,
  toPrivateRoomErrorMessage,
  toUnlockErrorMessage,
} from "../helper/unlock";
import {
  albumMachineSetup,
  failedState,
  idleState,
  loadingState,
} from "./album-flow";
import { createPrivateRoomActorActionSetup } from "./setup";

const requestUnlockAction = albumMachineSetup.assign(({ event }) =>
  event.type === "PrivateRoomUnlockRequested"
    ? {
        pendingConfirmAlbumId: event.albumId,
        unlockErrorMessage: null,
        unlockPaywallRequest: null,
      }
    : {},
);

const cancelUnlockAction = albumMachineSetup.assign({
  pendingConfirmAlbumId: null,
});

const clearUnlockPaywallAction = albumMachineSetup.assign({
  unlockPaywallRequest: null,
});

const beginUnlockAction = albumMachineSetup.assign(({ context }) => ({
  unlockingAlbumId: context.pendingConfirmAlbumId,
  unlockErrorMessage: null,
  unlockPaywallRequest: null,
}));

export const privateRoomMachineSetup = albumMachineSetup.extend({
  actions: {
    requestUnlock: requestUnlockAction,
    cancelUnlock: cancelUnlockAction,
    clearUnlockPaywall: clearUnlockPaywallAction,
    beginUnlock: beginUnlockAction,
  },
});

const unlockDoneSetup =
  createPrivateRoomActorActionSetup<
    DoneActorEvent<PrivateAlbumUnlockResponse>
  >();
const unlockErrorSetup =
  createPrivateRoomActorActionSetup<ErrorActorEvent>();

const applyUnlockSuccessAction = unlockDoneSetup.assign(
  ({ context, event }) => ({
    items: patchAlbumInList(context.items, event.output),
    creditBalance: event.output.creditBalance,
    pendingConfirmAlbumId: null,
    unlockingAlbumId: null,
    unlockErrorMessage: null,
    unlockSuccessNonce: context.unlockSuccessNonce + 1,
  }),
);

const applyUnlockPaywallAction = unlockDoneSetup.assign(
  ({ context, event }) => ({
    items: patchAlbumInList(context.items, event.output),
    creditBalance: event.output.creditBalance,
    pendingConfirmAlbumId: null,
    unlockingAlbumId: null,
    unlockErrorMessage: null,
    unlockPaywallRequest: toPaywallRequest(event.output),
  }),
);

const applyCostChangedAction = unlockDoneSetup.assign(({ event }) => ({
  pendingConfirmAlbumId: null,
  unlockingAlbumId: null,
  unlockErrorMessage: toUnlockErrorMessage(event.output),
}));

const applyAlbumNotFoundAction = unlockDoneSetup.assign(
  ({ context, event }) => ({
    items: removeAlbum(context.items, event.output.albumId),
    creditBalance: event.output.creditBalance,
    pendingConfirmAlbumId: null,
    unlockingAlbumId: null,
    unlockErrorMessage: toUnlockErrorMessage(event.output),
  }),
);

const applyUnlockFailureAction = unlockDoneSetup.assign(
  ({ context, event }) => ({
    items: patchAlbumInList(context.items, event.output),
    creditBalance: event.output.creditBalance,
    pendingConfirmAlbumId: null,
    unlockingAlbumId: null,
    unlockErrorMessage: toUnlockErrorMessage(event.output),
  }),
);

const applyUnlockActorErrorAction = unlockErrorSetup.assign(({ event }) => ({
  pendingConfirmAlbumId: null,
  unlockingAlbumId: null,
  unlockErrorMessage: toPrivateRoomErrorMessage(event.error),
}));

const readyState = privateRoomMachineSetup.createStateConfig({
  on: {
    PrivateRoomInit: "loading",
    PrivateRoomRefresh: "loading",
    PrivateRoomUnlockRequested: { actions: "requestUnlock" },
    PrivateRoomUnlockCancelled: { actions: "cancelUnlock" },
    PrivateRoomUnlockConfirmed: [
      {
        guard: "hasPendingAlbum",
        target: "unlocking",
      },
    ],
    PrivateRoomUnlockPaywallConsumed: { actions: "clearUnlockPaywall" },
  },
});

const unlockingState = privateRoomMachineSetup.createStateConfig({
  entry: "beginUnlock",
  invoke: {
    src: "unlockAlbum",
    input: ({ context }) => {
      const pendingAlbum = getPendingAlbum(context);
      return {
        albumId: pendingAlbum?.albumId ?? "",
        expectedCost: pendingAlbum?.unlockCost ?? 0,
      };
    },
    onDone: [
      {
        guard: ({ event }) => event.output.unlocked && !event.output.locked,
        target: "ready",
        actions: applyUnlockSuccessAction,
      },
      {
        guard: ({ event }) =>
          event.output.reason === "insufficient_credits",
        target: "ready",
        actions: applyUnlockPaywallAction,
      },
      {
        guard: ({ event }) => event.output.reason === "cost_changed",
        target: "loading",
        actions: applyCostChangedAction,
      },
      {
        guard: ({ event }) => event.output.reason === "not_found",
        target: "ready",
        actions: applyAlbumNotFoundAction,
      },
      {
        target: "ready",
        actions: applyUnlockFailureAction,
      },
    ],
    onError: {
      target: "ready",
      actions: applyUnlockActorErrorAction,
    },
  },
});

export const privateRoomRootStateConfig =
  privateRoomMachineSetup.createStateConfig({
    initial: "idle",
    states: {
      idle: idleState,
      loading: loadingState,
      failed: failedState,
      ready: readyState,
      unlocking: unlockingState,
    },
  });
