{% extends 'base.html' %}

{% block title %}Direct Spotify Authentication{% endblock %}

{% block content %}
<div class="max-w-4xl mx-auto px-4 py-8">
    <h1 class="text-3xl font-bold mb-2 text-navy-800 font-montserrat">Direct Spotify Authentication</h1>
    <p class="text-gray-600 mb-6">Use a manual bearer token to access the Spotify API directly.</p>

    <!-- Status Information -->
    {% if spotify_user %}
    <div class="bg-green-50 border border-green-200 rounded-lg p-4 mb-6">
        <div class="flex">
            <div class="flex-shrink-0">
                <svg class="h-5 w-5 text-green-400" viewBox="0 0 20 20" fill="currentColor">
                    <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd" />
                </svg>
            </div>
            <div class="ml-3">
                <h3 class="text-sm font-medium text-green-800">Currently authenticated</h3>
                <div class="mt-2 text-sm text-green-700">
                    <p>You are currently authenticated as <strong>{{ spotify_username }}</strong> (ID: {{ spotify_user }}).</p>
                </div>
                <div class="mt-4">
                    <form action="{{ url_for('import.direct_spotify_auth') }}" method="POST">
                        <input type="hidden" name="bearer_token" value="">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                        <button type="submit" class="inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-white bg-red-600 hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500">
                            Sign Out
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
    {% endif %}

    {% if error %}
    <div class="bg-red-50 border border-red-200 rounded-lg p-4 mb-6">
        <div class="flex">
            <div class="flex-shrink-0">
                <svg class="h-5 w-5 text-red-400" viewBox="0 0 20 20" fill="currentColor">
                    <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd" />
                </svg>
            </div>
            <div class="ml-3">
                <h3 class="text-sm font-medium text-red-800">Error</h3>
                <div class="mt-2 text-sm text-red-700">
                    <p>{{ error }}</p>
                </div>
            </div>
        </div>
    </div>
    {% endif %}

    {% if success %}
    <div class="bg-green-50 border border-green-200 rounded-lg p-4 mb-6">
        <div class="flex">
            <div class="flex-shrink-0">
                <svg class="h-5 w-5 text-green-400" viewBox="0 0 20 20" fill="currentColor">
                    <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd" />
                </svg>
            </div>
            <div class="ml-3">
                <h3 class="text-sm font-medium text-green-800">Success</h3>
                <div class="mt-2 text-sm text-green-700">
                    <p>{{ success }}</p>
                </div>
            </div>
        </div>
    </div>
    {% endif %}

    <!-- Instructions -->
    <div class="bg-white shadow-md rounded-lg p-6 mb-8">
        <h2 class="text-xl font-semibold mb-4 text-navy-800">How to Get a Bearer Token</h2>
        <ol class="list-decimal pl-5 space-y-4 text-gray-700">
            <li>
                <p class="mb-1">Go to the <a href="https://developer.spotify.com/console/" target="_blank" class="text-teal-600 hover:underline">Spotify Developer Console</a></p>
                <p class="text-sm text-gray-600">You'll need to log in with your Spotify account</p>
            </li>
            <li>
                <p class="mb-1">Select any API endpoint (e.g., "Get Current User's Profile")</p>
                <p class="text-sm text-gray-600">The specific endpoint doesn't matter, we just need to generate a token</p>
            </li>
            <li>
                <p class="mb-1">Click the "Get Token" button</p>
                <p class="text-sm text-gray-600">Make sure to select the following scopes:</p>
                <ul class="list-disc pl-5 mt-1 text-sm text-gray-600">
                    <li>user-read-private</li>
                    <li>user-read-email</li>
                    <li>playlist-read-private</li>
                    <li>playlist-read-collaborative</li>
                </ul>
            </li>
            <li>
                <p class="mb-1">Copy the generated OAuth token (it starts with "BQ...")</p>
            </li>
            <li>
                <p class="mb-1">Paste the token in the form below and click "Authenticate"</p>
            </li>
        </ol>
    </div>

    <!-- Token Entry Form -->
    <div class="bg-white shadow-md rounded-lg p-6">
        <h2 class="text-xl font-semibold mb-4 text-navy-800">Enter Your Bearer Token</h2>
        <form action="{{ url_for('import.direct_spotify_auth') }}" method="POST">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
            
            <div class="mb-4">
                <label for="bearer_token" class="block text-sm font-medium text-gray-700 mb-1">Bearer Token</label>
                <textarea name="bearer_token" id="bearer_token" rows="3" 
                    placeholder="BQBcQ1foQG0x14axu2kQVz..." 
                    class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-teal-500 focus:border-teal-500"></textarea>
                <p class="mt-1 text-xs text-gray-500">The token will expire after about 1 hour. You'll need to generate a new one after that.</p>
            </div>
            
            <div class="mt-4">
                <button type="submit" class="inline-flex items-center px-4 py-2 border border-transparent text-base font-medium rounded-md shadow-sm text-white bg-orange-500 hover:bg-orange-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-orange-500">
                    Authenticate
                </button>
            </div>
        </form>
    </div>

    <!-- Next Steps -->
    {% if spotify_user %}
    <div class="mt-8 text-center">
        <h3 class="text-lg font-semibold mb-2">Ready to go!</h3>
        <p class="text-gray-600 mb-4">You're authenticated and can now use the direct Spotify client.</p>
        <div class="flex justify-center space-x-4">
            <a href="{{ url_for('import.direct_official_playlists') }}" 
               class="inline-flex items-center px-4 py-2 border border-transparent text-base font-medium rounded-md text-white bg-teal-600 hover:bg-teal-700">
                Browse Official Playlists
            </a>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}