@extends('layouts.app')
@section('title', 'ProductCompat — Settings')

@section('content')
<section class="content-header"><h1>@lang('productcompat::lang.product_compatibility') <small>Settings</small></h1></section>

<section class="content">
    @includeIf('productcompat::partials.status')

    @if(!empty($limitAt))
        <div class="alert alert-warning"><i class="fa fa-exclamation-triangle"></i>
            Icecat limit was reached ({{ $limitAt }}). {{ $limitMsg }} — change the username below and save to clear this.
        </div>
    @endif

    <form method="POST" action="{{ route('productcompat.settings.save') }}">
        @csrf
        <div class="row">
            <div class="col-md-6">
                <div class="box box-primary">
                    <div class="box-header with-border"><h3 class="box-title">Icecat (enrichment by part number)</h3></div>
                    <div class="box-body">
                        <div class="form-group">
                            <label>Icecat username</label>
                            <input type="text" name="icecat_username" class="form-control" value="{{ $s['icecat_username'] }}">
                            <small class="text-muted">Free open user is <code>openIcecat-live</code>; your own account avoids rate limits.</small>
                        </div>
                        <div class="form-group">
                            <label>Language</label>
                            <input type="text" name="icecat_lang" class="form-control" value="{{ $s['icecat_lang'] }}" style="max-width:100px;">
                        </div>
                    </div>
                </div>

                <div class="box box-success">
                    <div class="box-header with-border"><h3 class="box-title">Free search (no part number) — Self-hosted SearXNG</h3></div>
                    <div class="box-body">
                        <div class="form-group">
                            <label>SearXNG instance URL</label>
                            <input type="text" name="searxng_url" class="form-control" value="{{ $s['searxng_url'] }}" placeholder="http://your-host:8080">
                            <small class="text-muted">Free + no daily cap. Your instance must allow <code>format=json</code> in settings.yml. Used for part-numberless products.</small>
                        </div>
                        <div class="form-group">
                            <label>Delay between SearXNG requests (ms)</label>
                            <input type="number" name="searxng_delay_ms" class="form-control" value="{{ $s['searxng_delay_ms'] ?: 0 }}" style="max-width:160px;" min="0" max="10000" step="100">
                            <small class="text-muted">Waits this long before each SearXNG call so a self-hosted / ngrok-tunnelled instance isn't hit back-to-back. 0 = off; try 500–1000 for a free ngrok tunnel.</small>
                        </div>
                    </div>
                </div>

                <div class="box box-info">
                    <div class="box-header with-border"><h3 class="box-title">Other search providers (optional)</h3></div>
                    <div class="box-body">
                        <div class="form-group">
                            <label>Google API key</label>
                            <input type="text" name="google_cse_key" class="form-control" value="{{ $s['google_cse_key'] }}">
                        </div>
                        <div class="form-group">
                            <label>Search engine id (cx)</label>
                            <input type="text" name="google_cse_cx" class="form-control" value="{{ $s['google_cse_cx'] }}">
                            <small class="text-muted">Free: 100 queries/day. Get both at programmablesearchengine.google.com + a Custom Search API key.</small>
                        </div>
                        <div class="form-group">
                            <label>SerpApi key (optional, paid)</label>
                            <input type="text" name="serp_key" class="form-control" value="{{ $s['serp_key'] }}">
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-6">
                <div class="box box-warning">
                    <div class="box-header with-border"><h3 class="box-title">Automation (scheduled batches)</h3></div>
                    <div class="box-body">
                        <div class="form-group">
                            <label><input type="checkbox" name="auto_enrich_enabled" value="1" @if(\Modules\ProductCompat\Entities\Services\Settings::bool('auto_enrich_enabled')) checked @endif> Enable scheduled auto-enrichment</label>
                            <small class="text-muted" style="display:block;">Runs a batch automatically on the schedule below (resumable; stops itself when the catalog is done). Requires the server cron <code>php artisan schedule:run</code> — no Kernel.php edit needed.</small>
                        </div>
                        <div class="form-group">
                            <label>Frequency</label>
                            <select name="auto_enrich_frequency" class="form-control" style="max-width:220px;">
                                @foreach(['everyFifteenMinutes'=>'Every 15 min','everyThirtyMinutes'=>'Every 30 min','hourly'=>'Hourly','daily'=>'Daily'] as $k=>$v)
                                    <option value="{{ $k }}" @if($s['auto_enrich_frequency']==$k) selected @endif>{{ $v }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group">
                            <label>Target</label>
                            <select name="auto_enrich_target" class="form-control" style="max-width:260px;">
                                <option value="enrich_has" @if($s['auto_enrich_target']=='enrich_has') selected @endif>Products with a part number (Icecat)</option>
                                <option value="enrich_missing" @if($s['auto_enrich_target']=='enrich_missing') selected @endif>Products without a part number (search)</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>Batch size per run</label>
                            <input type="number" name="enrich_batch_size" class="form-control" value="{{ $s['enrich_batch_size'] }}" style="max-width:140px;" min="10" max="2000">
                        </div>
                    </div>
                </div>

                <div class="box box-primary">
                    <div class="box-header with-border"><h3 class="box-title">@lang('productcompat::lang.ai_enrichment')</h3></div>
                    <div class="box-body">
                        {{-- AiCore status --}}
                        @if(!empty($s['ai_available']))
                            <p class="text-success"><i class="fa fa-check-circle"></i> @lang('productcompat::lang.ai_provider_configured')</p>
                        @else
                            <div class="alert alert-warning" style="padding:8px 12px;">
                                <i class="fa fa-exclamation-triangle"></i> @lang('productcompat::lang.ai_provider_not_configured')
                                @if(\Illuminate\Support\Facades\Route::has('aicore.settings.index'))
                                    <a href="{{ route('aicore.settings.index') }}">@lang('productcompat::lang.ai_settings_link')</a>
                                @endif
                            </div>
                        @endif

                        <div class="form-group">
                            <label><input type="checkbox" name="ai_queue_enabled" value="1" @if($s['ai_queue_enabled']) checked @endif> @lang('productcompat::lang.ai_queue_enabled')</label>
                            <small class="text-muted" style="display:block;">@lang('productcompat::lang.ai_queue_enabled_help')</small>
                        </div>

                        {{-- Last scheduled-run indicator: confirms the server cron is firing --}}
                        @php $lastSched = $s['ai_queue_last_scheduled_at'] ?? null; $lastAny = $s['ai_queue_last_run_at'] ?? null; @endphp
                        <div class="form-group">
                            @if($lastSched)
                                <p class="text-success" style="margin:0;">
                                    <i class="fa fa-clock-o"></i> @lang('productcompat::lang.last_scheduled_run'):
                                    <strong>{{ \Illuminate\Support\Carbon::parse($lastSched)->diffForHumans() }}</strong>
                                    <small class="text-muted">({{ \Illuminate\Support\Carbon::parse($lastSched)->format('M j, H:i') }})</small>
                                </p>
                                @if(!empty($s['ai_queue_last_run_note']))
                                    <small class="text-muted">@lang('productcompat::lang.last_run'): {{ $s['ai_queue_last_run_note'] }} ({{ $s['ai_queue_last_run_trigger'] }})</small>
                                @endif
                            @elseif($lastAny)
                                <p class="text-muted" style="margin:0;">
                                    <i class="fa fa-clock-o"></i> @lang('productcompat::lang.last_run'):
                                    <strong>{{ \Illuminate\Support\Carbon::parse($lastAny)->diffForHumans() }}</strong>
                                    ({{ $s['ai_queue_last_run_trigger'] ?: 'manual' }}) —
                                    <span class="text-warning">@lang('productcompat::lang.no_scheduled_run_yet')</span>
                                </p>
                            @else
                                <p class="text-muted" style="margin:0;"><i class="fa fa-clock-o"></i> @lang('productcompat::lang.no_run_yet')</p>
                            @endif
                        </div>
                        <div class="form-group">
                            <label>@lang('productcompat::lang.ai_queue_frequency')</label>
                            <select name="ai_queue_frequency" class="form-control" style="max-width:220px;">
                                @foreach(['everyFifteenMinutes'=>'Every 15 min','everyThirtyMinutes'=>'Every 30 min','hourly'=>'Hourly','daily'=>'Daily'] as $k=>$v)
                                    <option value="{{ $k }}" @if($s['ai_queue_frequency']==$k) selected @endif>{{ $v }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-group">
                            <label>@lang('productcompat::lang.ai_queue_batch_size')</label>
                            <input type="number" name="ai_queue_batch_size" class="form-control" value="{{ $s['ai_queue_batch_size'] }}" style="max-width:140px;" min="1" max="200">
                        </div>
                        <div class="form-group">
                            <label>@lang('productcompat::lang.ai_queue_max_attempts')</label>
                            <input type="number" name="ai_queue_max_attempts" class="form-control" value="{{ $s['ai_queue_max_attempts'] }}" style="max-width:140px;" min="1" max="10">
                        </div>
                        <div class="form-group">
                            <label><input type="checkbox" name="ai_auto_queue_on_create" value="1" @if($s['ai_auto_queue_on_create']) checked @endif> @lang('productcompat::lang.ai_auto_queue_on_create')</label>
                        </div>
                        <div class="form-group">
                            <label><input type="checkbox" name="ai_extra_search" value="1" @if($s['ai_extra_search']) checked @endif> @lang('productcompat::lang.ai_extra_search')</label>
                        </div>
                        <div class="form-group">
                            <label><input type="checkbox" name="ai_require_searxng" value="1" @if($s['ai_require_searxng']) checked @endif> @lang('productcompat::lang.ai_require_searxng')</label>
                            <small class="text-muted" style="display:block;">@lang('productcompat::lang.ai_require_searxng_help')</small>
                        </div>
                        <div class="form-group">
                            <label><input type="checkbox" name="ai_queue_auto_seed" value="1" @if($s['ai_queue_auto_seed']) checked @endif> @lang('productcompat::lang.ai_queue_auto_seed')</label>
                            <small class="text-muted" style="display:block;">@lang('productcompat::lang.ai_queue_auto_seed_help')</small>
                            <label style="margin-top:6px;">@lang('productcompat::lang.ai_queue_auto_seed_cap')</label>
                            <input type="number" name="ai_queue_auto_seed_cap" class="form-control" value="{{ $s['ai_queue_auto_seed_cap'] }}" style="max-width:140px;" min="1" max="5000">
                        </div>
                        <div class="form-group">
                            <label>@lang('productcompat::lang.ai_image_max_kb')</label>
                            <input type="number" name="ai_image_max_kb" class="form-control" value="{{ $s['ai_image_max_kb'] }}" style="max-width:140px;" min="1" max="20480">
                        </div>
                        <div class="form-group">
                            <label><input type="checkbox" name="ai_provider_enabled" value="1" @if($s['ai_provider_enabled']) checked @endif> @lang('productcompat::lang.ai_provider_enabled')</label>
                            <small class="text-muted" style="display:block;">@lang('productcompat::lang.ai_provider_enabled_help')</small>
                        </div>
                        <div class="form-group" style="border-top:1px solid #eee; padding-top:10px;">
                            <label><input type="checkbox" name="ai_use_model_knowledge" value="1" @if($s['ai_use_model_knowledge']) checked @endif> <strong>@lang('productcompat::lang.ai_use_model_knowledge')</strong></label>
                            <small class="text-muted" style="display:block;">@lang('productcompat::lang.ai_use_model_knowledge_help')</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <button type="submit" class="btn btn-primary">@lang('productcompat::lang.save')</button>
        <a href="{{ route('productcompat.panel') }}" class="btn btn-default">Control Panel</a>
    </form>
</section>
@endsection
