<template>
  <div class="guide-overlay" @click.self="$emit('close')">
    <div class="guide-modal">
      <button class="close-btn" @click="$emit('close')">✕</button>

      <h1>Getting Your Challonge API Key</h1>

      <div class="steps">
        <div class="step">
          <div class="step-header">
            <div class="step-number">1</div>
            <h2>Log Into Challonge</h2>
          </div>
          <div class="step-content">
            <p>
              Go to
              <a href="https://challonge.com/login" target="_blank"
                >Challonge.com</a
              >
              and log in with your account credentials.
            </p>
          </div>
        </div>

        <div class="step">
          <div class="step-header">
            <div class="step-number">2</div>
            <h2>Go to Developer Settings</h2>
          </div>
          <div class="step-content">
            <p>
              Once logged in, visit your
              <a
                href="https://challonge.com/settings/developer"
                target="_blank"
              >
                Developer Settings page
              </a>
            </p>
            <p>
              You'll see information about your account and any existing
              applications.
            </p>
          </div>
        </div>

        <div class="step">
          <div class="step-header">
            <div class="step-number">3</div>
            <h2>Click "Manage" Button</h2>
          </div>
          <div class="step-content">
            <p>
              On the Developer Settings page, look for a button labeled
              <strong>"Manage.challonge.com"</strong> or similar.
            </p>
            <p>Click this button to go to the app management portal.</p>
          </div>
        </div>

        <div class="step">
          <div class="step-header">
            <div class="step-number">4</div>
            <h2>Create a New Application</h2>
          </div>
          <div class="step-content">
            <p>
              On the app management page, look for a button to create a new
              application.
            </p>
            <p>
              Click it to create a new app. You'll be taken to the app
              creation/edit screen.
            </p>
          </div>
        </div>

        <div class="step">
          <div class="step-header">
            <div class="step-number">5</div>
            <h2>Fill in App Details</h2>
          </div>
          <div class="step-content">
            <p>On the app screen, you'll see several fields:</p>
            <ul class="field-list">
              <li>
                <strong>Name:</strong> Give your app a name (e.g., "Tournament
                Manager", "Pokedex Online")
              </li>
              <li>
                <strong>Description:</strong> Optional description of what the
                app does
              </li>
              <li>
                <strong>Reference link:</strong> Use
                <code>https://challonge.com</code> or your own website URL
              </li>
            </ul>
          </div>
        </div>

        <div class="step">
          <div class="step-header">
            <div class="step-number">6</div>
            <h2>Copy Your API Key</h2>
          </div>
          <div class="step-content">
            <p>
              After creating the app, you'll see your
              <strong>API Key</strong> displayed on the app screen.
            </p>
            <p class="important">
              ⚠️ <strong>Important:</strong> Copy this key carefully. It usually
              appears as a long string of characters.
            </p>
            <p>This is the key you'll store in the API Key Manager.</p>
          </div>
        </div>

        <div class="step">
          <div class="step-header">
            <div class="step-number">7</div>
            <h2>Store in API Key Manager</h2>
          </div>
          <div class="step-content">
            <p>
              Return to this app and go to the
              <strong>API Key Manager</strong> (linked below).
            </p>
            <p>Paste your API key there and click "Save Key".</p>
            <p class="success">
              ✅ Your API key is now stored and ready to use!
            </p>
          </div>
        </div>
      </div>

      <div class="guide-footer">
        <div class="security-note">
          <h3>🔒 Security Reminder</h3>
          <ul>
            <li>Never share your API key with anyone</li>
            <li>Don't commit it to version control or public repositories</li>
            <li>
              If you accidentally expose your key, regenerate it in your
              Challonge app settings
            </li>
            <li>
              Your key is stored securely in your browser (not sent to any
              server)
            </li>
          </ul>
        </div>

        <button class="btn btn-primary btn-large" @click="goToKeyManager">
          Go to API Key Manager
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router';

const router = useRouter();

defineEmits(['close']);

function goToKeyManager() {
  router.push('/api-key-manager');
}
</script>

<style scoped>
.guide-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 1000;
  overflow-y: auto;
}

