<template>
  <div v-if="visible" class="dialog-overlay">
    <div
      class="dialog"
      data-testid="gifted-dialog"
      :data-recipient-entity-type="currentRecipientEntityType"
    >
      <!-- Step 1: Entity Selection -->
      <EntitySelectionStep
        v-show="firstStep"
        :step-type="stepType"
        :giver-entity-type="currentGiverEntityType"
        :recipient-entity-type="currentRecipientEntityType"
        :show-projects="
          currentGiverEntityType === 'project' ||
          currentRecipientEntityType === 'project'
        "
        :all-contacts="allContacts"
        :active-did="activeDid"
        :all-my-dids="allMyDids"
        :conflict-checker="wouldCreateConflict"
        :from-project-id="fromProjectId"
        :to-project-id="toProjectId"
        :giver="giver"
        :receiver="receiver"
        :description="description"
        :amount-input="amountInput"
        :unit-code="unitCode"
        :offer-id="offerId"
        :notify="$notify"
        @entity-selected="handleEntitySelected"
        @cancel="cancel"
      />

      <!-- Step 2: Gift Details -->
      <GiftDetailsStep
        v-show="!firstStep"
        :giver="giver"
        :receiver="receiver"
        :giver-entity-type="currentGiverEntityType"
        :recipient-entity-type="currentRecipientEntityType"
        :description="description"
        :amount="parseFloat(amountInput) || 0"
        :unit-code="unitCode"
        :prompt="prompt"
        :has-conflict="hasPersonConflict"
        :offer-id="offerId"
        :from-project-id="fromProjectId"
        :to-project-id="toProjectId"
        :on-update-description="(desc: string) => (description = desc)"
        :on-update-amount="handleAmountUpdate"
        :on-update-unit-code="(code: string) => (unitCode = code)"
        @edit-entity="handleEditEntity"
        @explain-data="explainData"
        @submit="handleSubmit"
        @cancel="cancel"
      />
    </div>
  </div>
</template>

<script lang="ts">
import { Vue, Component, Prop } from "vue-facing-decorator";

import {
  createAndSubmitGive,
  didInfo,
  serverMessageForUser,
} from "../libs/endorserServer";
import * as libsUtil from "../libs/util";
import { Contact } from "../db/tables/contacts";
import { retrieveAccountDids } from "../libs/util";
import { logger } from "../utils/logger";
import EntityIcon from "../components/EntityIcon.vue";
import ProjectIcon from "../components/ProjectIcon.vue";
import EntitySelectionStep from "../components/EntitySelectionStep.vue";
import GiftDetailsStep from "../components/GiftDetailsStep.vue";
import { PlanData } from "../interfaces/records";
import { PlatformServiceMixin } from "@/utils/PlatformServiceMixin";
import { createNotifyHelpers, TIMEOUTS, NotifyFunction } from "@/utils/notify";
import { showSeedPhraseReminder } from "@/utils/seedPhraseReminder";
import {
  NOTIFY_GIFT_ERROR_NEGATIVE_AMOUNT,
  NOTIFY_GIFT_ERROR_NO_DESCRIPTION,
  NOTIFY_GIFTED_DETAILS_NO_IDENTIFIER,
  NOTIFY_GIFTED_DETAILS_RECORDING_GIVE,
} from "@/constants/notifications";
import { UNNAMED_ENTITY_NAME } from "@/constants/entities";

@Component({
  components: {
    EntityIcon,
    ProjectIcon,
    EntitySelectionStep,
    GiftDetailsStep,
  },
  mixins: [PlatformServiceMixin],
})
export default class GiftedDialog extends Vue {
  $notify!: NotifyFunction;
  notify!: ReturnType<typeof createNotifyHelpers>;

  /**
   * Safe notification method that ensures notify helpers are available
   */
  get safeNotify() {
    if (!this.notify && this.$notify) {
      this.notify = createNotifyHelpers(this.$notify);
    }
    return this.notify;
  }

