@page "/building-calculator"
@using System.Text.Json
@using AOW4.Data

<div class="calculator-page">
    <h1>Building Plan Calculator</h1>
    <p>Simulates resource income each turn and tracks build completion times for ordered buildings.</p>

    <section>
        <h2>Build Order</h2>
        <table class="calculator-table">
            <thead>
                <tr>
                    <th>Turn Requested</th>
                    <th>Building</th>
                    <th>Finish Turn</th>
                    <th>Industry Remaining</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var entry in Result.BuildOrder)
                {
                    <tr>
                        <td>@entry.RequestedTurn</td>
                        <td>@entry.Name</td>
                        <td>@(entry.BuiltFinishTurn == 0 ? "Starting" : entry.BuiltFinishTurn.ToString())</td>
                        <td>@entry.IndustryCostRemaining</td>
                    </tr>
                }
            </tbody>
        </table>
    </section>

    <section>
        <h2>Gold Over Time</h2>
        <table class="calculator-table">
            <thead>
                <tr>
                    <th>Turn</th>
                    <th>Stored Gold</th>
                    <th>Income</th>
                    <th>Upkeep</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var snapshot in Result.ResourceHistory)
                {
                    <tr>
                        <td>@snapshot.Turn</td>
                        <td>@snapshot.Stored.Gold</td>
                        <td>@snapshot.TotalIncome.Gold</td>
                        <td>@snapshot.TotalUpkeep.Gold</td>
                    </tr>
                }
            </tbody>
        </table>
    </section>

    <section>
        <h2>Result JSON</h2>
        <pre class="json-output">@Json</pre>
    </section>
</div>

@code {
    private BuildPlanResult Result = new();
    private string Json = string.Empty;

    protected override void OnInitialized()
    {
        Result = BuildingPlanCalculator.CreateSampleBuildPlan(60);
        Json = JsonSerializer.Serialize(Result, new JsonSerializerOptions
        {
            WriteIndented = true
        });
    }
}

<style>
    .calculator-page {
        padding: 2rem;
        max-width: 1100px;
        margin: 0 auto;
    }

    .calculator-table {
        width: 100%;
        border-collapse: collapse;
        margin-bottom: 1.75rem;
    }

    .calculator-table th,
    .calculator-table td {
        padding: 0.75rem 1rem;
        border: 1px solid #d3d3d3;
        text-align: left;
        font-size: 0.95rem;
    }

    .calculator-table th {
        background-color: #f3f6fb;
        font-weight: 600;
    }

    .json-output {
        display: block;
        white-space: pre-wrap;
        background: #1f2937;
        color: #f8fafc;
        padding: 1rem;
        border-radius: 0.5rem;
        overflow-x: auto;
        font-family: Consolas, "Courier New", monospace;
        font-size: 0.9rem;
    }
</style>
