<template>
  <div class="page">
    <div class="left" :style="{ background: `linear-gradient(135deg, ${primaryColor}e6, ${primaryColor}, ${primaryColor}cc)` }">
      <div class="brand">
        <div class="brand-icon"><StarOutlined :style="{ fontSize: '16px' }" /></div>
        <span>{{ t('app.name') }}</span>
      </div>
      <div class="characters-area">
        <AnimatedCharacters
          :is-typing="isTyping"
          :has-secret="!!formState.password"
          :secret-visible="showPassword"
        />
      </div>
      <div class="footer-links">
        <a href="#">Privacy Policy</a>
        <a href="#">Terms of Service</a>
        <a href="#">Contact</a>
      </div>
      <div class="deco-grid" />
      <div class="deco-circle deco-circle-1" />
      <div class="deco-circle deco-circle-2" />
    </div>
    <div class="right">
      <div class="form-wrapper">
        <div class="mobile-brand">
          <div class="brand-icon"><StarOutlined :style="{ fontSize: '16px' }" /></div>
          <span>{{ t('app.name') }}</span>
        </div>
        <div class="header">
          <h1>{{ t('auth.welcomeBack') }}</h1>
          <p>{{ t('auth.tenantAdminLogin') }}</p>
        </div>
        <form @submit.prevent="handleSubmit" class="form">
          <div class="field">
            <label for="login-identifier">{{ t('auth.loginIdentifier') }}</label>
            <input id="login-identifier" type="text" placeholder="admin@geo.local / 13800138000" v-model="formState.identifier"
              autocomplete="off" @focus="isTyping = true" @blur="isTyping = false" required />
          </div>
          <div class="field">
            <label for="login-password">{{ t('auth.password') }}</label>
            <div class="password-wrap">
              <input id="login-password" :type="showPassword ? 'text' : 'password'"
                placeholder="Admin@123" v-model="formState.password" required />
              <button type="button" class="eye-btn" @click="showPassword = !showPassword">
                <EyeInvisibleOutlined v-if="showPassword" />
                <EyeOutlined v-else />
              </button>
            </div>
          </div>
          <div class="options">
            <label class="remember"><input type="checkbox" v-model="remember" /> Remember for 30 days</label>
            <a href="#" class="forgot">Forgot password?</a>
          </div>
          
          <button type="submit" class="btn-primary" :disabled="submitting"
            :style="{ background: primaryColor }">
            {{ submitting ? t('auth.loginAndEnter') + '...' : t('auth.loginAndEnter') }}
          </button>
        </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>
  </div>
</template>

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

import { formatError } from "@/lib/errors";
import { useAuthStore } from "@/stores/auth";
import AnimatedCharacters from "@/components/login-animation/AnimatedCharacters.vue";

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

const primaryColor = ref('#1f5cff');

const submitting = ref(false);
const showPassword = ref(false);
const isTyping = ref(false);
const remember = ref(false);

const formState = reactive({
  identifier: "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>

<style scoped>
.page { 
  display: grid; 
  grid-template-columns: 1fr 1fr; 
  min-height: 100vh; 
  width: 100%;
  flex: 1;
}

.left {
  position: relative; display: flex; flex-direction: column; justify-content: space-between;
  padding: 48px; color: white; overflow: hidden;
}
.brand { position: relative; z-index: 20; display: flex; align-items: center; gap: 8px; font-size: 18px; font-weight: 600; }
.brand-icon {
  width: 32px; height: 32px; border-radius: 8px; background: rgba(255,255,255,0.1);
  backdrop-filter: blur(8px); display: flex; align-items: center; justify-content: center;
}
.characters-area { position: relative; z-index: 20; display: flex; align-items: flex-end; justify-content: center; height: 500px; transform: scale(0.95); }
.footer-links { position: relative; z-index: 20; display: flex; gap: 32px; font-size: 14px; }
.footer-links a { color: rgba(255,255,255,0.6); transition: color 0.2s; }
.footer-links a:hover { color: white; }
.deco-grid {
  position: absolute; inset: 0;
  background-image: linear-gradient(to right, rgba(255,255,255,0.05) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(255,255,255,0.05) 1px, transparent 1px);
  background-size: 20px 20px;
}
.deco-circle { position: absolute; border-radius: 50%; filter: blur(48px); }
.deco-circle-1 { top: 25%; right: 25%; width: 256px; height: 256px; background: rgba(255,255,255,0.1); }
.deco-circle-2 { bottom: 25%; left: 25%; width: 384px; height: 384px; background: rgba(255,255,255,0.05); }
.right { display: flex; align-items: center; justify-content: center; padding: 32px; background: #fff; }
.form-wrapper { width: 100%; max-width: 420px; }
.mobile-brand { display: none; }
.header { text-align: center; margin-bottom: 40px; }
.header h1 { font-size: 30px; font-weight: 700; letter-spacing: -0.5px; margin-bottom: 8px; color: #18181b; }
.header p { color: #71717a; font-size: 14px; margin: 0; }
.form { display: flex; flex-direction: column; gap: 20px; margin-bottom: 32px; }
.field { display: flex; flex-direction: column; gap: 8px; }
.field label { font-size: 14px; font-weight: 500; color: #18181b; }
.field input {
  height: 48px; padding: 0 12px; border-radius: 6px; border: 1px solid #d4d4d8;
  font-size: 14px; outline: none; background: #fff; transition: border-color 0.2s; color: #18181b;
}
.field input:focus { border-color: #1f5cff; box-shadow: 0 0 0 2px rgba(31,92,255,0.2); }
.password-wrap { position: relative; }
.password-wrap input { width: 100%; padding-right: 40px; box-sizing: border-box; }
.eye-btn {
  position: absolute; right: 12px; top: 50%; transform: translateY(-50%);
  background: none; border: none; color: #a1a1aa; transition: color 0.2s;
  display: flex; align-items: center; justify-content: center; font-size: 18px; cursor: pointer;
}
.eye-btn:hover { color: #3f3f46; }
.options { display: flex; align-items: center; justify-content: space-between; font-size: 14px; }
.remember { display: flex; align-items: center; gap: 8px; cursor: pointer; color: #18181b; }
.remember input { width: 16px; height: 16px; accent-color: #1f5cff; cursor: pointer; }
.forgot { color: #1f5cff; font-weight: 500; }
.forgot:hover { text-decoration: underline; }

.btn-primary {
  width: 100%; height: 48px; font-size: 16px; font-weight: 500; border: none; border-radius: 6px;
  color: white; transition: opacity 0.2s; cursor: pointer;
}
.btn-primary:hover { opacity: 0.9; }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.login-credentials {
  margin-top: 24px;
  padding: 16px;
  background: #f8fafc;
  border: 1px solid #e6edf5;
  border-radius: 12px;
}
.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, #71717a);
  font-size: 13px;
}
.login-credentials__row strong {
  color: #18181b;
  font-size: 13px;
}

@media (max-width: 1023px) {
  .page { grid-template-columns: 1fr; }
  .left { display: none; }
  .mobile-brand { display: flex; align-items: center; justify-content: center; gap: 8px; font-size: 18px; font-weight: 600; margin-bottom: 48px; color: #18181b; }
}
</style>