  @Prop() fromProjectId = "";
  @Prop() toProjectId = "";
  @Prop({ default: "person" }) initialGiverEntityType = "person" as
    | "person"
    | "project";
  @Prop({ default: "person" }) initialRecipientEntityType = "person" as
    | "person"
    | "project";

  activeDid = "";
  allContacts: Array<Contact> = [];
  allMyDids: Array<string> = [];
  apiServer = "";

  amountInput = "0";
  callbackOnSuccess?: (amount: number) => void = () => {};
  description = "";
  firstStep = true; // true = Step 1 (giver/recipient selection), false = Step 2 (amount/description)
  giver?: libsUtil.GiverReceiverInputInfo; // undefined means no identified giver agent
  currentGiverEntityType: "person" | "project" = "person"; // Mutable version (can be toggled)
  currentRecipientEntityType: "person" | "project" = "person"; // Mutable version (can be toggled)
  offerId = "";
  prompt = "";
  receiver?: libsUtil.GiverReceiverInputInfo;
  stepType = "giver";
  unitCode = "HUR";
  visible = false;

  libsUtil = libsUtil;

  didInfo = didInfo;

  // Computed property to check if current selection would create a conflict
  get hasPersonConflict() {
    // Only check for conflicts when both entities are persons
    if (
      this.currentGiverEntityType !== "person" ||
      this.currentRecipientEntityType !== "person"
    ) {
      return false;
    }

    // Check if giver and recipient are the same person
    if (
      this.giver?.did &&
      this.receiver?.did &&
      this.giver.did === this.receiver.did
    ) {
      return true;
    }

    return false;
  }

  // Computed property to check if current selection would create a project conflict
  get hasProjectConflict() {
    // Only check for conflicts when both entities are projects
    if (
      this.currentGiverEntityType !== "project" ||
      this.currentRecipientEntityType !== "project"
    ) {
      return false;
    }

    // Check if giver and recipient are the same project
    if (
      this.giver?.handleId &&
      this.receiver?.handleId &&
      this.giver.handleId === this.receiver.handleId
    ) {
      return true;
    }

    return false;
  }

  async open(
    giver?: libsUtil.GiverReceiverInputInfo,
    receiver?: libsUtil.GiverReceiverInputInfo,
    offerId?: string,
    prompt?: string,
    description?: string,
    amountInput?: string,
    unitCode?: string,
    callbackOnSuccess: (amount: number) => void = () => {},
  ) {
    this.giver = giver;
    this.receiver = receiver;
    this.offerId = offerId || "";
    this.prompt = prompt || "";
    this.description = description || "";
    this.amountInput = amountInput || "0";
    this.unitCode = unitCode || "HUR";
    this.callbackOnSuccess = callbackOnSuccess;
    // Initialize current entity types from initial prop values
    this.currentGiverEntityType = this.initialGiverEntityType;
    this.currentRecipientEntityType = this.initialRecipientEntityType;

    try {
      const settings = await this.$accountSettings();
      this.apiServer = settings.apiServer || "";

      // Get activeDid from active_identity table (single source of truth)
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      const activeIdentity = await (this as any).$getActiveIdentity();
      this.activeDid = activeIdentity.activeDid || "";

      // Skip Step 1 if both giver and receiver are provided
      const hasGiver = giver && (!!giver.did || !!giver.handleId);
      const hasReceiver = receiver && (!!receiver.did || !!receiver.handleId);
      this.firstStep = !hasGiver || !hasReceiver;
      if (this.firstStep) {
        this.stepType = giver ? "receiver" : "giver";
      }

      logger.debug("[GiftedDialog] Settings received:", {
        activeDid: this.activeDid,
        apiServer: this.apiServer,
      });

      this.allContacts = await this.$contactsByDateAdded();

      this.allMyDids = await retrieveAccountDids();
    } catch (err: unknown) {
      logger.error("Error retrieving settings from database:", err);
      this.safeNotify.error(
        err instanceof Error
          ? err.message
          : "There was an error retrieving your settings.",
        TIMEOUTS.MODAL,
      );
    }

    this.visible = true;
  }

