<template>
    <div>
      <h2>{{ $t('auth.activateAccount') }}</h2>
      <button @click="activate">{{ $t('auth.activate') }}</button>
    </div>
  
    
    <!-- Info Dialog -->
    <InfoDialog
        v-model="infoDialog.isOpen"
        :title="infoDialog.title"
        :message="infoDialog.message"
        :details="infoDialog.details"
        :type="infoDialog.type"
    />
    
    <!-- Confirm Dialog -->
    <ConfirmDialog
        v-model="confirmDialog.isOpen"
        :title="confirmDialog.title"
        :message="confirmDialog.message"
        :details="confirmDialog.details"
        :type="confirmDialog.type"
        @confirm="handleConfirmResult(true)"
        @cancel="handleConfirmResult(false)"
    />
</template>
  
  <script>
  import apiClient from '../apiClient.js';
  
  import InfoDialog from '../components/InfoDialog.vue';
import ConfirmDialog from '../components/ConfirmDialog.vue';
import { buildInfoConfig, buildConfirmConfig, safeErrorMessage } from '../utils/dialogUtils.js';
export default {
    data() {
        return {
            // Dialog States
            infoDialog: {
                isOpen: false,
                title: '',
                message: '',
                details: '',
                type: 'info'
            },
            confirmDialog: {
                isOpen: false,
                title: '',
                message: '',
                details: '',
                type: 'info',
                resolveCallback: null
            }
        };
    },
    mounted() {
        // Aktivierung direkt beim Aufruf des Links ausführen
        this.activate();
    },
    methods: {
        // Dialog Helper Methods
        async showInfo(title, message, details = '', type = 'info') {
            this.infoDialog = buildInfoConfig({ title, message, details, type });
        },
        
        async showConfirm(title, message, details = '', type = 'info', options = {}) {
            return new Promise((resolve) => {
                this.confirmDialog = buildConfirmConfig({
                    title,
                    message,
                    details,
                    type,
                    resolveCallback: resolve,
                    ...options,
                });
            });
        },
        
        handleConfirmResult(confirmed) {
            if (this.confirmDialog.resolveCallback) {
                this.confirmDialog.resolveCallback(confirmed);
                this.confirmDialog.resolveCallback = null;
            }
            this.confirmDialog.isOpen = false;
        },
        
      async activate() {
        try {
          const activationCode = this.$route.params.activationCode;
          await apiClient.get(`/auth/activate/${activationCode}`);
          await this.showInfo(this.$t('messages.success'), this.$t('auth.accountActivated'), '', 'success');
          this.$router.push('/login');
        } catch (error) {
            const message = safeErrorMessage(error, this.$t('auth.activationFailed'));
            await this.showInfo(this.$t('messages.error'), message, '', 'error');
        }
      },
    },
  };
  </script>
  