@extends('layouts.app')
@section('title', 'Offline batch enrichment')

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

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

    <div class="row">
        {{-- Step 1: generate --}}
        <div class="col-md-6">
            <div class="box box-solid box-primary">
                <div class="box-header with-border"><h3 class="box-title">1 · Generate batches</h3></div>
                <div class="box-body">
                    <form method="POST" action="{{ route('productcompat.batches.generate') }}" class="form-inline"
                          onsubmit="return confirm('Regenerate batch files? This replaces any existing set.');">
                        @csrf
                        <label>Products per file</label>
                        <input type="number" name="size" value="300" min="1" max="5000" class="form-control input-sm" style="width:90px;">
                        <label class="checkbox-inline" style="margin-left:8px;"><input type="checkbox" name="missing" value="1"> only missing-data</label>
                        <button class="btn btn-primary btn-sm"><i class="fa fa-cogs"></i> Generate</button>
                    </form>
                    <p class="text-muted" style="margin:6px 0 0;">Splits the catalogue into JSON files (full product data + existing part numbers) for offline enrichment.</p>
                </div>
            </div>
        </div>

        {{-- Step 3: upload + import --}}
        <div class="col-md-6">
            <div class="box box-solid box-success">
                <div class="box-header with-border"><h3 class="box-title">3 · Upload enriched files &amp; import</h3></div>
                <div class="box-body">
                    <form method="POST" action="{{ route('productcompat.batches.import') }}" enctype="multipart/form-data">
                        @csrf
                        <input type="file" name="files[]" accept=".json" multiple class="form-control" style="margin-bottom:8px;">
                        <button class="btn btn-success btn-sm"><i class="fa fa-upload"></i> Upload &amp; import</button>
                    </form>
                    <p class="text-muted" style="margin:6px 0 0;">Select the enriched <code>.json</code> file(s) from your AI assistant. Everything imports review-first.</p>

                    <hr style="margin:10px 0;">
                    <form method="POST" action="{{ route('productcompat.batches.match') }}"
                          onsubmit="return confirm('Run cross-product compatibility matching now? Do this after all batches are imported.');">
                        @csrf
                        <button class="btn btn-primary btn-sm"><i class="fa fa-link"></i> 4 · Match compatibility</button>
                    </form>
                    <p class="text-muted" style="margin:6px 0 0;">Final step — links products that share a part number and resolves AI-suggested compatible products to real product IDs (review-first).</p>
                </div>
            </div>
        </div>
    </div>

    {{-- Step 2: the prompt --}}
    @if($prompt !== '')
        <div class="box box-default collapsed-box">
            <div class="box-header with-border">
                <h3 class="box-title">2 · Prompt for the AI assistant</h3>
                <div class="box-tools pull-right">
                    <button type="button" id="pc-copy-btn" class="btn btn-xs btn-default" onclick="pcCopyPrompt(this)"><i class="fa fa-copy"></i> Copy</button>
                    <button type="button" class="btn btn-xs btn-default" data-widget="collapse"><i class="fa fa-plus"></i></button>
                </div>
            </div>
            <div class="box-body">
                <p class="text-muted">Give this to Claude Code (or any AI assistant) together with each batch file.</p>
                <textarea id="pc-prompt" class="form-control" rows="10" readonly style="font-family:monospace;font-size:12px;">{{ $prompt }}</textarea>
            </div>
        </div>
    @endif

    {{-- Batch files list --}}
    <div class="box box-info">
        <div class="box-header with-border">
            <h3 class="box-title">Batch files ({{ count($batches) }})</h3>
            <div class="box-tools pull-right">
                @if(count($batches))
                    <a href="{{ route('productcompat.batches.download_all') }}" class="btn btn-xs btn-info"><i class="fa fa-file-archive-o"></i> Download all (zip)</a>
                @endif
                <form method="POST" action="{{ route('productcompat.batches.clear') }}" style="display:inline;" onsubmit="return confirm('Delete all batch + enriched files for this business?');">
                    @csrf <button class="btn btn-xs btn-default"><i class="fa fa-trash"></i> Clear</button>
                </form>
            </div>
        </div>
        <div class="box-body table-responsive">
            @if(count($batches))
                <table class="table table-condensed table-bordered">
                    <thead><tr><th>File</th><th style="width:100px;">Size</th><th style="width:120px;">Download</th></tr></thead>
                    <tbody>
                        @foreach($batches as $b)
                            <tr>
                                <td>{{ $b['name'] }}</td>
                                <td>{{ $b['size'] }}</td>
                                <td><a href="{{ route('productcompat.batches.download', ['file' => $b['name']]) }}" class="btn btn-xs btn-default"><i class="fa fa-download"></i> Download</a></td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
                @if(count($enriched))
                    <p class="text-muted"><i class="fa fa-check text-green"></i> {{ count($enriched) }} enriched file(s) already uploaded in this set.</p>
                @endif
            @else
                <p class="text-muted">No batch files yet — generate them above.</p>
            @endif
        </div>
    </div>
</section>
@endsection

@section('javascript')
<script>
function pcCopyPrompt(btn) {
    var t = document.getElementById('pc-prompt');
    if (!t) return;
    var text = t.value;
    var done = function () {
        if (!btn) return;
        var html = btn.innerHTML;
        btn.innerHTML = '<i class="fa fa-check"></i> Copied!';
        setTimeout(function () { btn.innerHTML = html; }, 1500);
    };
    // Clipboard API works even while the box is collapsed (textarea hidden).
    if (navigator.clipboard && navigator.clipboard.writeText) {
        navigator.clipboard.writeText(text).then(done).catch(function () { pcCopyFallback(t, done); });
    } else {
        pcCopyFallback(t, done);
    }
}
function pcCopyFallback(t, done) {
    var tmp = document.createElement('textarea');
    tmp.value = t.value;
    document.body.appendChild(tmp);
    tmp.select();
    try { document.execCommand('copy'); done(); } catch (e) {}
    document.body.removeChild(tmp);
}
</script>
@endsection