  close() {
    // close the dialog but don't change values (since it might be submitting info)
    this.visible = false;
  }

  changeUnitCode() {
    const units = Object.keys(this.libsUtil.UNIT_SHORT);
    const index = units.indexOf(this.unitCode);
    this.unitCode = units[(index + 1) % units.length];
  }

  increment() {
    this.amountInput = `${(parseFloat(this.amountInput) || 0) + 1}`;
  }

  decrement() {
    this.amountInput = `${Math.max(
      0,
      (parseFloat(this.amountInput) || 1) - 1,
    )}`;
  }

  cancel() {
    this.close();
    this.eraseValues();
  }

  // Computed property to check if a contact or project would create a conflict when selected
  wouldCreateConflict(identifier: string) {
    // Check for person conflicts when both entities are persons
    if (
      this.currentGiverEntityType === "person" &&
      this.currentRecipientEntityType === "person"
    ) {
      if (this.stepType === "giver") {
        // If selecting as giver, check if it conflicts with current recipient
        return this.receiver?.did === identifier;
      } else if (this.stepType === "recipient") {
        // If selecting as recipient, check if it conflicts with current giver
        return this.giver?.did === identifier;
      }
    }

    // Check for project conflicts when both entities are projects
    if (
      this.currentGiverEntityType === "project" &&
      this.currentRecipientEntityType === "project"
    ) {
      if (this.stepType === "giver") {
        // If selecting as giver, check if it conflicts with current recipient
        return this.receiver?.handleId === identifier;
      } else if (this.stepType === "recipient") {
        // If selecting as recipient, check if it conflicts with current giver
        return this.giver?.handleId === identifier;
      }
    }

    return false;
  }

  eraseValues() {
    this.description = "";
    this.giver = undefined;
    this.receiver = undefined;
    this.amountInput = "0";
    this.prompt = "";
    this.unitCode = "HUR";
    this.firstStep = true;
    // Reset to initial prop values
    this.currentGiverEntityType = this.initialGiverEntityType;
    this.currentRecipientEntityType = this.initialRecipientEntityType;
  }

  async confirm() {
    if (!this.activeDid) {
      this.safeNotify.error(
        NOTIFY_GIFTED_DETAILS_NO_IDENTIFIER.message,
        TIMEOUTS.SHORT,
      );
      return;
    }
    if (parseFloat(this.amountInput) < 0) {
      this.safeNotify.error(
        NOTIFY_GIFT_ERROR_NEGATIVE_AMOUNT.message,
        TIMEOUTS.SHORT,
      );
      return;
    }
    if (!this.description && !parseFloat(this.amountInput)) {
      this.safeNotify.error(
        NOTIFY_GIFT_ERROR_NO_DESCRIPTION.message.replace(
          "{unit}",
          this.libsUtil.UNIT_SHORT[this.unitCode] || this.unitCode,
        ),
        TIMEOUTS.SHORT,
      );
      return;
    }

    // Check for person conflict
    if (this.hasPersonConflict) {
      this.safeNotify.error(
        "You cannot select the same person as both giver and recipient.",
        TIMEOUTS.STANDARD,
      );
      return;
    }

    // Check for project conflict
    if (this.hasProjectConflict) {
      this.safeNotify.error(
        "You cannot select the same project as both giver and recipient.",
        TIMEOUTS.STANDARD,
      );
      return;
    }

    this.close();
    this.safeNotify.toast(
      NOTIFY_GIFTED_DETAILS_RECORDING_GIVE.message,
      undefined,
      TIMEOUTS.BRIEF,
    );
    // this is asynchronous, but we don't need to wait for it to complete
    await this.recordGive(
      (this.giver?.did as string) || null,
      (this.receiver?.did as string) || null,
      this.description,
      parseFloat(this.amountInput),
      this.unitCode,
    ).then(() => {
      this.eraseValues();
    });
  }

