@extends('layouts.admin')

@section('title', 'Import Products')

@section('content')
<div class="admin-page-header">
    <h1>Import Products from CSV</h1>
    <div class="d-flex gap-2">
        @can('products.view')
        <a href="{{ route('admin.products.import.reports') }}" class="admin-btn-primary">
            <i class="bi bi-list-ul"></i> View Import Reports
        </a>
        <a href="{{ route('admin.products.index') }}" class="admin-btn-primary">Back to Products</a>
        @endcan
    </div>
</div>

@if(session('missing_categories'))
    <div class="alert alert-warning alert-dismissible fade show" role="alert">
        <i class="bi bi-exclamation-triangle"></i> <strong>Warning:</strong>
        <p class="mb-2">The following categories were referenced in your import but don't exist:</p>
        <ul class="mb-0">
            @foreach(session('missing_categories') as $category)
                <li>{{ $category }}</li>
            @endforeach
        </ul>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
@endif

<div class="admin-card">
    <div class="card-body">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h5 class="mb-0" style="color: var(--admin-primary);">
                <i class="bi bi-upload"></i> Import Products from CSV
            </h5>
            <div class="d-flex gap-2">
                <a href="{{ route('admin.products.download-categories') }}" class="admin-btn-primary admin-action-btn-sm" style="background: #10b981;">
                    <i class="bi bi-list-check"></i> Download Categories CSV
                </a>
                <a href="{{ route('admin.products.download-sample') }}" class="admin-btn-primary admin-action-btn-sm" style="background: #10b981;">
                    <i class="bi bi-download"></i> Download Sample CSV
                </a>
            </div>
        </div>
        <div class="alert alert-info mb-3">
            <i class="bi bi-info-circle"></i> <strong>CSV Format:</strong> The file must include the <code>product_type</code> column with values: <code>single</code> or <code>variant</code>.
            <br><br>
            <strong>Required columns for all product types:</strong>
            <ul class="mb-2">
                <li><code>product_type</code> - Must be: single or variant</li>
                <li><code>name</code> - Product name</li>
                <li><code>category_name</code> - Must match an existing category</li>
                <li><code>price</code> - Product price (required for single products)</li>
            </ul>
            
            <strong>Optional columns:</strong> <code>sku</code> (auto-generated if empty), <code>tags</code>, <code>mrp</code>, <code>purchase_price</code>, <code>wholesale_price</code>, <code>opening_stock</code> (or <code>stock</code>), <code>barcode</code>, <code>unit_name</code>, <code>short_desc</code>, <code>long_desc</code>, <code>is_active</code>, <code>is_featured</code>, <code>is_prebooking</code>, <code>image_name</code>
            <br><br>
            
            <strong>Product Type Specifics:</strong>
            <ul class="mb-2">
                <li><strong>Single Products:</strong> One row = one product. Price and stock are required. SKU is auto-generated if not provided.</li>
                <li><strong>Variant Products:</strong> Multiple rows with the same product name. Each row creates one variant. Required columns:
                    <ul>
                        <li><code>variant_attributes</code> - Comma-separated attribute values (e.g., "Large,Red" or "Gold,Small")</li>
                        <li><code>variant_price</code> - Variant-specific price (optional, uses product price if empty)</li>
                        <li><code>variant_opening_stock</code> - Stock for this variant</li>
                    </ul>
                    Optional variant columns: <code>variant_sku</code>, <code>variant_mrp</code>, <code>variant_barcode</code>, <code>variant_image_name</code>
                </li>
            </ul>
            
            <strong>Image processing:</strong>
            <ul class="mb-2">
                <li><strong>Folder:</strong> Place product images in <code>public/uploads/imports/products/</code>. In the CSV, set <code>image_name</code> (main/parent image) and/or <code>variant_image_name</code> (per variant row) to the file name (e.g. <code>product_image.jpg</code>).</li>
                <li><strong>Max file size:</strong> 5MB. <strong>Allowed formats:</strong> JPG, PNG, WebP.</li>
                <li>Imported images are processed like manual uploads: multiple sizes (thumb 150px, small 300px, medium 600px, large 1200px) are generated and stored. Images are resized and optimized (WebP with JPEG fallback).</li>
                <li><strong>Where each size is used:</strong> Listing pages use medium; product detail uses large; cart/checkout use small; admin/POS use thumb.</li>
            </ul>

            <strong>Important Notes:</strong>
            <ul class="mb-2">
                <li><strong>Category name</strong> must match an existing category exactly.</li>
                <li><strong>Unit name</strong> is optional (must match existing unit: set, pc, pair, etc.).</li>
                <li><strong>Variant Attributes:</strong> Comma-separated values (e.g., "Large,Red") will be matched to existing AttributeValue records. Values are case-insensitive.</li>
                <li><strong>Variant images:</strong> Use <code>variant_image_name</code> for variant rows; same folder, 5MB max, JPG/PNG/WebP. Same processing and size rules apply.</li>
                <li>For <code>is_active</code>, <code>is_featured</code>, and <code>is_prebooking</code>, use: 1, true, yes, or active/featured/prebooking.</li>
            </ul>
            
            <strong>Example:</strong> For variant products, create multiple rows with the same product name. Each row should have different <code>variant_attributes</code> values. Download the sample CSV to see examples of all three product types.
            <br><br>
            <strong>Background import:</strong> The file is saved and processed in the background. After upload you are redirected to the Import Reports page. To process jobs, run <code>php artisan queue:work</code> (or set up a supervisor/cron). If the queue is not running, imports will stay in "Pending" until the worker runs.
        </div>
        <form action="{{ route('admin.products.import') }}" method="POST" enctype="multipart/form-data" class="row g-3 align-items-center">
            @csrf
            <div class="col-md-8">
                <label class="admin-form-label">Select CSV File</label>
                <input type="file" 
                       name="import_file" 
                       class="admin-form-control form-control @error('import_file') is-invalid @enderror" 
                       accept=".csv,.txt"
                       required>
                @error('import_file')
                    <div class="invalid-feedback">{{ $message }}</div>
                @enderror
                <small class="text-muted">Maximum file size: 5MB. Download sample file to see the correct format.</small>
            </div>
            <div class="col-md-4">
                <button type="submit" class="admin-btn-primary w-100">
                    <i class="bi bi-upload"></i> Import Products
                </button>
            </div>
        </form>
    </div>
</div>
@endsection
