@model WeekPlannerViewModel
@{
    ViewData["Title"] = "Week Planner";
}

<div class="container-fluid">
    <h1 class="mb-4">📅 Week Planner</h1>

    <!-- Debug info -->
    <div class="alert alert-warning">
        <p>Debug: Model is @(Model == null ? "NULL" : "NOT NULL")</p>
        @if (Model != null)
        {
            <p>StartDate: @Model.StartDate</p>
            <p>CurrentWeekPlan is @(Model.CurrentWeekPlan == null ? "NULL" : "NOT NULL")</p>
        }
    </div>

    @if (Model?.CurrentWeekPlan == null)
    {
        <div class="alert alert-info">
            <h4>No meal plan for this week yet!</h4>
            <p>Create a new week plan to start planning your meals.</p>
            <form asp-action="CreateWeekPlan" method="post" class="d-inline">
                <input type="hidden" name="startDate"
                    value="@(Model?.StartDate.ToString("yyyy-MM-dd") ?? DateTime.Today.ToString("yyyy-MM-dd"))" />
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-plus"></i> Create Week Plan
                </button>
            </form>
        </div>
    }
    else
    {
        <div class="week-header mb-4">
            <h2>@Model!.CurrentWeekPlan!.Name</h2>
            <p class="text-muted">Week of @Model!.StartDate.ToString("MMMM dd, yyyy")</p>
        </div>

        <div class="row">
            @foreach (var dayPlan in Model!.CurrentWeekPlan!.DayPlans.OrderBy(dp => dp.Date))
            {
                <div class="col-xl-3 col-lg-4 col-md-6 mb-4">
                    <div class="card h-100">
                        <div class="card-header d-flex justify-content-between align-items-center">
                            <h5 class="mb-0">@dayPlan.DayOfWeek</h5>
                            <small class="text-muted">@dayPlan.Date.ToString("MMM dd")</small>
                        </div>
                        <div class="card-body">
                            @if (dayPlan.Recipe != null)
                            {
                                <div class="recipe-preview">
                                    <h6>@dayPlan.Recipe.Title</h6>
                                    <div class="recipe-info">
                                        @if (!string.IsNullOrEmpty(dayPlan.MainIngredient))
                                        {
                                            <span class="badge bg-primary mb-2">@dayPlan.MainIngredient</span>
                                        }
                                        @if (!string.IsNullOrEmpty(dayPlan.Recipe.Time))
                                        {
                                            <span class="badge bg-info mb-2">@dayPlan.Recipe.Time min</span>
                                        }
                                        <p class="small text-muted">@(dayPlan.Recipe.Description?.Length > 80 ?
                                                                            dayPlan.Recipe.Description.Substring(0, 80) + "..." :
                                                                            dayPlan.Recipe.Description)</p>
                                    </div>
                                </div>
                            }
                            else
                            {
                                <div class="text-center text-muted">
                                    <i class="fas fa-utensils fa-3x mb-3" style="color: #dee2e6;"></i>
                                    <p>No recipe selected</p>
                                    @if (!string.IsNullOrEmpty(dayPlan.MainIngredient))
                                    {
                                        <span class="badge bg-secondary">@dayPlan.MainIngredient</span>
                                    }
                                </div>
                            }
                        </div>
                        <div class="card-footer">
                            <div class="btn-group w-100" role="group">
                                <a href="@Url.Action("ConfigureDay", new { weekPlanId = Model.CurrentWeekPlan.Id, dayOfWeek = dayPlan.DayOfWeek })"
                                    class="btn btn-outline-primary btn-sm">
                                    <i class="fas fa-cog"></i> Configure
                                </a>
                                <form asp-action="RandomizeRecipe" method="post" class="d-inline" style="flex: 1;">
                                    <input type="hidden" name="dayPlanId" value="@dayPlan.Id" />
                                    <button type="submit" class="btn btn-outline-success btn-sm w-100">
                                        <i class="fas fa-dice"></i> Random
                                    </button>
                                </form>
                            </div>
                            @if (dayPlan.Recipe != null)
                            {
                                <div class="mt-2">
                                    <a href="@Url.Action("Details", "Recipe", new { id = dayPlan.Recipe.Id })"
                                        class="btn btn-sm btn-outline-secondary w-100">
                                        <i class="fas fa-eye"></i> View Recipe
                                    </a>
                                </div>
                            }
                        </div>
                    </div>
                </div>
            }
        </div>

        <div class="week-actions mt-4">
            <div class="row">
                <div class="col-md-6">
                    <form asp-action="CreateWeekPlan" method="post" class="d-inline">
                        <input type="hidden" name="startDate" value="@Model.StartDate.AddDays(7).ToString("yyyy-MM-dd")" />
                        <button type="submit" class="btn btn-success">
                            <i class="fas fa-plus"></i> Create Next Week
                        </button>
                    </form>
                </div>
                <div class="col-md-6 text-end">
                    <form asp-action="CreateWeekPlan" method="post" class="d-inline">
                        <input type="hidden" name="startDate" value="@Model.StartDate.AddDays(-7).ToString("yyyy-MM-dd")" />
                        <button type="submit" class="btn btn-outline-secondary">
                            <i class="fas fa-arrow-left"></i> Create Previous Week
                        </button>
                    </form>
                </div>
            </div>
        </div>
    }
</div>

<style>
    .recipe-suggestion:hover {
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }

    .card-footer .btn-group {
        gap: 0;
    }

    .card-footer .btn-group .btn {
        border-radius: 0;
    }

    .card-footer .btn-group .btn:first-child {
        border-top-left-radius: 0.375rem;
        border-bottom-left-radius: 0.375rem;
    }

    .card-footer .btn-group .btn:last-child {
        border-top-right-radius: 0.375rem;
        border-bottom-right-radius: 0.375rem;
    }
</style>