@page "/"
@using Intentor.Services
@using Intentor.Models
@inject HomeAssistantService HomeAssistant

<PageTitle>Home</PageTitle>

<h1>Entity States</h1>

@if (states == null)
{
    <p>Loading...</p>
}
else if (!states.Any())
{
    <p>No entities found.</p>
}
else
{
    <table class="table">
        <thead>
            <tr>
                <th>Entity ID</th>
                <th>State</th>
                <th>Last Changed</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var state in states)
            {
                <tr>
                    <td>@state.EntityId</td>
                    <td>@state.State</td>
                    <td>@state.LastChanged.ToString("g")</td>
                </tr>
            }
        </tbody>
    </table>
}

@code {
    private List<EntityState>? states;

    protected override async Task OnInitializedAsync()
    {
        states = await HomeAssistant.GetStatesAsync();
    }
}
