<x-app-layout>

    <x-module-hero
        title="Edit Question"
        description="{{ \Illuminate\Support\Str::limit($question->title, 60) }}"
        icon="fa-circle-question">

        <x-slot name="backButton">
            <a href="{{ route('knowledge-sharing.knowledge-base.show', $question) }}"
               class="inline-flex items-center gap-1 text-sm text-[#475569] hover:text-[#1e293b] transition-colors">
                <i class="fas fa-arrow-left text-xs"></i> Back to Question
            </a>
        </x-slot>

        <x-slot name="tabs">
            @can('view discussion topics')
            <a href="{{ route('knowledge-sharing.discussions.topics.index') }}"
               class="module-hero-tab {{ request()->routeIs('knowledge-sharing.discussions.topics.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }} inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-lightbulb text-sm"></i> Discussion Topics
            </a>
            @endcan
            @can('view discussions')
            <a href="{{ route('knowledge-sharing.discussions.index') }}"
               class="module-hero-tab {{ request()->routeIs('knowledge-sharing.discussions.index') || (request()->routeIs('knowledge-sharing.discussions.*') && !request()->routeIs('knowledge-sharing.discussions.topics.*') && !request()->routeIs('knowledge-sharing.discussions.replies.*')) ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }} inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-comments text-sm"></i> Discussions
            </a>
            @endcan
            @if(Route::has('knowledge-sharing.discussions.replies.index'))
            @can('view discussion replies')
            <a href="{{ route('knowledge-sharing.discussions.replies.index') }}"
               class="module-hero-tab {{ request()->routeIs('knowledge-sharing.discussions.replies.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }} inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-reply-all text-sm"></i> All Replies
            </a>
            @endcan
            @endif
            @can('manage knowledge base categories')
            <a href="{{ route('knowledge-sharing.knowledge-base.categories.index') }}"
               class="module-hero-tab {{ request()->routeIs('knowledge-sharing.knowledge-base.categories.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }} inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-book-open text-sm"></i> KB Categories
            </a>
            @endcan
            @can('view knowledge base questions')
            <a href="{{ route('knowledge-sharing.knowledge-base.index') }}"
               class="module-hero-tab {{ request()->routeIs('knowledge-sharing.knowledge-base.index') || (request()->routeIs('knowledge-sharing.knowledge-base.*') && !request()->routeIs('knowledge-sharing.knowledge-base.categories.*')) ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }} inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-circle-question text-sm"></i> Knowledge Base Q&amp;A
            </a>
            @endcan
            @hasRole('Contributor')
            <a href="{{ route('contributor.dashboard') }}"
               class="module-hero-tab {{ request()->routeIs('contributor.dashboard') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }} inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-pen-nib text-sm"></i> Contributor Dashboard
            </a>
            @endhasRole
        </x-slot>

    </x-module-hero>

    <div class="p-6">

        @if($errors->any())
            <div class="mb-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg">
                <p class="font-semibold mb-1"><i class="fas fa-exclamation-circle mr-1"></i> Please fix the following errors:</p>
                <ul class="list-disc list-inside text-sm space-y-0.5">
                    @foreach($errors->all() as $error)<li>{{ $error }}</li>@endforeach
                </ul>
            </div>
        @endif

        <form method="POST" action="{{ route('knowledge-sharing.knowledge-base.update', $question) }}">
            @csrf
            @method('PUT')

            <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden mb-6">
                <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                    <h3 class="text-base font-semibold text-gray-800">
                        <i class="fas fa-circle-question text-green-600 mr-2"></i> Question Details
                    </h3>
                </div>
                <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">

                    <div class="md:col-span-2">
                        <label for="title" class="block text-sm font-medium text-gray-700 mb-1">
                            Question Title <span class="text-red-500">*</span>
                        </label>
                        <input type="text" id="title" name="title" value="{{ old('title', $question->title) }}"
                               class="w-full px-4 py-2.5 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-transparent"
                               required>
                        <x-input-error :messages="$errors->get('title')" class="mt-1" />
                    </div>

                    <div>
                        <label for="category_id" class="block text-sm font-medium text-gray-700 mb-1">
                            Category <span class="text-red-500">*</span>
                        </label>
                        <select id="category_id" name="category_id"
                                class="w-full px-4 py-2.5 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-transparent"
                                required>
                            @foreach($categories as $category)
                                <option value="{{ $category->id }}" {{ old('category_id', $question->category_id) == $category->id ? 'selected' : '' }}>
                                    {{ $category->name }}
                                </option>
                            @endforeach
                        </select>
                        <x-input-error :messages="$errors->get('category_id')" class="mt-1" />
                    </div>

                    <div>
                        <label for="status" class="block text-sm font-medium text-gray-700 mb-1">Status</label>
                        <select id="status" name="status"
                                class="w-full px-4 py-2.5 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-transparent">
                            <option value="published" {{ old('status', $question->status) === 'published' ? 'selected' : '' }}>Published</option>
                            <option value="draft" {{ old('status', $question->status) === 'draft' ? 'selected' : '' }}>Draft</option>
                            <option value="pending_review" {{ old('status', $question->status) === 'pending_review' ? 'selected' : '' }}>Pending Review</option>
                            <option value="archived" {{ old('status', $question->status) === 'archived' ? 'selected' : '' }}>Archived</option>
                        </select>
                        <x-input-error :messages="$errors->get('status')" class="mt-1" />
                    </div>

                    <div>
                        <label for="difficulty" class="block text-sm font-medium text-gray-700 mb-1">Difficulty Level</label>
                        <select id="difficulty" name="difficulty"
                                class="w-full px-4 py-2.5 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-transparent">
                            <option value="">Select difficulty</option>
                            <option value="beginner" {{ old('difficulty', $question->difficulty) === 'beginner' ? 'selected' : '' }}>Beginner</option>
                            <option value="intermediate" {{ old('difficulty', $question->difficulty) === 'intermediate' ? 'selected' : '' }}>Intermediate</option>
                            <option value="advanced" {{ old('difficulty', $question->difficulty) === 'advanced' ? 'selected' : '' }}>Advanced</option>
                        </select>
                        <x-input-error :messages="$errors->get('difficulty')" class="mt-1" />
                    </div>

                    <div>
                        <label for="tags" class="block text-sm font-medium text-gray-700 mb-1">Tags</label>
                        <input type="text" id="tags" name="tags"
                               value="{{ old('tags', is_array($question->tags) ? implode(', ', $question->tags) : $question->tags) }}"
                               placeholder="e.g., tutorial, api, integration"
                               class="w-full px-4 py-2.5 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-transparent">
                        <p class="text-xs text-gray-400 mt-1">Comma-separated tags</p>
                        <x-input-error :messages="$errors->get('tags')" class="mt-1" />
                    </div>

                    <div class="md:col-span-2">
                        <label for="content" class="block text-sm font-medium text-gray-700 mb-1">
                            Content <span class="text-red-500">*</span>
                        </label>
                        <textarea id="content" name="content" rows="10"
                                  class="w-full px-4 py-2.5 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-transparent"
                                  required>{{ old('content', $question->content) }}</textarea>
                        <x-input-error :messages="$errors->get('content')" class="mt-1" />
                    </div>

                    <div class="flex items-center gap-6">
                        <label class="flex items-center gap-2 cursor-pointer">
                            <input type="checkbox" name="is_active" value="1" {{ old('is_active', $question->is_active ?? true) ? 'checked' : '' }}
                                   class="w-4 h-4 text-green-600 rounded border-gray-300">
                            <span class="text-sm text-gray-700">Active (visible)</span>
                        </label>
                        @can('feature knowledge base questions')
                        <label class="flex items-center gap-2 cursor-pointer">
                            <input type="checkbox" name="is_featured" value="1" {{ old('is_featured', $question->is_featured) ? 'checked' : '' }}
                                   class="w-4 h-4 text-green-600 rounded border-gray-300">
                            <span class="text-sm text-gray-700">Featured</span>
                        </label>
                        @endcan
                    </div>

                </div>
            </div>

            {{-- SEO --}}
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden mb-6">
                <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                    <h3 class="text-base font-semibold text-gray-800">
                        <i class="fas fa-search text-blue-500 mr-2"></i> SEO Settings
                    </h3>
                </div>
                <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label for="meta_title" class="block text-sm font-medium text-gray-700 mb-1">Meta Title</label>
                        <input type="text" id="meta_title" name="meta_title" value="{{ old('meta_title', $question->meta_title) }}"
                               class="w-full px-4 py-2.5 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-transparent">
                    </div>
                    <div>
                        <label for="meta_keywords" class="block text-sm font-medium text-gray-700 mb-1">Meta Keywords</label>
                        <input type="text" id="meta_keywords" name="meta_keywords" value="{{ old('meta_keywords', $question->meta_keywords) }}"
                               class="w-full px-4 py-2.5 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-transparent">
                    </div>
                    <div class="md:col-span-2">
                        <label for="meta_description" class="block text-sm font-medium text-gray-700 mb-1">Meta Description</label>
                        <textarea id="meta_description" name="meta_description" rows="2"
                                  class="w-full px-4 py-2.5 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-transparent">{{ old('meta_description', $question->meta_description) }}</textarea>
                    </div>
                </div>
            </div>

            <div class="flex items-center justify-between">
                @can('delete knowledge base questions')
                <form action="{{ route('knowledge-sharing.knowledge-base.destroy', $question) }}" method="POST"
                      onsubmit="return confirm('Delete this question?')">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn-action-delete">
                        <i class="fas fa-trash"></i> Delete Question
                    </button>
                </form>
                @endcan
                <div class="flex items-center gap-3">
                    <a href="{{ route('knowledge-sharing.knowledge-base.show', $question) }}"
                       class="px-5 py-2.5 border border-gray-300 rounded-lg text-sm text-gray-700 hover:bg-gray-50 transition-colors">
                        Cancel
                    </a>
                    <button type="submit"
                            class="px-5 py-2.5 bg-green-600 text-white text-sm font-medium rounded-lg hover:bg-green-700 transition-colors">
                        <i class="fas fa-save mr-1"></i> Save Changes
                    </button>
                </div>
            </div>

        </form>
    </div>

</x-app-layout>
