@extends('admin.layouts.app')

@section('title', __('admin/ai-staff.training_edit_title'))

@section('content')
<div class="container-fluid px-4">
    @include('admin.ai-staff._nav')

    <x-admin.page-header
        :title="__('admin/ai-staff.training_edit_title')"
        :subtitle="__('admin/ai-staff.training_edit_subtitle')"
        :buttons="[
            [
                'label' => __('admin/ai-staff.buttons_back'),
                'route' => route('admin.ai-staff.training.index'),
                'icon' => 'fas fa-arrow-left',
                'color' => 'outline-secondary',
            ],
        ]"
    />

    <form id="trainingDatasetForm" method="POST" action="{{ route('admin.ai-staff.training.update', $dataset) }}">
        @csrf
        @method('PUT')

        <div class="row">
            <!-- Main Form Column -->
            <div class="col-lg-8">
                <!-- Dataset Details Card -->
                <x-admin.card :title="__('admin/ai-staff.training_card_details')" icon="fas fa-brain" class="mb-4">
                    <div class="mb-3">
                        <label for="name" class="form-label">{{ __('admin/ai-staff.training_fields_name') }} <span class="text-danger">*</span></label>
                        <input type="text" class="form-control @error('name') is-invalid @enderror"
                               id="name" name="name" value="{{ old('name', $dataset->name) }}" required>
                        @error('name')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                    <div class="mb-3">
                        <label for="description" class="form-label">{{ __('admin/ai-staff.training_fields_description') }}</label>
                        <textarea class="form-control @error('description') is-invalid @enderror"
                                  id="description" name="description" rows="4">{{ old('description', $dataset->description) }}</textarea>
                        @error('description')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="type" class="form-label">{{ __('admin/ai-staff.training_fields_type') }} <span class="text-danger">*</span></label>
                                <select class="form-select @error('type') is-invalid @enderror"
                                        id="type" name="type" required>
                                    <option value="">{{ __('admin/ai-staff.training_select_type') }}</option>
                                    <option value="qa_pairs" {{ old('type', $dataset->type) == 'qa_pairs' ? 'selected' : '' }}>{{ __('admin/ai-staff.training_type_qa_pairs') }}</option>
                                    <option value="instructions" {{ old('type', $dataset->type) == 'instructions' ? 'selected' : '' }}>{{ __('admin/ai-staff.training_type_instructions') }}</option>
                                    <option value="corrections" {{ old('type', $dataset->type) == 'corrections' ? 'selected' : '' }}>{{ __('admin/ai-staff.training_type_corrections') }}</option>
                                </select>
                                @error('type')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="dataset_status" class="form-label">{{ __('admin/ai-staff.training_fields_status') }}</label>
                                <select class="form-select @error('status') is-invalid @enderror"
                                        id="dataset_status" name="status">
                                    <option value="draft" {{ old('status', $dataset->status) == 'draft' ? 'selected' : '' }}>{{ __('admin/ai-staff.training_status_draft') }}</option>
                                    <option value="ready" {{ old('status', $dataset->status) == 'ready' ? 'selected' : '' }}>{{ __('admin/ai-staff.training_status_ready') }}</option>
                                </select>
                                @error('status')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>
                </x-admin.card>

                <!-- Tabs: Training Items / Import / Fine-Tune -->
                <x-admin.card class="mb-4">
                    <x-slot:header>
                        <x-admin.tab-nav
                            id="trainingTabs"
                            type="card"
                            class=""
                            :tabs="[
                                [
                                    'id' => 'items-pane',
                                    'label' => __('admin/ai-staff.training_tab_items'),
                                    'icon' => 'fas fa-list',
                                    'badge' => $dataset->items_count ?? 0,
                                    'badgeColor' => 'primary',
                                ],
                                [
                                    'id' => 'import-pane',
                                    'label' => __('admin/ai-staff.training_tab_import'),
                                    'icon' => 'fas fa-file-import',
                                ],
                                [
                                    'id' => 'finetune-pane',
                                    'label' => __('admin/ai-staff.training_tab_fine_tune'),
                                    'icon' => 'fas fa-magic',
                                ],
                            ]"
                        />
                    </x-slot:header>

                    <div class="tab-content" id="trainingTabsContent">
                        <!-- Training Items Tab -->
                        <div class="tab-pane fade show active" id="items-pane" role="tabpanel" aria-labelledby="items-pane-tab">
                            <div class="d-flex justify-content-between align-items-center mb-3">
                                <p class="text-muted mb-0 small">{{ __('admin/ai-staff.training_items_description') }}</p>
                                <button type="button" class="btn btn-outline-primary btn-sm" id="addTrainingItemBtn">
                                    <i class="fas fa-plus me-1"></i>{{ __('admin/ai-staff.training_buttons_add_item') }}
                                </button>
                            </div>
                            <div class="table-responsive">
                                <table id="trainingItemsTable" class="table table-hover align-middle mb-0"
                                       data-records-url="{{ route('api.admin.ai-staff.training.items.records', $dataset) }}">
                                    <thead>
                                        <tr>
                                            <th>{{ __('admin/ai-staff.training_items_user_message') }}</th>
                                            <th>{{ __('admin/ai-staff.training_items_assistant_message') }}</th>
                                            <th>{{ __('admin/ai-staff.training_items_source') }}</th>
                                            <th>{{ __('admin/ai-staff.training_items_approved') }}</th>
                                            <th class="text-end">{{ __('admin/common.actions') }}</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>
                        </div>

                        <!-- Import Tab -->
                        <div class="tab-pane fade" id="import-pane" role="tabpanel" aria-labelledby="import-pane-tab">
                            <p class="text-muted mb-3">{{ __('admin/ai-staff.training_import_description') }}</p>
                            <div class="row g-3">
                                <div class="col-md-4">
                                    <div class="card border-0 shadow-sm h-100">
                                        <div class="card-body text-center">
                                            <div class="mb-3">
                                                <div class="rounded-circle d-inline-flex align-items-center justify-content-center"
                                                     style="width: 56px; height: 56px; background-color: rgba(var(--bs-primary-rgb), 0.1);">
                                                    <i class="fas fa-headset text-primary fa-lg"></i>
                                                </div>
                                            </div>
                                            <h6>{{ __('admin/ai-staff.training_import_from_tickets') }}</h6>
                                            <p class="text-muted small">{{ __('admin/ai-staff.training_import_tickets_desc') }}</p>
                                            <button type="button" class="btn btn-outline-primary btn-sm" id="importFromTicketsBtn">
                                                <i class="fas fa-download me-1"></i>{{ __('admin/ai-staff.training_buttons_import') }}
                                            </button>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="card border-0 shadow-sm h-100">
                                        <div class="card-body text-center">
                                            <div class="mb-3">
                                                <div class="rounded-circle d-inline-flex align-items-center justify-content-center"
                                                     style="width: 56px; height: 56px; background-color: rgba(var(--bs-success-rgb), 0.1);">
                                                    <i class="fas fa-book text-success fa-lg"></i>
                                                </div>
                                            </div>
                                            <h6>{{ __('admin/ai-staff.training_import_from_kb') }}</h6>
                                            <p class="text-muted small">{{ __('admin/ai-staff.training_import_kb_desc') }}</p>
                                            <button type="button" class="btn btn-outline-success btn-sm" id="importFromKbBtn">
                                                <i class="fas fa-download me-1"></i>{{ __('admin/ai-staff.training_buttons_import') }}
                                            </button>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="card border-0 shadow-sm h-100">
                                        <div class="card-body text-center">
                                            <div class="mb-3">
                                                <div class="rounded-circle d-inline-flex align-items-center justify-content-center"
                                                     style="width: 56px; height: 56px; background-color: rgba(var(--bs-warning-rgb), 0.1);">
                                                    <i class="fas fa-comment-dots text-warning fa-lg"></i>
                                                </div>
                                            </div>
                                            <h6>{{ __('admin/ai-staff.training_import_from_replies') }}</h6>
                                            <p class="text-muted small">{{ __('admin/ai-staff.training_import_replies_desc') }}</p>
                                            <button type="button" class="btn btn-outline-warning btn-sm" id="importFromRepliesBtn">
                                                <i class="fas fa-download me-1"></i>{{ __('admin/ai-staff.training_buttons_import') }}
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Fine-Tune Tab -->
                        <div class="tab-pane fade" id="finetune-pane" role="tabpanel" aria-labelledby="finetune-pane-tab">
                            <div class="d-flex justify-content-between align-items-center mb-3">
                                <div>
                                    <p class="text-muted mb-0">{{ __('admin/ai-staff.training_finetune_description') }}</p>
                                </div>
                                <button type="button" class="btn btn-primary btn-sm" id="startFineTuneBtn">
                                    <i class="fas fa-play me-1"></i>{{ __('admin/ai-staff.training_buttons_start_finetune') }}
                                </button>
                            </div>

                            <!-- Fine-Tune Job History -->
                            <h6 class="mt-4 mb-3">{{ __('admin/ai-staff.training_finetune_history') }}</h6>
                            <div class="table-responsive">
                                <table id="fineTuneJobsTable" class="table table-hover align-middle mb-0">
                                    <thead>
                                        <tr>
                                            <th>{{ __('admin/ai-staff.training_finetune_job_id') }}</th>
                                            <th>{{ __('admin/ai-staff.training_finetune_model') }}</th>
                                            <th>{{ __('admin/ai-staff.training_finetune_status') }}</th>
                                            <th>{{ __('admin/ai-staff.training_finetune_started_at') }}</th>
                                            <th>{{ __('admin/ai-staff.training_finetune_completed_at') }}</th>
                                        </tr>
                                    </thead>
                                    <tbody id="fineTuneJobsBody">
                                        @if(isset($fineTuneJobs) && $fineTuneJobs->count())
                                            @foreach($fineTuneJobs as $job)
                                                <tr>
                                                    <td><code>{{ $job->job_id }}</code></td>
                                                    <td>{{ $job->model }}</td>
                                                    <td>
                                                        @if($job->status === 'succeeded')
                                                            <span class="badge bg-success">{{ __('admin/ai-staff.training_finetune_status_succeeded') }}</span>
                                                        @elseif($job->status === 'running')
                                                            <span class="badge bg-primary">{{ __('admin/ai-staff.training_finetune_status_running') }}</span>
                                                        @elseif($job->status === 'failed')
                                                            <span class="badge bg-danger">{{ __('admin/ai-staff.training_finetune_status_failed') }}</span>
                                                        @else
                                                            <span class="badge bg-secondary">{{ $job->status }}</span>
                                                        @endif
                                                    </td>
                                                    <td>{{ $job->created_at->format('Y-m-d H:i') }}</td>
                                                    <td>{{ $job->completed_at ? $job->completed_at->format('Y-m-d H:i') : '-' }}</td>
                                                </tr>
                                            @endforeach
                                        @else
                                            <tr>
                                                <td colspan="5" class="text-center text-muted py-4">
                                                    {{ __('admin/ai-staff.training_finetune_no_jobs') }}
                                                </td>
                                            </tr>
                                        @endif
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </x-admin.card>
            </div>

            <!-- Actions Column -->
            <div class="col-lg-4">
                <x-admin.card :title="__('admin/ai-staff.card_actions')">
                    <div class="d-grid gap-2">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save me-2"></i>{{ __('admin/ai-staff.buttons_save') }}
                        </button>
                        <a href="{{ route('admin.ai-staff.training.index') }}" class="btn btn-outline-secondary">
                            <i class="fas fa-times me-2"></i>{{ __('admin/ai-staff.buttons_cancel') }}
                        </a>
                    </div>
                </x-admin.card>

                <!-- Dataset Info Card -->
                <x-admin.card :title="__('admin/ai-staff.training_card_info')" class="mt-3">
                    <ul class="list-unstyled mb-0">
                        <li class="d-flex justify-content-between py-2 border-bottom">
                            <span class="text-muted">{{ __('admin/ai-staff.training_fields_items_count') }}</span>
                            <strong>{{ $dataset->items_count ?? 0 }}</strong>
                        </li>
                        <li class="d-flex justify-content-between py-2 border-bottom">
                            <span class="text-muted">{{ __('admin/ai-staff.training_fields_created_by') }}</span>
                            <strong>{{ $dataset->creator->name ?? '-' }}</strong>
                        </li>
                        <li class="d-flex justify-content-between py-2">
                            <span class="text-muted">{{ __('admin/ai-staff.training_fields_created_at') }}</span>
                            <strong>{{ $dataset->created_at->format('Y-m-d H:i') }}</strong>
                        </li>
                    </ul>
                </x-admin.card>
            </div>
        </div>
    </form>
