<script setup>
import PageTitle from "@/components/global/PageTitle.vue";
import RestrictedPageWrapper from "@/components/page/wrappers/RestrictedPageWrapper.vue";
import { SessionUser } from "@/components/session/token/SessionUser.vue";
import { useRouter } from 'vue-router'
import { ref } from 'vue';
import { ConfigurationSelectOption } from "@/components/displays/superuser/configuration/ConfigurationSelectObject.vue";
import ConfigurationSubPageWrapper
  from "@/views/dashboards/superUserDashboard/configuration/ConfigurationSubPageWrapper.vue";
import ConfigurationCategory from "@/components/displays/superuser/configuration/ConfigurationCategory.vue";
import ConfigurationSwitch from "@/components/displays/superuser/configuration/ConfigurationSwitch.vue";
import ConfigurationSelect from "@/components/displays/superuser/configuration/ConfigurationSelect.vue";
import ConfigurationSecretKey from "@/components/displays/superuser/configuration/ConfigurationSecretKey.vue";
import ConfigurationInput from "@/components/displays/superuser/configuration/ConfigurationInput.vue";

// Get the department from the route
const router = useRouter()

const enabled = ref(false);
const module_config = ref([]);

const getEnabled = async () => {
  await SessionUser.superUser.modules.entra.config.enabled.get().then((response) => {
    console.log('Entra enabled: ', response.data.data);
    enabled.value = response.data.data;
  }).catch((error) => {
    console.log(error);
  });
};

const getModuleConfig = async () => {
  await SessionUser.superUser.modules.entra.config.get_all().then((response) => {
    let tmp_module_config = response.data.data;
    let tmp_module_config_array = [];
    for (const [key, value] of Object.entries(tmp_module_config)) {
      console.log(`${value.variable}: ${value.value}`);
      tmp_module_config_array.push({
        variable: value.variable,
        value: value.value,
      });
    }
    module_config.value = tmp_module_config_array;
    console.log(module_config.value);
  }).catch((error) => {
    console.log(error);
  });
};

const getModuleConfigValue = (variable) => {
  const config = module_config.value.find((config) => config.variable === variable);
  // Log to the console, what the value was found
  if (config) {
    console.log(`Found value for ${variable}: ${config.value}`);
  } else {
    console.log(`No value found for ${variable}`);
  }
  return config ? config.value : '';
};

const fetchEntraBookingCalendarGroups = async () => {
  try {
    const response = await SessionUser.request(
        '/modules/entra/calendar-groups',
        'GET'
    )
    console.log('Entra booking calendar groups:', response.data.data);
    return response.data.data.map((option) => new ConfigurationSelectOption(option));
  } catch (error) {
    console.error('Error fetching Entra booking calendar groups:', error);
    return [];
  }
};

const load = async () => {
  await getModuleConfig();
};

load();
fetchEntraBookingCalendarGroups()
</script>

<template>
  <RestrictedPageWrapper :hasPermission="SessionUser.canAccessSuperUser()">
    <ConfigurationSubPageWrapper>
      <template #title>
        <PageTitle title="Entra configuration" subtitle="The configuration of the Entra integration" />
      </template>
      <template v-if="module_config.length > 0" #content>
        <ConfigurationCategory
          class="mt-2"
          module="Entra"
          title="General settings"
          description="The general settings for the Entra integration."
          icon="fas fa-cogs"
        >
        <ConfigurationSwitch
          class="mt-2"
          module="Entra"
          title="Enable Entra"
          description="Enable or disable the Entra integration."
          icon="fas fa-cogs"
          :value="getModuleConfigValue('enabled') === true"
          :on-switch="SessionUser.superUser.modules.entra.config.enabled.set"
        />
        </ConfigurationCategory>
        <ConfigurationCategory
          class="mt-2"
          module="Entra"
          title="Entra settings"
          description="The Entra settings for the integration."
          icon="fas fa-cogs"
        >
          <ConfigurationInput
            class="mt-2"
            module="Entra"
            title="Client ID"
            description="The client ID is used to identify the application in the Entra system."
            icon="fas fa-cogs"
            :value="getModuleConfigValue('entra_client_id')"
            :on-save="SessionUser.superUser.modules.entra.config.keys.entra_client_id.set"
          />
          <ConfigurationSecretKey
            class="mt-2"
            module="Entra"
            title="Client Secret"
            description="The client secret is used to authenticate the application in the Entra system."
            icon="fas fa-cogs"
            :is-set="getModuleConfigValue('entra_client_secret') !== ''"
            :on-save="SessionUser.superUser.modules.entra.config.keys.entra_client_secret.set"
          />
          <ConfigurationInput
            class="mt-2"
            module="Entra"
            title="Tenant ID"
            description="The tenant ID is used to identify the tenant in the Entra system."
            icon="fas fa-cogs"
            :value="getModuleConfigValue('entra_tenant_id')"
            :on-save="SessionUser.superUser.modules.entra.config.keys.entra_tenant_id.set"
          />
        </ConfigurationCategory>
        <ConfigurationCategory
          class="mt-2"
          module="Entra"
          title="Calendar settings"
          description="The calendar settings for the Entra integration."
          icon="fas fa-cogs"
        >
          <!-- Bookings calendar group -->
          <ConfigurationSelect
            class="mt-2"
            module="Entra"
            title="Bookings calendar group"
            description="The calendar group to use for bookings."
            icon="fas fa-calendar-alt"
            :value="getModuleConfigValue('entra_bookings_calendar_group')"
            :options="EntraBookingCalendarGroups"
          />
        </ConfigurationCategory>
      </template>
    </ConfigurationSubPageWrapper>
  </RestrictedPageWrapper>
</template>

<style scoped>

</style>