  /**
   *
   * @param giverDid may be null
   * @param recipientDid may be null
   * @param description may be an empty string
   * @param amount may be 0
   * @param unitCode may be omitted, defaults to "HUR"
   */
  async recordGive(
    giverDid: string | null,
    recipientDid: string | null,
    description: string,
    amount: number,
    unitCode: string = "HUR",
  ) {
    try {
      // Determine the correct parameters based on entity types
      let fromDid: string | undefined;
      let toDid: string | undefined;
      let fulfillsProjectHandleId: string | undefined;
      let providerPlanHandleId: string | undefined;

      if (
        this.currentGiverEntityType === "project" &&
        this.currentRecipientEntityType === "person"
      ) {
        // Project-to-person gift
        fromDid = undefined;
        toDid = recipientDid as string;
        fulfillsProjectHandleId = undefined;
        providerPlanHandleId = this.giver?.handleId;
      } else if (
        this.currentGiverEntityType === "person" &&
        this.currentRecipientEntityType === "project"
      ) {
        // Person-to-project gift
        fromDid = giverDid as string;
        toDid = undefined;
        fulfillsProjectHandleId = this.receiver?.handleId;
        providerPlanHandleId = undefined;
      } else if (
        this.currentGiverEntityType === "person" &&
        this.currentRecipientEntityType === "person"
      ) {
        // Person-to-person gift
        fromDid = giverDid as string;
        toDid = recipientDid as string;
        fulfillsProjectHandleId = undefined;
        providerPlanHandleId = undefined;
      } else {
        // Project-to-project gift
        fromDid = undefined;
        toDid = undefined;
        fulfillsProjectHandleId = this.receiver?.handleId;
        providerPlanHandleId = this.giver?.handleId;
      }

      const result = await createAndSubmitGive(
        this.axios,
        this.apiServer,
        this.activeDid,
        fromDid,
        toDid,
        description,
        amount,
        unitCode,
        fulfillsProjectHandleId,
        this.offerId,
        false,
        undefined,
        providerPlanHandleId,
      );

      if (!result.success) {
        const errorMessage = this.getGiveCreationErrorMessage(result);
        logger.error("Error with give creation result:", result);
        this.safeNotify.error(
          errorMessage || "There was an error creating the give.",
          TIMEOUTS.MODAL,
        );
      } else {
        this.safeNotify.success("That gift was recorded.", TIMEOUTS.VERY_LONG);

        // Show seed phrase backup reminder if needed
        try {
          const settings = await this.$accountSettings();
          showSeedPhraseReminder(!!settings.hasBackedUpSeed, this.$notify);
        } catch (error) {
          logger.error("Error checking seed backup status:", error);
        }

        if (this.callbackOnSuccess) {
          this.callbackOnSuccess(amount);
        }
      }
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
    } catch (error: any) {
      logger.error("Error with give recordation caught:", error);
      const errorMessage =
        error.userMessage ||
        serverMessageForUser(error) ||
        "There was an error recording the give.";
      this.safeNotify.error(errorMessage, TIMEOUTS.MODAL);
    }
  }

  // Helper functions for readability

  /**
   * @param result direct response eg. ErrorResult or SuccessResult (potentially with embedded "data")
   * @returns best guess at an error message
   */
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  getGiveCreationErrorMessage(result: any) {
    return (
      result.error?.userMessage ||
      result.error?.error ||
      result.response?.data?.error?.message
    );
  }

  explainData() {
    this.safeNotify.info(libsUtil.PRIVACY_MESSAGE, TIMEOUTS.MODAL);
  }

  goBackToStep1(step: string) {
    this.stepType = step;
    this.firstStep = true;
  }

