<template>
  <div class="sso-container">
    <div class="sso-card">
      <img src="@/assets/logo.svg" alt="Logo" class="logo" />
      <h1>Login SSO</h1>
      
      <form @submit.prevent="handleSSOLogin" class="sso-form">
        <BaseInput
          id="email"
          v-model="email"
          label="Email Corporativo"
          type="email"
          :validations="[validators.required, validators.email]"
          @validation="updateValidation('email', $event)"
        />

        <BaseButton
          type="submit"
          :disabled="!isFormValid || isLoading"
          :loading="isLoading"
        >
          Entrar com SSO
        </BaseButton>

        <div class="alternative-actions">
          <router-link to="/login">Login Padrão</router-link>
          <router-link to="/forgot-password">Esqueceu a senha?</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import BaseInput from '@/components/common/BaseInput.vue'
import BaseButton from '@/components/common/BaseButton.vue'
import { validators } from '@/utils/validators'

export default {
  name: 'SSOView',
  components: {
    BaseInput,
    BaseButton
  },
  setup() {
    const store = useStore()
    const router = useRouter()
    
    const email = ref('')
    const isLoading = ref(false)
    const validations = ref({
      email: false
    })

    const isFormValid = computed(() => {
      return Object.values(validations.value).every(valid => valid)
    })

    const updateValidation = (field, isValid) => {
      validations.value[field] = isValid
    }

    const handleSSOLogin = async () => {
      if (!isFormValid.value) return

      isLoading.value = true
      try {
        await store.dispatch('auth/loginWithSSO', { email: email.value })
        router.push('/dashboard')
      } catch (error) {
        console.error('SSO login failed:', error)
      } finally {
        isLoading.value = false
      }
    }

    return {
      email,
      isLoading,
      isFormValid,
      validators,
      updateValidation,
      handleSSOLogin
    }
  }
}
</script>

<style scoped>
.sso-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1rem;
  background-color: #f5f5f5;
}

.sso-card {
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
  height: 48px;
}

.sso-form {
  margin-top: 2rem;
}

.alternative-actions {
  margin-top: 1rem;
  display: flex;
  justify-content: space-between;
  font-size: 0.875rem;
}

.alternative-actions a {
  color: #666;
  text-decoration: none;
}

.alternative-actions a:hover {
  text-decoration: underline;
}
</style>
