@{
    ViewData["Title"] = "Database Test";
}

<div class="container mt-4">
    <h2>Database Connection Test</h2>
    
    <div class="alert @(ViewBag.Error != null ? "alert-danger" : "alert-success")">
        <h4>@ViewBag.ConnectionStatus</h4>
        @if (ViewBag.Error != null)
        {
            <p><strong>Error:</strong> @ViewBag.Error</p>
        }
        @if (ViewBag.RecipeCount != null)
        {
            <p><strong>Recipes in database:</strong> @ViewBag.RecipeCount</p>
        }
        @if (ViewBag.TableError != null)
        {
            <p><strong>Table Error:</strong> @ViewBag.TableError</p>
        }
    </div>

    @if (ViewBag.Tables != null)
    {
        <div class="card">
            <div class="card-header">
                <h5>Tables in Database 'recept'</h5>
            </div>
            <div class="card-body">
                @if (((List<string>)ViewBag.Tables).Any())
                {
                    <ul class="list-group">
                        @foreach (var table in (List<string>)ViewBag.Tables)
                        {
                            <li class="list-group-item">
                                @table
                                @if (table.ToLower().Contains("recepie"))
                                {
                                    <span class="badge bg-success ms-2">Recipe Related</span>
                                }
                            </li>
                        }
                    </ul>
                }
                else
                {
                    <p class="text-muted">No tables found in the database.</p>
                }
            </div>
        </div>
    }

    @if (ViewBag.RecepieColumns != null)
    {
        <div class="card mt-3">
            <div class="card-header">
                <h5>Structure of 'recepie' table</h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>Column Name</th>
                                <th>Data Type</th>
                                <th>Nullable</th>
                                <th>Key</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var column in (List<dynamic>)ViewBag.RecepieColumns)
                            {
                                <tr>
                                    <td><strong>@column.Name</strong></td>
                                    <td>@column.Type</td>
                                    <td>@column.Nullable</td>
                                    <td>@column.Key</td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    }

    @if (ViewBag.SampleData != null && ViewBag.ColumnNames != null)
    {
        <div class="card mt-3">
            <div class="card-header">
                <h5>Sample Data from 'recepie' table</h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-striped table-sm">
                        <thead>
                            <tr>
                                @foreach (var columnName in (List<string>)ViewBag.ColumnNames)
                                {
                                    <th>@columnName</th>
                                }
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var row in (List<Dictionary<string, object>>)ViewBag.SampleData)
                            {
                                <tr>
                                    @foreach (var columnName in (List<string>)ViewBag.ColumnNames)
                                    {
                                        <td>
                                            @{
                                                var value = row[columnName]?.ToString() ?? "NULL";
                                                if (value.Length > 50)
                                                {
                                                    value = value.Substring(0, 47) + "...";
                                                }
                                            }
                                            @value
                                        </td>
                                    }
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    }

    <div class="mt-3">
        <a href="@Url.Action("Index")" class="btn btn-primary">Back to Recipes</a>
    </div>
</div>