@extends('layouts.admin')

@section('title', 'Profit Report')

@section('content')
<x-admin.report-page title="Profit Report" subtitle="Revenue, COGS, and margin across products for the selected period.">
    <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.by-product', request()->query()) }}" class="ci-btn ci-btn--ghost">
            By Product
        </a>
        <a href="{{ route('admin.reports.profit.by-order', request()->query()) }}" class="ci-btn ci-btn--ghost">
            By Order
        </a>
        <a href="{{ route('admin.reports.missing-purchase-price') }}" class="ci-btn ci-btn--ghost">
            Missing Prices
        </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__field">
            <label for="order_status">Order Status</label>
            <select name="order_status" id="order_status">
                <option value="">All Status</option>
                <option value="delivered" {{ $filters['order_status'] == 'delivered' ? 'selected' : '' }}>Delivered</option>
                <option value="shipped" {{ $filters['order_status'] == 'shipped' ? 'selected' : '' }}>Shipped</option>
                <option value="confirmed" {{ $filters['order_status'] == 'confirmed' ? 'selected' : '' }}>Confirmed</option>
            </select>
        </div>
        <div class="ci-filters__actions">
            <button type="submit" class="ci-btn ci-btn--primary">Filter</button>
        </div>
    </form>

    <div class="ci-stats">
        <div class="ci-stat">
            <div>
                <p class="ci-stat__label">Total Revenue</p>
                <p class="ci-stat__value ci-money">₹{{ number_format($profitMetrics['total_revenue'], 2) }}</p>
            </div>
            <span class="ci-stat__icon"><i class="bi bi-currency-rupee"></i></span>
        </div>
        <div class="ci-stat">
            <div>
                <p class="ci-stat__label">Total COGS</p>
                <p class="ci-stat__value ci-money">₹{{ number_format($profitMetrics['total_cogs'], 2) }}</p>
            </div>
            <span class="ci-stat__icon"><i class="bi bi-box"></i></span>
        </div>
        <div class="ci-stat">
            <div>
                <p class="ci-stat__label">Gross Profit</p>
                <p class="ci-stat__value ci-money">₹{{ number_format($profitMetrics['total_profit'], 2) }}</p>
            </div>
            <span class="ci-stat__icon"><i class="bi bi-graph-up"></i></span>
        </div>
        <div class="ci-stat">
            <div>
                <p class="ci-stat__label">Profit Margin</p>
                <p class="ci-stat__value">{{ number_format($profitMetrics['profit_margin_percent'], 2) }}%</p>
            </div>
            <span class="ci-stat__icon"><i class="bi bi-percent"></i></span>
        </div>
    </div>

    @if($profitMetrics['profit_data_coverage_percent'] < 100)
    <div class="ci-section">
        <div class="ci-section__body">
            <p class="mb-0">
                <i class="bi bi-exclamation-triangle text-warning"></i>
                <strong>Data Coverage:</strong> {{ $profitMetrics['profit_data_coverage_percent'] }}% of items have purchase price data
                ({{ $profitMetrics['items_with_profit_data'] }} out of {{ $profitMetrics['total_items'] }} items).
                <a href="{{ route('admin.reports.missing-purchase-price') }}" class="ci-link">View products missing purchase prices</a>
            </p>
        </div>
    </div>
    @endif

    <div class="ci-grid-2">
        <div class="ci-section" style="margin-bottom:0;">
            <div class="ci-section__head">
                <h5><i class="bi bi-trophy"></i> Top Profitable Products</h5>
            </div>
            <div class="ci-section__body--flush">
                @if($topProducts['most_profitable']->count() > 0)
                    <div class="ci-table-wrap">
                        <table class="ci-table">
                            <thead>
                                <tr>
                                    <th>Product</th>
                                    <th class="ci-end">Profit</th>
                                    <th class="ci-end">Margin</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($topProducts['most_profitable']->take(5) as $product)
                                <tr>
                                    <td>
                                        <strong>{{ $product['product_name'] }}</strong><br>
                                        <small class="text-muted">{{ $product['sku'] }}</small>
                                    </td>
                                    <td class="ci-end ci-money" style="color:var(--ci-success);">₹{{ number_format($product['gross_profit'], 2) }}</td>
                                    <td class="ci-end">
                                        <span class="ci-pill ci-pill--success">{{ number_format($product['margin_percent'], 1) }}%</span>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <div class="ci-empty">
                        <i class="bi bi-graph-down"></i>
                        <p>No profit data available. Add purchase prices to products to see profit analysis.</p>
                    </div>
                @endif
            </div>
        </div>

        <div class="ci-section" style="margin-bottom:0;">
            <div class="ci-section__head">
                <h5><i class="bi bi-exclamation-triangle text-warning"></i> Least Profitable Products</h5>
            </div>
            <div class="ci-section__body--flush">
                @if($topProducts['least_profitable']->count() > 0)
                    <div class="ci-table-wrap">
                        <table class="ci-table">
                            <thead>
                                <tr>
                                    <th>Product</th>
                                    <th class="ci-end">Profit</th>
                                    <th class="ci-end">Margin</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($topProducts['least_profitable']->take(5) as $product)
                                <tr>
                                    <td>
                                        <strong>{{ $product['product_name'] }}</strong><br>
                                        <small class="text-muted">{{ $product['sku'] }}</small>
                                    </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>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <div class="ci-empty">
                        <i class="bi bi-graph-down"></i>
                        <p>No profit data available.</p>
                    </div>
                @endif
            </div>
        </div>
    </div>

    <div class="ci-section">
        <div class="ci-section__head">
            <h5><i class="bi bi-table"></i> Profit by Product</h5>
            <a href="{{ route('admin.reports.profit.by-product', request()->query()) }}" class="ci-btn ci-btn--ghost">
                View Detailed Report
            </a>
        </div>
        <div class="ci-section__body--flush">
            @if($profitByProduct->count() > 0)
                <div class="ci-table-card" style="border:0;border-radius:0;box-shadow:none;">
                    <div class="ci-table-wrap">
                        <table class="ci-table">
                            <thead>
                                <tr>
                                    <th>Product</th>
                                    <th>SKU</th>
                                    <th class="ci-end">Units Sold</th>
                                    <th class="ci-end">Revenue</th>
                                    <th class="ci-end">COGS</th>
                                    <th class="ci-end">Gross Profit</th>
                                    <th class="ci-end">Margin %</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($profitByProduct->take(10) as $product)
                                <tr>
                                    <td><strong>{{ $product['product_name'] }}</strong></td>
                                    <td><code class="ci-code">{{ $product['sku'] }}</code></td>
                                    <td class="ci-end ci-num">{{ $product['units_sold'] }}</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>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>

                @if($profitByProduct->count() > 10)
                    <div class="ci-section__body" style="text-align:center;">
                        <a href="{{ route('admin.reports.profit.by-product', request()->query()) }}" class="ci-btn ci-btn--ghost">
                            View All {{ $profitByProduct->count() }} Products
                        </a>
                    </div>
                @endif
            @else
                <div class="ci-empty">
                    <i class="bi bi-graph-down"></i>
                    <h3>No Profit Data Available</h3>
                    <p>Add purchase prices to your products to see profit analysis.</p>
                    <a href="{{ route('admin.products.index') }}" class="ci-btn ci-btn--primary">
                        <i class="bi bi-plus"></i> Add Purchase Prices
                    </a>
                </div>
            @endif
        </div>
    </div>
</x-admin.report-page>
@endsection
