@props([
    'header' => [],
    'stock_rows' => [],
    'purchase_lines' => [],
    'sales_lines' => [],
    'inventory_movements' => [],
    'stocktake_differences' => [],
    'product_operation_query_url' => null,
    'inventory_balance_url' => null,
    'inventory_transaction_url' => null,
])

@php
    $lineTableColumns = [
        'code' => __('dberp.fields.document_code'),
        'business_time' => __('dberp.fields.business_time'),
        'partner' => __('dberp.fields.counterparty'),
        'quantity' => __('dberp.fields.quantity'),
        'price' => __('dberp.fields.unit_price'),
        'status' => __('dberp.fields.status'),
    ];

    $stockTableColumns = [
        'spec_summary' => __('dberp.fields.spec_combo_summary'),
        'warehouse' => __('dberp.fields.warehouse'),
        'current_stock' => __('dberp.fields.current_stock_quantity'),
        'available_stock' => __('dberp.fields.available_quantity'),
        'safety_stock' => __('dberp.fields.safety_stock'),
        'latest_inbound' => __('dberp.fields.latest_inbound_date'),
        'latest_outbound' => __('dberp.fields.latest_outbound_date'),
    ];

    $relatedSections = [
        [
            'title' => __('dberp.sections.product_recent_purchases'),
            'rows' => $purchase_lines,
            'empty' => __('dberp.messages.product_overview_no_recent_purchases'),
        ],
        [
            'title' => __('dberp.sections.product_recent_sales'),
            'rows' => $sales_lines,
            'empty' => __('dberp.messages.product_overview_no_recent_sales'),
        ],
        [
            'title' => __('dberp.sections.product_recent_inventory_movements'),
            'rows' => $inventory_movements,
            'empty' => __('dberp.messages.product_overview_no_recent_inventory_movements'),
        ],
        [
            'title' => __('dberp.sections.product_recent_stocktake_differences'),
            'rows' => $stocktake_differences,
            'empty' => __('dberp.messages.product_overview_no_recent_stocktake_differences'),
        ],
    ];
@endphp

<div class="space-y-6">
    @if (count($header) > 0)
        <dl class="grid grid-cols-2 gap-x-4 gap-y-3 sm:grid-cols-4">
            @foreach ($header as $item)
                <div class="min-w-0">
                    <dt class="text-xs font-medium text-gray-500 dark:text-gray-400">
                        {{ $item['label'] }}
                    </dt>
                    <dd class="mt-0.5 text-sm font-semibold text-gray-950 dark:text-white break-words">
                        {{ $item['value'] }}
                    </dd>
                </div>
            @endforeach
        </dl>
    @endif

    <div class="flex flex-wrap gap-x-4 gap-y-1 text-xs">
        @if (filled($product_operation_query_url))
            <a href="{{ $product_operation_query_url }}" class="font-medium text-primary-600 hover:underline dark:text-primary-400">
                {{ __('dberp.actions.view_product_operation_query') }}
            </a>
        @endif
        @if (filled($inventory_balance_url))
            <a href="{{ $inventory_balance_url }}" class="font-medium text-primary-600 hover:underline dark:text-primary-400">
                {{ __('dberp.actions.view_inventory_balance') }}
            </a>
        @endif
        @if (filled($inventory_transaction_url))
            <a href="{{ $inventory_transaction_url }}" class="font-medium text-primary-600 hover:underline dark:text-primary-400">
                {{ __('dberp.actions.view_inventory_transactions') }}
            </a>
        @endif
    </div>

    <div class="rounded-lg border border-gray-200 dark:border-white/10 overflow-hidden">
        <div class="border-b border-gray-200 bg-gray-50 px-3 py-2 dark:border-white/10 dark:bg-white/5">
            <h4 class="text-xs font-semibold text-gray-700 dark:text-gray-200">
                {{ __('dberp.sections.product_sku_stock_summary') }}
            </h4>
        </div>

        @if (count($stock_rows) === 0)
            <p class="px-3 py-4 text-xs text-gray-500 dark:text-gray-400">
                {{ __('dberp.messages.product_overview_no_stock_records') }}
            </p>
        @else
            <div class="overflow-x-auto">
                <table class="w-full min-w-full divide-y divide-gray-200 text-xs dark:divide-white/10">
                    <thead class="bg-white dark:bg-gray-900">
                        <tr>
                            @foreach ($stockTableColumns as $columnLabel)
                                <th scope="col" class="px-2 py-2 text-left font-medium text-gray-500 dark:text-gray-400 whitespace-nowrap">
                                    {{ $columnLabel }}
                                </th>
                            @endforeach
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100 bg-white dark:divide-white/5 dark:bg-gray-900">
                        @foreach ($stock_rows as $row)
                            <tr>
                                @foreach (array_keys($stockTableColumns) as $columnKey)
                                    <td class="px-2 py-2 whitespace-nowrap text-gray-700 dark:text-gray-300">
                                        {{ $row[$columnKey] ?? '—' }}
                                    </td>
                                @endforeach
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @endif
    </div>

    <div class="grid grid-cols-1 gap-4 xl:grid-cols-2">
        @foreach ($relatedSections as $section)
            <div class="rounded-lg border border-gray-200 dark:border-white/10 overflow-hidden">
                <div class="border-b border-gray-200 bg-gray-50 px-3 py-2 dark:border-white/10 dark:bg-white/5">
                    <h4 class="text-xs font-semibold text-gray-700 dark:text-gray-200">
                        {{ $section['title'] }}
                    </h4>
                </div>

                @if (count($section['rows']) === 0)
                    <p class="px-3 py-4 text-xs text-gray-500 dark:text-gray-400">
                        {{ $section['empty'] }}
                    </p>
                @else
                    <div class="overflow-x-auto">
                        <table class="w-full min-w-full divide-y divide-gray-200 text-xs dark:divide-white/10">
                            <thead class="bg-white dark:bg-gray-900">
                                <tr>
                                    @foreach ($lineTableColumns as $columnLabel)
                                        <th scope="col" class="px-2 py-2 text-left font-medium text-gray-500 dark:text-gray-400 whitespace-nowrap">
                                            {{ $columnLabel }}
                                        </th>
                                    @endforeach
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-100 bg-white dark:divide-white/5 dark:bg-gray-900">
                                @foreach ($section['rows'] as $row)
                                    <tr>
                                        <td class="px-2 py-2 whitespace-nowrap font-medium text-gray-950 dark:text-white">
                                            @if (filled($row['url'] ?? null))
                                                <a href="{{ $row['url'] }}" class="text-primary-600 hover:underline dark:text-primary-400">
                                                    {{ $row['code'] }}
                                                </a>
                                            @else
                                                {{ $row['code'] }}
                                            @endif
                                        </td>
                                        <td class="px-2 py-2 whitespace-nowrap text-gray-700 dark:text-gray-300">
                                            {{ $row['business_time'] }}
                                        </td>
                                        <td class="px-2 py-2 whitespace-nowrap text-gray-700 dark:text-gray-300">
                                            {{ $row['partner'] }}
                                        </td>
                                        <td class="px-2 py-2 whitespace-nowrap text-gray-700 dark:text-gray-300">
                                            {{ $row['quantity'] }}
                                        </td>
                                        <td class="px-2 py-2 whitespace-nowrap text-gray-700 dark:text-gray-300">
                                            {{ $row['price'] }}
                                        </td>
                                        <td class="px-2 py-2 whitespace-nowrap text-gray-700 dark:text-gray-300">
                                            {{ $row['status'] }}
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @endif
            </div>
        @endforeach
    </div>
</div>
