@using CutList.Core
@using CutList.Core.Nesting
@using CutList.Core.Formatting
@inject ReportService ReportService

<div class="cut-list-report">
    <header class="report-header">
        <h1>CUT LIST</h1>
        <div class="meta-info">
            <div class="meta-row"><span>Date:</span> @DateTime.Now.ToString("g")</div>
            <div class="meta-row"><span>Job:</span> @Job.Name</div>
            @if (!string.IsNullOrWhiteSpace(Job.Customer))
            {
                <div class="meta-row"><span>Customer:</span> @Job.Customer</div>
            }
            @if (Job.CuttingTool != null)
            {
                <div class="meta-row"><span>Cut Method:</span> @Job.CuttingTool.Name (kerf: @Job.CuttingTool.KerfInches")</div>
            }
            <div class="meta-row"><span>Stock Bars:</span> @PackResult.Bins.Count</div>
            <div class="meta-row"><span>Total Pieces:</span> @TotalPieces</div>
        </div>
    </header>

    @foreach (var (bin, index) in PackResult.Bins.Select((b, i) => (b, i + 1)))
    {
        <section class="bin-section">
            <h2>BAR #@index - Length: @ReportService.FormatLength(bin.Length)</h2>
            <table class="cuts-table">
                <thead>
                    <tr>
                        <th style="width: 60px;">Qty</th>
                        <th style="width: 120px;">Length</th>
                        <th>Label</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var group in ReportService.GroupItems(bin.Items))
                    {
                        <tr>
                            <td>@group.Count</td>
                            <td>@ReportService.FormatLength(group.Length)</td>
                            <td>@group.Name</td>
                        </tr>
                    }
                </tbody>
            </table>
            <div class="drop">
                Remaining drop: @ReportService.FormatLength(bin.RemainingLength)
                (@((bin.Utilization * 100).ToString("F1"))% utilization)
            </div>
        </section>
    }

    <footer class="summary">
        <h2>SUMMARY</h2>
        <div class="summary-grid">
            <div class="summary-row"><span>Stock Bars Needed:</span> <strong>@PackResult.Bins.Count</strong></div>
            <div class="summary-row"><span>Total Pieces:</span> <strong>@TotalPieces</strong></div>
            <div class="summary-row"><span>Total Material:</span> <strong>@ReportService.FormatLength(TotalMaterial)</strong></div>
            <div class="summary-row"><span>Total Used:</span> <strong>@ReportService.FormatLength(TotalUsed)</strong></div>
            <div class="summary-row"><span>Total Waste:</span> <strong>@ReportService.FormatLength(TotalWaste)</strong></div>
            <div class="summary-row"><span>Efficiency:</span> <strong>@Efficiency.ToString("F1")%</strong></div>
        </div>
    </footer>

    @if (!string.IsNullOrEmpty(Job.Notes))
    {
        <div class="notes-section">
            <h3>Notes</h3>
            <p>@Job.Notes</p>
        </div>
    }
</div>

@code {
    [Parameter, EditorRequired]
    public Job Job { get; set; } = null!;

    [Parameter, EditorRequired]
    public PackResult PackResult { get; set; } = null!;

    private int TotalPieces => PackResult.Bins.Sum(b => b.Items.Count);
    private double TotalMaterial => PackResult.Bins.Sum(b => b.Length);
    private double TotalUsed => PackResult.Bins.Sum(b => b.UsedLength);
    private double TotalWaste => PackResult.Bins.Sum(b => b.RemainingLength);
    private double Efficiency => TotalMaterial > 0 ? TotalUsed / TotalMaterial * 100 : 0;
}
