<script setup lang="ts">
import { LockOutlined, MailOutlined } from "@ant-design/icons-vue";
import { message } from "ant-design-vue";
import { reactive, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useI18n } from "vue-i18n";

import { formatError } from "@/lib/errors";
import { useAuthStore } from "@/stores/auth";

const authStore = useAuthStore();
const router = useRouter();
const route = useRoute();
const { t } = useI18n();
const submitting = ref(false);

const formState = reactive({
  email: "admin@geo.local",
  password: "Admin@123",
});

async function handleSubmit(): Promise<void> {
  if (submitting.value) {
    return;
  }

  submitting.value = true;

  try {
    await authStore.login(formState);
    const redirect = typeof route.query.redirect === "string" ? route.query.redirect : "/workspace";
    await router.replace(redirect);
  } catch (error) {
    message.error(formatError(error));
  } finally {
    submitting.value = false;
  }
}
</script>

<template>
  <div class="login-screen">
    <div class="login-screen__cover">
      <div class="login-screen__cover-copy">
        <p class="eyebrow">Tenant Admin</p>
        <h1>{{ t("app.name") }}</h1>
        <p>{{ t("app.loginIntro") }}</p>
      </div>
    </div>

    <div class="login-screen__form-container">
      <div class="login-header">
        <h2>{{ t("auth.welcomeBack") }}</h2>
        <p>{{ t("auth.tenantAdminLogin") }}</p>
      </div>

      <a-form
        layout="vertical"
        :model="formState"
        class="login-form"
        @finish="handleSubmit"
        @submit.prevent="handleSubmit"
      >
        <a-form-item :label="t('auth.email')" name="email">
          <a-input v-model:value="formState.email" size="large" placeholder="admin@geo.local">
            <template #prefix><MailOutlined class="login-form__icon" /></template>
          </a-input>
        </a-form-item>

        <a-form-item :label="t('auth.password')" name="password">
          <a-input-password v-model:value="formState.password" size="large" placeholder="Admin@123">
            <template #prefix><LockOutlined class="login-form__icon" /></template>
          </a-input-password>
        </a-form-item>

        <a-button
          type="primary"
          size="large"
          block
          html-type="submit"
          :loading="submitting"
          @click="handleSubmit"
        >
          {{ t("auth.loginAndEnter") }}
        </a-button>
      </a-form>

      <div class="login-credentials">
        <div class="login-credentials__row">
          <span>{{ t("auth.defaultTestAccount") }}</span>
          <strong>admin@geo.local</strong>
        </div>
        <div class="login-credentials__row">
          <span>{{ t("auth.defaultTestPassword") }}</span>
          <strong>Admin@123</strong>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-screen {
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(420px, 500px);
  min-height: 100vh;
  background: #eef3f9;
}

.login-screen__cover {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 48px;
  background:
    radial-gradient(circle at top left, rgba(112, 172, 255, 0.35), transparent 34%),
    linear-gradient(135deg, #0f172a 0%, #1f5cff 48%, #57b5ff 100%);
  color: #fff;
}

.login-screen__cover-copy {
  max-width: 520px;
}

.login-screen__cover h1 {
  margin: 0;
  font-size: 56px;
  line-height: 0.96;
}

.login-screen__cover p:last-child {
  max-width: 420px;
  margin: 18px 0 0;
  color: rgba(255, 255, 255, 0.82);
  font-size: 18px;
  line-height: 1.6;
}

.login-screen__form-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 64px 48px;
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(14px);
  box-shadow: -16px 0 40px rgba(15, 23, 42, 0.08);
}

.login-header {
  margin-bottom: 36px;
}

.login-header h2 {
  margin: 0 0 8px;
  font-size: 32px;
}

.login-header p {
  margin: 0;
  color: var(--muted);
}

.login-form__icon {
  color: rgba(0, 0, 0, 0.25);
}

.login-credentials {
  margin-top: 24px;
  padding: 16px;
  background: #f8fafc;
  border: 1px solid #e6edf5;
  border-radius: 16px;
}

.login-credentials__row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
}

.login-credentials__row + .login-credentials__row {
  margin-top: 8px;
}

.login-credentials__row span {
  color: var(--muted);
  font-size: 13px;
}

@media (max-width: 980px) {
  .login-screen {
    grid-template-columns: 1fr;
  }

  .login-screen__cover {
    min-height: 280px;
  }
}

@media (max-width: 640px) {
  .login-screen__form-container,
  .login-screen__cover {
    padding: 32px 20px;
  }

  .login-screen__cover h1 {
    font-size: 40px;
  }
}
</style>
