@extends('layouts.admin')

@section('title', 'Profit by Product Report')

@section('content')
<div class="admin-page-header">
    <h1>Profit by Product Report</h1>
    <div class="admin-page-actions">
        <a href="{{ route('admin.reports.profit.export-csv', request()->query()) }}" class="admin-btn-primary" style="background: #10b981;">
            <i class="bi bi-download"></i> Export CSV
        </a>
        <a href="{{ route('admin.reports.profit') }}" class="admin-btn-primary">
            <i class="bi bi-arrow-left"></i> Back to Overview
        </a>
    </div>
</div>

<!-- Filters -->
<div class="admin-card mb-4">
    <div class="card-body">
        <form method="GET" class="row g-3">
            <div class="col-md-3">
                <label class="admin-form-label">Start Date</label>
                <input type="date" name="start_date" class="admin-form-control form-control" value="{{ $filters['start_date'] }}">
            </div>
            <div class="col-md-3">
                <label class="admin-form-label">End Date</label>
                <input type="date" name="end_date" class="admin-form-control form-control" value="{{ $filters['end_date'] }}">
            </div>
            <div class="col-md-3">
                <label class="admin-form-label">Category</label>
                <select name="category_id" class="admin-form-control form-select">
                    <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="col-md-3">
                <label class="admin-form-label">&nbsp;</label>
                <button type="submit" class="admin-btn-primary d-block w-100">Filter</button>
            </div>
        </form>
    </div>
</div>

<!-- Detailed Product Profit Table -->
<div class="admin-card">
    <div class="card-header">
        <h5><i class="bi bi-table"></i> Product Profit Analysis</h5>
        <div class="text-muted">
            Showing {{ $profitByProduct->count() }} products
        </div>
    </div>
    <div class="card-body">
        @if($profitByProduct->count() > 0)
            <div class="table-responsive">
                <table class="admin-table table table-hover">
                    <thead class="table-light">
                        <tr>
                            <th>Product Name</th>
                            <th>SKU</th>
                            <th class="text-center">Units Sold</th>
                            <th class="text-end">Total Revenue</th>
                            <th class="text-end">Total COGS</th>
                            <th class="text-end">Gross Profit</th>
                            <th class="text-center">Margin %</th>
                            <th class="text-end">Avg. Unit Profit</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($profitByProduct as $product)
                        <tr>
                            <td>
                                <strong>{{ $product['product_name'] }}</strong>
                            </td>
                            <td>
                                <code class="bg-light px-2 py-1 rounded">{{ $product['sku'] }}</code>
                            </td>
                            <td class="text-center">
                                <span class="badge bg-secondary">{{ $product['units_sold'] }}</span>
                            </td>
                            <td class="text-end">
                                <strong>₹{{ number_format($product['total_revenue'], 2) }}</strong>
                            </td>
                            <td class="text-end text-muted">
                                ₹{{ number_format($product['total_cogs'], 2) }}
                            </td>
                            <td class="text-end">
                                <strong class="{{ $product['gross_profit'] >= 0 ? 'text-success' : 'text-danger' }}">
                                    ₹{{ number_format($product['gross_profit'], 2) }}
                                </strong>
                            </td>
                            <td class="text-center">
                                <span class="badge {{ $product['margin_percent'] >= 0 ? 'bg-success' : 'bg-danger' }}">
                                    {{ number_format($product['margin_percent'], 1) }}%
                                </span>
                            </td>
                            <td class="text-end">
                                @if($product['units_sold'] > 0)
                                    <span class="{{ ($product['gross_profit'] / $product['units_sold']) >= 0 ? 'text-success' : 'text-danger' }}">
                                        ₹{{ number_format($product['gross_profit'] / $product['units_sold'], 2) }}
                                    </span>
                                @else
                                    <span class="text-muted">-</span>
                                @endif
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                    <tfoot class="table-light">
                        <tr>
                            <th colspan="2">TOTALS</th>
                            <th class="text-center">{{ $profitByProduct->sum('units_sold') }}</th>
                            <th class="text-end">₹{{ number_format($profitByProduct->sum('total_revenue'), 2) }}</th>
                            <th class="text-end">₹{{ number_format($profitByProduct->sum('total_cogs'), 2) }}</th>
                            <th class="text-end">
                                <strong class="{{ $profitByProduct->sum('gross_profit') >= 0 ? 'text-success' : 'text-danger' }}">
                                    ₹{{ number_format($profitByProduct->sum('gross_profit'), 2) }}
                                </strong>
                            </th>
                            <th class="text-center">
                                @php
                                    $totalRevenue = $profitByProduct->sum('total_revenue');
                                    $totalProfit = $profitByProduct->sum('gross_profit');
                                    $overallMargin = $totalRevenue > 0 ? ($totalProfit / $totalRevenue) * 100 : 0;
                                @endphp
                                <span class="badge {{ $overallMargin >= 0 ? 'bg-success' : 'bg-danger' }}">
                                    {{ number_format($overallMargin, 1) }}%
                                </span>
                            </th>
                            <th class="text-end">
                                @php
                                    $totalUnits = $profitByProduct->sum('units_sold');
                                    $avgUnitProfit = $totalUnits > 0 ? $totalProfit / $totalUnits : 0;
                                @endphp
                                <span class="{{ $avgUnitProfit >= 0 ? 'text-success' : 'text-danger' }}">
                                    ₹{{ number_format($avgUnitProfit, 2) }}
                                </span>
                            </th>
                        </tr>
                    </tfoot>
                </table>
            </div>
        @else
            <div class="text-center py-5">
                <i class="bi bi-graph-down text-muted" style="font-size: 4rem;"></i>
                <h4 class="text-muted mt-3">No Profit Data Available</h4>
                <p class="text-muted">No products with profit data found for the selected criteria.</p>
                <div class="mt-4">
                    <a href="{{ route('admin.products.index') }}" class="btn btn-primary me-2">
                        <i class="bi bi-plus"></i> Add Purchase Prices
                    </a>
                    <a href="{{ route('admin.reports.missing-purchase-price') }}" class="btn btn-outline-warning">
                        <i class="bi bi-exclamation-triangle"></i> View Missing Purchase Prices
                    </a>
                </div>
            </div>
        @endif
    </div>
</div>

@endsection

@section('styles')
<style>
.table th {
    border-top: none;
    font-weight: 600;
    font-size: 0.9rem;
}

.table-hover tbody tr:hover {
    background-color: rgba(0,0,0,0.02);
}

code {
    font-size: 0.85rem;
}

.badge {
    font-size: 0.8rem;
}

.admin-page-actions {
    display: flex;
    gap: 0.5rem;
}

@media (max-width: 768px) {
    .admin-page-actions {
        flex-direction: column;
        width: 100%;
    }
    
    .admin-page-actions .btn {
        width: 100%;
    }
}
</style>
@endsection