import { type Ref, computed, defineComponent, inject, onMounted, ref } from 'vue';
import { useI18n } from 'vue-i18n';

import { useVuelidate } from '@vuelidate/core';
import { email, maxLength, minLength, required } from '@vuelidate/validators';

import { type IUtenteApp } from '@/shared/model/utente-app.model';
import { Ruolo } from '@/shared/model/enumerations/ruolo.model';
import UtenteAppService from '@/entities/utente-app/utente-app.service';
import { useStore } from '@/store';

const validations = {
  utenteApp: {
    nome: {
      required,
      minLength: minLength(1),
      maxLength: maxLength(50),
    },
    cognome: {
      required,
      minLength: minLength(1),
      maxLength: maxLength(50),
    },
    dataNascita: {
      required,
    },
    luogoNascita: {
      required,
      minLength: minLength(1),
      maxLength: maxLength(100),
    },
    residente: {
      required,
      minLength: minLength(1),
      maxLength: maxLength(200),
    },
    telefono: {
      required,
      minLength: minLength(8),
      maxLength: maxLength(20),
    },
    societa: {
      maxLength: maxLength(100),
    },
    sede: {
      maxLength: maxLength(200),
    },
    codfiscale: {
      minLength: minLength(11),
      maxLength: maxLength(16),
    },
    telefonoSoc: {
      minLength: minLength(8),
      maxLength: maxLength(20),
    },
    emailSoc: {
      email,
      minLength: minLength(5),
      maxLength: maxLength(254),
    },
  },
};

export default defineComponent({
  name: 'Profile',
  validations,
  setup() {
    const store = useStore();
    const utenteAppService = inject<UtenteAppService>('utenteAppService', () => new UtenteAppService());

    const success: Ref<boolean> = ref(false);
    const error: Ref<boolean> = ref(false);
    const loading: Ref<boolean> = ref(true);

    const utenteApp: Ref<IUtenteApp> = ref({});

    const currentUser = computed(() => store.account);

    onMounted(async () => {
      try {
        loading.value = true;
        const result = await utenteAppService.getCurrentUser();

        if (result) {
          utenteApp.value = result;
        } else {
          // Initialize with user data
          utenteApp.value = {
            username: currentUser.value?.login,
            email: currentUser.value?.email,
            ruolo: Ruolo.USER,
            attivo: true,
          };
        }
      } catch (err) {
        // If not found, initialize with user data
        utenteApp.value = {
          username: currentUser.value?.login,
          email: currentUser.value?.email,
          ruolo: Ruolo.USER,
          attivo: true,
        };
      } finally {
        loading.value = false;
      }
    });

    return {
      success,
      error,
      loading,
      utenteApp,
      currentUser,
      v$: useVuelidate(),
      t$: useI18n().t,
      utenteAppService,
    };
  },
  methods: {
    async save() {
      this.success = false;
      this.error = false;

      try {
        await this.utenteAppService.saveCurrentUserProfile(this.utenteApp);
        this.success = true;
        this.error = false;
      } catch (ex) {
        this.success = false;
        this.error = true;
      }
    },
  },
});
