@extends('layouts.admin')

@section('title', 'Profit Report')

@section('content')
<div class="admin-page-header">
    <h1>Profit 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>
    </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-2">
                <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-2">
                <label class="admin-form-label">Order Status</label>
                <select name="order_status" class="admin-form-control form-select">
                    <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="col-md-2">
                <label class="admin-form-label">&nbsp;</label>
                <button type="submit" class="admin-btn-primary d-block w-100">Filter</button>
            </div>
        </form>
    </div>
</div>

<!-- Summary Cards -->
<div class="row g-4 mb-4">
    <div class="col-md-6 col-lg-3">
        <div class="kpi-card">
            <div class="kpi-card-icon kpi-card-icon-primary">
                <i class="bi bi-currency-rupee"></i>
            </div>
            <div class="kpi-card-content-wrapper">
                <div class="kpi-card-value">₹{{ number_format($profitMetrics['total_revenue'], 2) }}</div>
                <div class="kpi-card-label">Total Revenue</div>
            </div>
        </div>
    </div>
    <div class="col-md-6 col-lg-3">
        <div class="kpi-card">
            <div class="kpi-card-icon kpi-card-icon-warning">
                <i class="bi bi-box"></i>
            </div>
            <div class="kpi-card-content-wrapper">
                <div class="kpi-card-value">₹{{ number_format($profitMetrics['total_cogs'], 2) }}</div>
                <div class="kpi-card-label">Total COGS</div>
            </div>
        </div>
    </div>
    <div class="col-md-6 col-lg-3">
        <div class="kpi-card">
            <div class="kpi-card-icon kpi-card-icon-success">
                <i class="bi bi-graph-up"></i>
            </div>
            <div class="kpi-card-content-wrapper">
                <div class="kpi-card-value">₹{{ number_format($profitMetrics['total_profit'], 2) }}</div>
                <div class="kpi-card-label">Gross Profit</div>
            </div>
        </div>
    </div>
    <div class="col-md-6 col-lg-3">
        <div class="kpi-card">
            <div class="kpi-card-icon kpi-card-icon-info">
                <i class="bi bi-percent"></i>
            </div>
            <div class="kpi-card-content-wrapper">
                <div class="kpi-card-value">{{ number_format($profitMetrics['profit_margin_percent'], 2) }}%</div>
                <div class="kpi-card-label">Profit Margin</div>
            </div>
        </div>
    </div>
</div>

<!-- Data Coverage Info -->
@if($profitMetrics['profit_data_coverage_percent'] < 100)
<div class="alert alert-warning mb-4">
    <i class="bi bi-exclamation-triangle"></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="alert-link">View products missing purchase prices</a>
</div>
@endif

<!-- Top Profitable Products -->
<div class="row mb-4">
    <div class="col-md-6">
        <div class="admin-card">
            <div class="card-header">
                <h5><i class="bi bi-trophy"></i> Top Profitable Products</h5>
            </div>
            <div class="card-body">
                @if($topProducts['most_profitable']->count() > 0)
                    <div class="table-responsive">
                        <table class="table table-sm">
                            <thead>
                                <tr>
                                    <th>Product</th>
                                    <th>Profit</th>
                                    <th>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="text-success">₹{{ number_format($product['gross_profit'], 2) }}</td>
                                    <td>
                                        <span class="badge bg-success">{{ number_format($product['margin_percent'], 1) }}%</span>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <p class="text-muted">No profit data available. Add purchase prices to products to see profit analysis.</p>
                @endif
            </div>
        </div>
    </div>
    
    <div class="col-md-6">
        <div class="admin-card">
            <div class="card-header">
                <h5><i class="bi bi-exclamation-triangle text-warning"></i> Least Profitable Products</h5>
            </div>
            <div class="card-body">
                @if($topProducts['least_profitable']->count() > 0)
                    <div class="table-responsive">
                        <table class="table table-sm">
                            <thead>
                                <tr>
                                    <th>Product</th>
                                    <th>Profit</th>
                                    <th>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="{{ $product['gross_profit'] >= 0 ? 'text-success' : 'text-danger' }}">
                                        ₹{{ number_format($product['gross_profit'], 2) }}
                                    </td>
                                    <td>
                                        <span class="badge {{ $product['margin_percent'] >= 0 ? 'bg-success' : 'bg-danger' }}">
                                            {{ number_format($product['margin_percent'], 1) }}%
                                        </span>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <p class="text-muted">No profit data available.</p>
                @endif
            </div>
        </div>
    </div>
</div>

<!-- Detailed Profit by Product -->
<div class="admin-card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5><i class="bi bi-table"></i> Profit by Product</h5>
        <div class="card-actions">
            <a href="{{ route('admin.reports.profit.by-product', request()->query()) }}" class="btn btn-sm btn-outline-primary">
                View Detailed Report
            </a>
        </div>
    </div>
    <div class="card-body">
        @if($profitByProduct->count() > 0)
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th>Product</th>
                            <th>SKU</th>
                            <th>Units Sold</th>
                            <th>Revenue</th>
                            <th>COGS</th>
                            <th>Gross Profit</th>
                            <th>Margin %</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($profitByProduct->take(10) as $product)
                        <tr>
                            <td><strong>{{ $product['product_name'] }}</strong></td>
                            <td><code>{{ $product['sku'] }}</code></td>
                            <td>{{ $product['units_sold'] }}</td>
                            <td>₹{{ number_format($product['total_revenue'], 2) }}</td>
                            <td>₹{{ number_format($product['total_cogs'], 2) }}</td>
                            <td class="{{ $product['gross_profit'] >= 0 ? 'text-success' : 'text-danger' }}">
                                <strong>₹{{ number_format($product['gross_profit'], 2) }}</strong>
                            </td>
                            <td>
                                <span class="badge {{ $product['margin_percent'] >= 0 ? 'bg-success' : 'bg-danger' }}">
                                    {{ number_format($product['margin_percent'], 1) }}%
                                </span>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            
            @if($profitByProduct->count() > 10)
                <div class="text-center mt-3">
                    <a href="{{ route('admin.reports.profit.by-product', request()->query()) }}" class="btn btn-outline-primary">
                        View All {{ $profitByProduct->count() }} Products
                    </a>
                </div>
            @endif
        @else
            <div class="text-center py-4">
                <i class="bi bi-graph-down text-muted" style="font-size: 3rem;"></i>
                <h5 class="text-muted mt-2">No Profit Data Available</h5>
                <p class="text-muted">Add purchase prices to your products to see profit analysis.</p>
                <a href="{{ route('admin.products.index') }}" class="btn btn-primary">
                    <i class="bi bi-plus"></i> Add Purchase Prices
                </a>
            </div>
        @endif
    </div>
</div>

@endsection

@section('styles')
<style>
.admin-stat-card {
    background: white;
    border-radius: 8px;
    padding: 1.5rem;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    display: flex;
    align-items: center;
    gap: 1rem;
}

.stat-icon {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 1.5rem;
}

.stat-content h3 {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 600;
}

.stat-content p {
    margin: 0;
    color: #6c757d;
    font-size: 0.9rem;
}

.card-actions {
    display: flex;
    gap: 0.5rem;
}
</style>
@endsection