  moveToStep2() {
    this.firstStep = false;
  }

  selectGiverPerson(contact?: Contact) {
    if (contact) {
      this.giver = {
        did: contact.did,
        name: contact.name,
      };
    } else {
      // Only set to "Unnamed" if no giver is currently set
      if (!this.giver || !this.giver.did) {
        this.giver = {
          did: "",
          name: UNNAMED_ENTITY_NAME,
        };
      }
    }
    this.firstStep = false;
  }

  selectGiverProject(project: PlanData) {
    this.giver = {
      name: project.name,
      image: project.image,
      handleId: project.handleId,
    };
    this.firstStep = false;
  }

  selectRecipientPerson(contact?: Contact) {
    if (contact) {
      this.receiver = {
        did: contact.did,
        name: contact.name,
      };
    } else {
      // Only set to "Unnamed" if no receiver is currently set
      if (!this.receiver || !this.receiver.did) {
        this.receiver = {
          did: "",
          name: UNNAMED_ENTITY_NAME,
        };
      }
    }
    this.firstStep = false;
  }

  selectRecipientProject(project: PlanData) {
    this.receiver = {
      name: project.name,
      image: project.image,
      handleId: project.handleId,
    };
    this.firstStep = false;
  }

  // New event handlers for component integration

  /**
   * Handle entity selection from EntitySelectionStep
   * @param entity - The selected entity (person or project) with stepType
   */
  handleEntitySelected(entity: {
    type: "person" | "project";
    data: Contact | PlanData;
    stepType: string;
  }) {
    if (entity.type === "person") {
      const contact = entity.data as Contact;
      // Apply DID-based logic for person entities
      const processedContact = this.processPersonEntity(contact);
      if (entity.stepType === "giver") {
        this.selectGiverPerson(processedContact);
      } else {
        this.selectRecipientPerson(processedContact);
      }
    } else if (entity.type === "project") {
      const project = entity.data as PlanData;
      if (entity.stepType === "giver") {
        this.selectGiverProject(project);
      } else {
        this.selectRecipientProject(project);
      }
    }
  }

  /**
   * Processes person entities using DID-based logic for "You" and "Unnamed"
   */
  private processPersonEntity(contact: Contact): Contact {
    if (contact.did === this.activeDid) {
      // If DID matches active DID, create "You" entity
      return { ...contact, name: "You" };
    } else if (!contact.did || contact.did === "") {
      // If DID is empty/null, create "Unnamed" entity
      return { ...contact, name: UNNAMED_ENTITY_NAME };
    } else {
      // Return the contact as-is
      return contact;
    }
  }

  /**
   * Handle edit entity request from GiftDetailsStep
   * @param data - Object containing entityType and currentEntity
   */
  handleEditEntity(data: {
    entityType: string;
    currentEntity: { did: string; name: string };
  }) {
    // Always allow editing - go back to Step 1 to select a new entity
    this.goBackToStep1(data.entityType);
  }

  /**
   * Handle form submission from GiftDetailsStep
   */
  handleSubmit() {
    this.confirm();
  }

  /**
   * Handle amount update from GiftDetailsStep
   */
  handleAmountUpdate(newAmount: number) {
    logger.debug("[GiftedDialog] handleAmountUpdate() called", {
      oldAmount: this.amountInput,
      newAmount,
    });
    this.amountInput = newAmount.toString();
    logger.debug("[GiftedDialog] handleAmountUpdate() - amountInput updated", {
      amountInput: this.amountInput,
    });
  }

  created() {
    // Initialize notify helpers when component is created
    if (this.$notify) {
      this.notify = createNotifyHelpers(this.$notify);
    }
  }

  mounted() {
    // Ensure notify helpers are initialized if not already done
    if (!this.notify && this.$notify) {
      this.notify = createNotifyHelpers(this.$notify);
    }
  }
}
</script>
