@page "/ticket/edit/{Id:guid}"
@rendermode InteractiveServer
@using TodoTicketApp.Models
@using TodoTicketApp.Services
@inject ITicketService TicketService
@inject NavigationManager Nav

<div class="detail-container">
    @if (ticket == null)
    {
        <p>Lade Ticket...</p>
    }
    else
    {
        <div class="header-actions">
            <button class="btn btn-link" @onclick="GoBack">&larr; Zurück zum Dashboard</button>
            <span class="badge @(ticket.IsCompleted ? "bg-success" : "bg-warning")">
                @(ticket.IsCompleted ? "Erledigt" : "Offen")
            </span>
        </div>

        <div class="card shadow-sm p-4">
            <div class="mb-3">
                <label class="form-label fw-bold">Titel</label>
                <input class="form-control form-control-lg" @bind="ticket.Title" />
            </div>

            <div class="row mb-3">
                <div class="col-md-6">
                    <label class="form-label fw-bold">Priorität</label>
                    <select class="form-select" @bind="ticket.Priority">
                        @foreach (var prio in Enum.GetValues<TicketPriority>())
                        {
                            <option value="@prio">@prio</option>
                        }
                    </select>
                </div>
                <div class="col-md-6 d-flex align-items-end gap-4">
                    <div class="form-check mb-2">
                        <input class="form-check-input" type="checkbox" id="isWaiting" @bind="ticket.IsWaitingForFeedback">
                        <label class="form-check-label text-warning" for="isWaiting">Wartet auf Rückmeldung</label>
                    </div>
                    <div class="form-check mb-2">
                        <input class="form-check-input" type="checkbox" id="isDone" @bind="ticket.IsCompleted">
                        <label class="form-check-label text-success" for="isDone">Als erledigt markieren</label>
                    </div>
                </div>
            </div>

            <div class="mb-3">
                <label class="form-label fw-bold">Beschreibung</label>
                <textarea class="form-control" rows="5" @bind="ticket.Description" placeholder="Beschreibe das Ticket näher..."></textarea>
            </div>

            <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                <button class="btn btn-primary px-5" @onclick="Save">Speichern</button>
            </div>
        </div>

        <div class="row mt-4">
            <div class="col-md-7">
                <h5 class="mb-3">Kommentare (@ticket.Comments.Count)</h5>
                <div class="comment-list mb-3">
                    @foreach (var c in ticket.Comments.OrderByDescending(x => x.CreatedAt))
                    {
                        <div class="card mb-2 p-2 bg-light border-0">
                            <small class="text-muted">@c.CreatedAt.ToLocalTime().ToString("g")</small>
                            <p class="mb-0">@c.Text</p>
                        </div>
                    }
                </div>
                <div class="input-group">
                    <input class="form-control" placeholder="Kommentar schreiben..." @bind="newCommentText" />
                    <button class="btn btn-outline-secondary" @onclick="AddComment">Senden</button>
                </div>
            </div>

            <div class="col-md-5">
                <h5 class="mb-3">Anhänge</h5>
                <div class="list-group mb-2">
                    @foreach (var file in ticket.AttachmentNames)
                    {
                        <div class="list-group-item d-flex justify-content-between align-items-center">
                            <span><i class="bi bi-file-earmark"></i> @file</span>
                        </div>
                    }
                </div>
                <button class="btn btn-sm btn-outline-primary" @onclick="MockAddFile">+ Datei simulieren</button>
            </div>
        </div>
    }
</div>

@code {
    [Parameter] public Guid Id { get; set; }
    private Ticket? ticket;
    private string newCommentText = "";

    protected override void OnInitialized()
    {
        ticket = TicketService.GetTicketById(Id);
    }

    private void Save()
    {
        if (ticket != null) TicketService.UpdateTicket(ticket);
        Nav.NavigateTo("/");
    }

    private void AddComment()
    {
        if (!string.IsNullOrWhiteSpace(newCommentText) && ticket != null)
        {
            TicketService.AddComment(ticket.Id, new TicketComment { Text = newCommentText });
            newCommentText = "";
            StateHasChanged();
        }
    }

    private void MockAddFile()
    {
        ticket?.AttachmentNames.Add($"dokument_{DateTime.Now.Ticks % 1000}.pdf");
    }

    private void GoBack() => Nav.NavigateTo("/");
}

<style>
    .detail-container { max-width: 1000px; margin: 2rem auto; }
    .header-actions { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; }
</style>