.guide-modal {
  background: white;
  border-radius: 12px;
  padding: 3rem;
  max-width: 800px;
  width: 100%;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  position: relative;
  max-height: 90vh;
  overflow-y: auto;
}

.close-btn {
  position: absolute;
  top: 1.5rem;
  right: 1.5rem;
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: #999;
  transition: color 0.3s ease;
}

.close-btn:hover {
  color: #333;
}

h1 {
  color: #333;
  margin-bottom: 2rem;
  font-size: 2rem;
  text-align: center;
}

.steps {
  display: flex;
  flex-direction: column;
  gap: 2rem;
  margin-bottom: 2rem;
}

.step {
  background: #f8f9fa;
  border-left: 4px solid #667eea;
  padding: 1.5rem;
  border-radius: 8px;
}

.step-header {
  display: flex;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1rem;
}

.step-number {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  font-weight: 700;
  font-size: 1.1rem;
  flex-shrink: 0;
}

.step-header h2 {
  color: #333;
  font-size: 1.3rem;
  margin: 0;
  flex: 1;
}

.step-content {
  margin-left: calc(40px + 1rem);
}

.step-content p {
  color: #666;
  margin: 0.75rem 0;
  line-height: 1.6;
}

.step-content p:first-child {
  margin-top: 0;
}

.step-content p:last-child {
  margin-bottom: 0;
}

.field-list {
  list-style: none;
  padding: 0;
  margin: 1rem 0;
}

.field-list li {
  padding: 0.75rem;
  margin: 0.5rem 0;
  background: white;
  border-radius: 6px;
  border-left: 3px solid #667eea;
}

.field-list strong {
  color: #333;
  display: inline-block;
  min-width: 100px;
}

code {
  background: #e9ecef;
  padding: 0.2rem 0.4rem;
  border-radius: 3px;
  font-family: 'Courier New', monospace;
  font-size: 0.9rem;
}

.important {
  background: #fff3cd;
  padding: 1rem;
  border-radius: 6px;
  border-left: 4px solid #ffc107;
  color: #856404;
}

.success {
  background: #d4edda;
  padding: 1rem;
  border-radius: 6px;
  border-left: 4px solid #28a745;
  color: #155724;
  margin: 1rem 0 0 0;
}

.guide-footer {
  border-top: 2px solid #e9ecef;
  padding-top: 2rem;
}

.security-note {
  background: #f0f4ff;
  padding: 1.5rem;
  border-radius: 8px;
  margin-bottom: 1.5rem;
  border-left: 4px solid #667eea;
}

.security-note h3 {
  color: #667eea;
  margin-top: 0;
  font-size: 1.1rem;
}

.security-note ul {
  margin: 0;
  padding-left: 1.5rem;
  list-style: none;
}

.security-note li {
  color: #495057;
  margin: 0.5rem 0;
  padding-left: 1.5rem;
  position: relative;
}

.security-note li:before {
  content: '✓';
  position: absolute;
  left: 0;
  color: #667eea;
  font-weight: 700;
}

.btn {
  padding: 0.75rem 1.5rem;
  font-size: 1rem;
  font-weight: 600;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.btn-large {
  padding: 1rem 2rem;
  font-size: 1.05rem;
  display: block;
  width: 100%;
}

.step-content a {
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
}

.step-content a:hover {
  text-decoration: underline;
}

@media (max-width: 640px) {
  .guide-modal {
    padding: 1.5rem;
    border-radius: 12px;
  }

  h1 {
    font-size: 1.5rem;
    margin-bottom: 1.5rem;
  }

  .step {
    padding: 1rem;
  }

  .step-header {
    flex-direction: column;
    gap: 0.5rem;
  }

  .step-header h2 {
    font-size: 1.1rem;
  }

  .step-content {
    margin-left: 0;
  }

  .close-btn {
    font-size: 1.25rem;
  }
}
</style>
