@extends('layouts.admin')

@section('title', 'Profit by Product Report')

@section('content')
<x-admin.report-page title="Profit by Product Report" subtitle="Detailed product-level revenue, COGS, and margin.">
    <x-slot:actions>
        <a href="{{ route('admin.reports.profit.export-csv', request()->query()) }}" class="ci-btn ci-btn--ghost">
            <i class="bi bi-download"></i> Export CSV
        </a>
        <a href="{{ route('admin.reports.profit') }}" class="ci-btn ci-btn--ghost">
            <i class="bi bi-arrow-left"></i> Back to Overview
        </a>
    </x-slot:actions>

    <form method="GET" class="ci-filters ci-filters--report">
        @include('admin.reports.partials.location-filter', ['ci' => true])
        <div class="ci-filters__field">
            <label for="start_date">Start Date</label>
            <input type="date" name="start_date" id="start_date" value="{{ $filters['start_date'] }}">
        </div>
        <div class="ci-filters__field">
            <label for="end_date">End Date</label>
            <input type="date" name="end_date" id="end_date" value="{{ $filters['end_date'] }}">
        </div>
        <div class="ci-filters__field">
            <label for="category_id">Category</label>
            <select name="category_id" id="category_id">
                <option value="">All Categories</option>
                @foreach($categories as $category)
                    <option value="{{ $category->id }}" {{ $filters['category_id'] == $category->id ? 'selected' : '' }}>
                        {{ $category->name }}
                    </option>
                @endforeach
            </select>
        </div>
        <div class="ci-filters__actions">
            <button type="submit" class="ci-btn ci-btn--primary">Filter</button>
        </div>
        <div class="ci-filters__count">
            Showing <strong>{{ $profitByProduct->count() }}</strong> products
        </div>
    </form>

    <div class="ci-table-card">
        <div class="ci-table-wrap">
            <table class="ci-table">
                <thead>
                    <tr>
                        <th>Product Name</th>
                        <th>SKU</th>
                        <th class="ci-end">Units Sold</th>
                        <th class="ci-end">Total Revenue</th>
                        <th class="ci-end">Total COGS</th>
                        <th class="ci-end">Gross Profit</th>
                        <th class="ci-end">Margin %</th>
                        <th class="ci-end">Avg. Unit Profit</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($profitByProduct as $product)
                    <tr>
                        <td>
                            <strong>{{ $product['product_name'] }}</strong>
                        </td>
                        <td>
                            <code class="ci-code">{{ $product['sku'] }}</code>
                        </td>
                        <td class="ci-end">
                            <span class="ci-pill ci-pill--count">{{ $product['units_sold'] }}</span>
                        </td>
                        <td class="ci-end ci-money">
                            ₹{{ number_format($product['total_revenue'], 2) }}
                        </td>
                        <td class="ci-end ci-money ci-money--muted">
                            ₹{{ number_format($product['total_cogs'], 2) }}
                        </td>
                        <td class="ci-end ci-money {{ $product['gross_profit'] >= 0 ? '' : 'ci-stat__value--danger' }}" @if($product['gross_profit'] >= 0) style="color:var(--ci-success);" @endif>
                            ₹{{ number_format($product['gross_profit'], 2) }}
                        </td>
                        <td class="ci-end">
                            <span class="ci-pill {{ $product['margin_percent'] >= 0 ? 'ci-pill--success' : 'ci-pill--danger' }}">
                                {{ number_format($product['margin_percent'], 1) }}%
                            </span>
                        </td>
                        <td class="ci-end ci-money">
                            @if($product['units_sold'] > 0)
                                <span class="{{ ($product['gross_profit'] / $product['units_sold']) >= 0 ? '' : 'ci-stat__value--danger' }}" @if(($product['gross_profit'] / $product['units_sold']) >= 0) style="color:var(--ci-success);" @endif>
                                    ₹{{ number_format($product['gross_profit'] / $product['units_sold'], 2) }}
                                </span>
                            @else
                                <span class="ci-money--muted">-</span>
                            @endif
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="8">
                            <div class="ci-empty">
                                <i class="bi bi-graph-down"></i>
                                <h3>No Profit Data Available</h3>
                                <p>No products with profit data found for the selected criteria.</p>
                                <div style="display:flex;flex-wrap:wrap;gap:0.6rem;justify-content:center;">
                                    <a href="{{ route('admin.products.index') }}" class="ci-btn ci-btn--primary">
                                        <i class="bi bi-plus"></i> Add Purchase Prices
                                    </a>
                                    <a href="{{ route('admin.reports.missing-purchase-price') }}" class="ci-btn ci-btn--ghost">
                                        <i class="bi bi-exclamation-triangle"></i> View Missing Purchase Prices
                                    </a>
                                </div>
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
                @if($profitByProduct->count() > 0)
                <tfoot>
                    <tr>
                        <td colspan="2">TOTALS</td>
                        <td class="ci-end ci-num">{{ $profitByProduct->sum('units_sold') }}</td>
                        <td class="ci-end ci-money">₹{{ number_format($profitByProduct->sum('total_revenue'), 2) }}</td>
                        <td class="ci-end ci-money">₹{{ number_format($profitByProduct->sum('total_cogs'), 2) }}</td>
                        <td class="ci-end ci-money {{ $profitByProduct->sum('gross_profit') >= 0 ? '' : 'ci-stat__value--danger' }}" @if($profitByProduct->sum('gross_profit') >= 0) style="color:var(--ci-success);" @endif>
                            ₹{{ number_format($profitByProduct->sum('gross_profit'), 2) }}
                        </td>
                        <td class="ci-end">
                            @php
                                $totalRevenue = $profitByProduct->sum('total_revenue');
                                $totalProfit = $profitByProduct->sum('gross_profit');
                                $overallMargin = $totalRevenue > 0 ? ($totalProfit / $totalRevenue) * 100 : 0;
                            @endphp
                            <span class="ci-pill {{ $overallMargin >= 0 ? 'ci-pill--success' : 'ci-pill--danger' }}">
                                {{ number_format($overallMargin, 1) }}%
                            </span>
                        </td>
                        <td class="ci-end ci-money">
                            @php
                                $totalUnits = $profitByProduct->sum('units_sold');
                                $avgUnitProfit = $totalUnits > 0 ? $totalProfit / $totalUnits : 0;
                            @endphp
                            <span class="{{ $avgUnitProfit >= 0 ? '' : 'ci-stat__value--danger' }}" @if($avgUnitProfit >= 0) style="color:var(--ci-success);" @endif>
                                ₹{{ number_format($avgUnitProfit, 2) }}
                            </span>
                        </td>
                    </tr>
                </tfoot>
                @endif
            </table>
        </div>
    </div>
</x-admin.report-page>
@endsection
