import { type Ref, defineComponent, inject, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useRoute, useRouter } from 'vue-router';

import { useAlertService } from '@/shared/alert/alert.service';
import { useDateFormat } from '@/shared/composables';
import { type IAuditLog } from '@/shared/model/audit-log.model';

import AuditLogService from './audit-log.service';

export default defineComponent({
  name: 'AuditLogDetails',
  setup() {
    const dateFormat = useDateFormat();
    const auditLogService = inject('auditLogService', () => new AuditLogService());
    const alertService = inject('alertService', () => useAlertService(), true);

    const route = useRoute();
    const router = useRouter();

    const previousState = () => router.go(-1);
    const auditLog: Ref<IAuditLog> = ref({});

    const retrieveAuditLog = async auditLogId => {
      try {
        const res = await auditLogService().find(auditLogId);
        auditLog.value = res;
      } catch (error) {
        alertService.showHttpError(error.response);
      }
    };

    if (route.params?.auditLogId) {
      retrieveAuditLog(route.params.auditLogId);
    }

    return {
      ...dateFormat,
      alertService,
      auditLog,

      previousState,
      t$: useI18n().t,
    };
  },
});
