@model LittleShop.DTOs.BotDto
@{
    ViewData["Title"] = $"Bot Details - {Model.Name}";
    var metricsSummary = ViewData["MetricsSummary"] as LittleShop.DTOs.BotMetricsSummaryDto;
    var activeSessions = ViewData["ActiveSessions"] as IEnumerable<LittleShop.DTOs.BotSessionDto>;
}

<h1>Bot Details</h1>

@if (TempData["Success"] != null)
{
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        @TempData["Success"]
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
}

@if (TempData["BotKey"] != null)
{
    <div class="alert alert-warning">
        <strong>Important!</strong> Save this API key securely. It will not be shown again:
        <div class="mt-2">
            <code class="bg-dark text-white p-2 d-block">@TempData["BotKey"]</code>
        </div>
    </div>
}

<div class="row">
    <div class="col-md-8">
        <div class="card mb-3">
            <div class="card-header">
                <h5 class="mb-0">Bot Information</h5>
            </div>
            <div class="card-body">
                <dl class="row">
                    <dt class="col-sm-3">Name</dt>
                    <dd class="col-sm-9">@Model.Name</dd>

                    <dt class="col-sm-3">Description</dt>
                    <dd class="col-sm-9">@(string.IsNullOrEmpty(Model.Description) ? "N/A" : Model.Description)</dd>

                    <dt class="col-sm-3">Type</dt>
                    <dd class="col-sm-9"><span class="badge bg-info">@Model.Type</span></dd>

                    <dt class="col-sm-3">Platform Info</dt>
                    <dd class="col-sm-9">
                        @if (!string.IsNullOrEmpty(Model.PlatformUsername))
                        {
                            <div>
                                <strong>@@@Model.PlatformUsername</strong>
                                @if (!string.IsNullOrEmpty(Model.PlatformDisplayName))
                                {
                                    <br />
                                    <span class="text-muted">@Model.PlatformDisplayName</span>
                                }
                                @if (!string.IsNullOrEmpty(Model.PlatformId))
                                {
                                    <br />
                                    <small><code>ID: @Model.PlatformId</code></small>
                                }
                            </div>
                        }
                        else
                        {
                            <span class="text-muted">Not configured - platform info will be auto-detected on first connection</span>
                        }
                    </dd>

                    <dt class="col-sm-3">Status</dt>
                    <dd class="col-sm-9">
                        @switch (Model.Status)
                        {
                            case LittleShop.Enums.BotStatus.Active:
                                <span class="badge bg-success">Active</span>
                                break;
                            case LittleShop.Enums.BotStatus.Inactive:
                                <span class="badge bg-secondary">Inactive</span>
                                break;
                            case LittleShop.Enums.BotStatus.Suspended:
                                <span class="badge bg-warning">Suspended</span>
                                break;
                            default:
                                <span class="badge bg-dark">@Model.Status</span>
                                break;
                        }
                    </dd>

                    <dt class="col-sm-3">Bot ID</dt>
                    <dd class="col-sm-9"><code>@Model.Id</code></dd>

                    <dt class="col-sm-3">Version</dt>
                    <dd class="col-sm-9">@(string.IsNullOrEmpty(Model.Version) ? "N/A" : Model.Version)</dd>

                    <dt class="col-sm-3">IP Address</dt>
                    <dd class="col-sm-9">@(string.IsNullOrEmpty(Model.IpAddress) ? "N/A" : Model.IpAddress)</dd>

                    <dt class="col-sm-3">Created</dt>
                    <dd class="col-sm-9">@Model.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss")</dd>

                    <dt class="col-sm-3">Last Seen</dt>
                    <dd class="col-sm-9">
                        @if (Model.LastSeenAt.HasValue)
                        {
                            @Model.LastSeenAt.Value.ToString("yyyy-MM-dd HH:mm:ss")
                            @if ((DateTime.UtcNow - Model.LastSeenAt.Value).TotalMinutes < 5)
                            {
                                <span class="badge bg-success ms-2">Online</span>
                            }
                        }
                        else
                        {
                            <span class="text-muted">Never</span>
                        }
                    </dd>

                    <dt class="col-sm-3">Config Synced</dt>
                    <dd class="col-sm-9">
                        @if (Model.LastConfigSyncAt.HasValue)
                        {
                            @Model.LastConfigSyncAt.Value.ToString("yyyy-MM-dd HH:mm:ss")
                        }
                        else
                        {
                            <span class="text-muted">Never</span>
                        }
                    </dd>
                </dl>
            </div>
        </div>

        <div class="card mb-3">
            <div class="card-header">
                <h5 class="mb-0">30-Day Metrics Summary</h5>
            </div>
            <div class="card-body">
                @if (metricsSummary != null)
                {
                    <div class="row">
                        <div class="col-md-3">
                            <div class="text-center">
                                <h3>@metricsSummary.TotalSessions</h3>
                                <p class="text-muted">Total Sessions</p>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="text-center">
                                <h3>@metricsSummary.TotalOrders</h3>
                                <p class="text-muted">Total Orders</p>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="text-center">
                                <h3>$@metricsSummary.TotalRevenue.ToString("F2")</h3>
                                <p class="text-muted">Total Revenue</p>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="text-center">
                                <h3>@metricsSummary.TotalErrors</h3>
                                <p class="text-muted">Total Errors</p>
                            </div>
                        </div>
                    </div>

                    @if (metricsSummary.UptimePercentage > 0)
                    {
                        <div class="progress mt-3">
                            <div class="progress-bar bg-success" role="progressbar" style="width: @metricsSummary.UptimePercentage%" 
                                 aria-valuenow="@metricsSummary.UptimePercentage" aria-valuemin="0" aria-valuemax="100">
                                Uptime: @metricsSummary.UptimePercentage.ToString("F1")%
                            </div>
                        </div>
                    }
                }
                else
                {
                    <p class="text-muted">No metrics available</p>
                }
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">Active Sessions</h5>
            </div>
            <div class="card-body">
                @if (activeSessions != null && activeSessions.Any())
                {
                    <div class="table-responsive">
                        <table class="table table-sm">
                            <thead>
                                <tr>
                                    <th>Session ID</th>
                                    <th>Platform</th>
                                    <th>Started</th>
                                    <th>Messages</th>
                                    <th>Orders</th>
                                    <th>Spent</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var session in activeSessions.Take(10))
                                {
                                    <tr>
                                        <td><small>@session.Id.ToString().Substring(0, 8)...</small></td>
                                        <td>@session.Platform</td>
                                        <td>@session.StartedAt.ToString("HH:mm:ss")</td>
                                        <td>@session.MessageCount</td>
                                        <td>@session.OrderCount</td>
                                        <td>$@session.TotalSpent.ToString("F2")</td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                }
                else
                {
                    <p class="text-muted">No active sessions</p>
                }
            </div>
        </div>
    </div>

    <div class="col-md-4">
        <div class="card mb-3">
            <div class="card-header">
                <h5 class="mb-0">Actions</h5>
            </div>
            <div class="card-body">
                <div class="d-grid gap-2">
                    <a href="/Admin/Bots/Edit/@Model.Id" class="btn btn-primary">
                        <i class="bi bi-pencil"></i> Edit Settings
                    </a>
                    <a href="/Admin/Bots/Metrics/@Model.Id" class="btn btn-success">
                        <i class="bi bi-graph-up"></i> View Detailed Metrics
                    </a>
                    
                    @if (Model.Status == LittleShop.Enums.BotStatus.Active)
                    {
                        <form action="/Admin/Bots/Suspend/@Model.Id" method="post">
                            <button type="submit" class="btn btn-warning w-100" 
                                    onclick="return confirm('Are you sure you want to suspend this bot?')">
                                <i class="bi bi-pause-circle"></i> Suspend Bot
                            </button>
                        </form>
                    }
                    else if (Model.Status == LittleShop.Enums.BotStatus.Suspended)
                    {
                        <form action="/Admin/Bots/Activate/@Model.Id" method="post">
                            <button type="submit" class="btn btn-success w-100">
                                <i class="bi bi-play-circle"></i> Activate Bot
                            </button>
                        </form>
                    }

                    <hr />
                    
                    <form action="/Admin/Bots/Delete/@Model.Id" method="post">
                        <button type="submit" class="btn btn-danger w-100" 
                                onclick="return confirm('Are you sure you want to delete this bot? This action cannot be undone.')">
                            <i class="bi bi-trash"></i> Delete Bot
                        </button>
                    </form>
                </div>
            </div>
        </div>

        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">Quick Stats</h5>
            </div>
            <div class="card-body">
                <ul class="list-unstyled mb-0">
                    <li>
                        <strong>Total Sessions:</strong> @Model.TotalSessions
                    </li>
                    <li>
                        <strong>Active Sessions:</strong> @Model.ActiveSessions
                    </li>
                    <li>
                        <strong>Total Orders:</strong> @Model.TotalOrders
                    </li>
                    <li>
                        <strong>Total Revenue:</strong> $@Model.TotalRevenue.ToString("F2")
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>

<div class="mt-3">
    <a href="/Admin/Bots" class="btn btn-secondary">Back to List</a>
</div>