</div>
@endsection

@push('scripts')
<script>
    window.trainingConfig = {
        datasetId: {{ $dataset->id }},
        routes: {
            update: '{{ route("admin.ai-staff.training.update", $dataset) }}',
            itemsRecords: '{{ route("api.admin.ai-training.items.records", $dataset) }}',
            itemStore: '{{ route("api.admin.ai-training.items.store", $dataset) }}',
            itemDestroy: '{{ route("api.admin.ai-training.items.update", ":id") }}',
            import: '{{ route("api.admin.ai-training.import", $dataset) }}',
            startFineTune: '{{ route("api.admin.ai-training.fine-tune") }}'
        },
        translations: {
            validation_error: '{{ __("admin/ai-staff.validation_error") }}',
            updated_success: '{{ __("admin/ai-staff.training_updated_success") }}',
            add_item_title: '{{ __("admin/ai-staff.training_add_item_title") }}',
            user_message_label: '{{ __("admin/ai-staff.training_items_user_message") }}',
            assistant_message_label: '{{ __("admin/ai-staff.training_items_assistant_message") }}',
            item_added: '{{ __("admin/ai-staff.training_item_added") }}',
            item_deleted: '{{ __("admin/ai-staff.training_item_deleted") }}',
            confirm_delete_item: '{{ __("admin/ai-staff.training_confirm_delete_item") }}',
            approved_toggled: '{{ __("admin/ai-staff.training_approved_toggled") }}',
            import_started: '{{ __("admin/ai-staff.training_import_started") }}',
            import_completed: '{{ __("admin/ai-staff.training_import_completed") }}',
            finetune_started: '{{ __("admin/ai-staff.training_finetune_started") }}',
            finetune_confirm: '{{ __("admin/ai-staff.training_finetune_confirm") }}',
            source_ticket: '{{ __("admin/ai-staff.training_source_ticket") }}',
            source_kb: '{{ __("admin/ai-staff.training_source_kb") }}',
            source_reply: '{{ __("admin/ai-staff.training_source_reply") }}',
            source_manual: '{{ __("admin/ai-staff.training_source_manual") }}',
            approved_yes: '{{ __("admin/ai-staff.training_approved_yes") }}',
            approved_no: '{{ __("admin/ai-staff.training_approved_no") }}'
        }
    };
</script>
@vite('resources/js/admin/ai-staff/training/edit.js')
@endpush
