@extends('layouts.admin')

@section('title', 'Reports')

@section('content')
    <section class="dashboard-head">
        <div>
            <span class="eyebrow">Sales</span>
            <h1>Reports</h1>
        </div>
        <a class="button ghost" href="{{ route('admin.reports.low-stock-export', ['low_stock_threshold' => $lowStockThreshold]) }}">Export low stock</a>
    </section>

    <section class="panel">
        <form method="GET" action="{{ route('admin.reports.index') }}" class="admin-form report-filter-form">
            <div class="form-grid">
                <label>
                    <span>From</span>
                    <input name="from" type="date" value="{{ $from->format('Y-m-d') }}">
                </label>

                <label>
                    <span>To</span>
                    <input name="to" type="date" value="{{ $to->format('Y-m-d') }}">
                </label>

                <label>
                    <span>Low stock threshold</span>
                    <input name="low_stock_threshold" type="number" min="0" step="1" value="{{ $lowStockThreshold }}">
                </label>
            </div>
            <div class="table-actions">
                <button class="button primary" type="submit">Apply filters</button>
                <a class="button ghost" href="{{ route('admin.reports.index') }}">Reset</a>
            </div>
        </form>
    </section>

    <section class="metrics">
        @foreach ($metrics as $metric)
            <article class="metric">
                <span>{{ $metric['label'] }}</span>
                <strong>{{ $metric['value'] }}</strong>
                <small>{{ $metric['note'] }}</small>
            </article>
        @endforeach
    </section>

    <section class="admin-grid">
        <article class="panel">
            <div class="panel-head">
                <div>
                    <h2>Monthly Revenue</h2>
                    <span>Last 12 months</span>
                </div>
            </div>
            <div class="report-bars">
                @foreach ($monthlyRevenue as $month)
                    <div class="report-bar-row">
                        <span>{{ $month['label'] }}</span>
                        <div class="bar"><span style="width: {{ max(4, ($month['revenue'] / $maxMonthlyRevenue) * 100) }}%"></span></div>
                        <strong>{{ $month['revenue_display'] }}</strong>
                    </div>
                @endforeach
            </div>
        </article>

        <article class="panel">
            <div class="panel-head">
                <div>
                    <h2>Low Stock</h2>
                    <span>At or below {{ $lowStockThreshold }} units</span>
                </div>
                <a class="text-button" href="{{ route('admin.reports.low-stock-export', ['low_stock_threshold' => $lowStockThreshold]) }}">Export CSV</a>
            </div>
            <div class="inventory-list">
                @forelse ($lowStockItems->take(8) as $item)
                    <div class="inventory-item">
                        <div class="inventory-top">
                            <span>{{ $item['product'] }} <small>{{ $item['variant'] }}</small></span>
                            <span>{{ $item['stock'] }} left</span>
                        </div>
                        <div class="bar"><span style="width: {{ min(100, max(5, $item['stock'] * 20)) }}%"></span></div>
                    </div>
                @empty
                    <p class="empty-state">No low stock items.</p>
                @endforelse
            </div>
        </article>
    </section>

    <section class="panel">
        <div class="panel-head">
            <div>
                <h2>Best-Selling Products</h2>
                <span>{{ $from->format('M d, Y') }} to {{ $to->format('M d, Y') }}</span>
            </div>
        </div>
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>Units Sold</th>
                        <th>Revenue</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($bestSellingProducts as $product)
                        <tr>
                            <td><strong>{{ $product['name'] }}</strong></td>
                            <td>{{ number_format($product['units_sold']) }}</td>
                            <td>{{ $product['revenue_display'] }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="3">No sales found for this date range.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </section>

    <section class="panel">
        <div class="panel-head">
            <div>
                <h2>Low Stock Export Preview</h2>
                <span>{{ $lowStockItems->count() }} rows available for CSV export</span>
            </div>
        </div>
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Type</th>
                        <th>Product</th>
                        <th>Variant</th>
                        <th>Sale Price</th>
                        <th>Purchase Price</th>
                        <th>Stock</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($lowStockItems->take(12) as $item)
                        <tr>
                            <td>{{ $item['type'] }}</td>
                            <td>{{ $item['product'] }}</td>
                            <td>{{ $item['variant'] }}</td>
                            <td>{{ $item['sale_price'] }}</td>
                            <td>{{ $item['purchase_price'] }}</td>
                            <td>{{ $item['stock'] }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="6">No low stock items.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </section>
@endsection
