<template>
  <div class="absolute right-5 top-[max(0.75rem,env(safe-area-inset-top))]">
    <span class="align-center text-red-500 mr-2">{{ message }}</span>
    <span class="ml-2">
      <router-link
        :to="{ name: 'help' }"
        class="text-xs uppercase bg-gradient-to-b from-blue-400 to-blue-700 shadow-[inset_0_-1px_0_0_rgba(0,0,0,0.5)] text-white px-1.5 py-1 rounded-md ml-1"
      >
        Help
      </router-link>
    </span>
  </div>
</template>

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

import { AppString, NotificationIface } from "../constants/app";
import { MASTER_SETTINGS_KEY } from "../db/tables/settings";
import { DEFAULT_ENDORSER_API_SERVER } from "../constants/app";
import { PlatformServiceMixin } from "../utils/PlatformServiceMixin";

@Component({
  mixins: [PlatformServiceMixin],
})
export default class TopMessage extends Vue {
  // Enhanced PlatformServiceMixin provides:
  // - this.$dbQuery(), this.$dbExec(), this.$dbGetOneRow() with built-in error handling
  // - this.$getSettings(), this.$getMergedSettings() utility methods
  // - this.$withTransaction() for safe database transactions
  // - this.platformService, this.isCapacitor, this.isWeb, this.isElectron
  // - this.capabilities computed property
  // All methods use $ prefix following Vue conventions

  $notify!: (notification: NotificationIface, timeout?: number) => void;

  @Prop selected = "";

  message = "";

  async mounted() {
    try {
      const settings = await this.getActiveAccountSettings();
      if (
        settings.warnIfTestServer &&
        settings.apiServer !== AppString.PROD_ENDORSER_API_SERVER
      ) {
        const didPrefix = settings.activeDid?.slice(11, 15);
        this.message = "You're not using prod, user " + didPrefix;
      } else if (
        settings.warnIfProdServer &&
        settings.apiServer === AppString.PROD_ENDORSER_API_SERVER
      ) {
        const didPrefix = settings.activeDid?.slice(11, 15);
        this.message = "You are using prod, user " + didPrefix;
      }
    } catch (err: unknown) {
      this.$notify(
        {
          group: "alert",
          type: "danger",
          title: "Error Detecting Server",
          text: JSON.stringify(err),
        },
        -1,
      );
    }
  }

  /**
   * Get settings for the active account using enhanced mixin utilities.
   * Dramatically simplified using $getMergedSettings utility method.
   */
  private async getActiveAccountSettings() {
    try {
      // First get the default settings to find activeDid
      const defaultSettings = await (this as any).$getSettings(
        MASTER_SETTINGS_KEY,
        {
          id: MASTER_SETTINGS_KEY,
          activeDid: undefined,
          apiServer: DEFAULT_ENDORSER_API_SERVER,
        },
      );

      // Use enhanced utility to merge default and account-specific settings
      // This replaces 50+ lines of duplicated logic with a single method call!
      const mergedSettings = await (this as any).$getMergedSettings(
        MASTER_SETTINGS_KEY,
        defaultSettings.activeDid,
        {
          id: MASTER_SETTINGS_KEY,
          activeDid: undefined,
          apiServer: DEFAULT_ENDORSER_API_SERVER,
        },
      );

      return mergedSettings;
    } catch (error) {
      // Enhanced mixin already provides detailed error logging
      // Just provide fallback for UI stability
      return {
        id: MASTER_SETTINGS_KEY,
        activeDid: undefined,
        apiServer: DEFAULT_ENDORSER_API_SERVER,
      };
    }
  }
}
</script>
