@page "/account/login"
@using TrueCV.Web.Components.Layout
@layout MainLayout

@using Microsoft.AspNetCore.Identity
@using TrueCV.Infrastructure.Identity

@inject SignInManager<ApplicationUser> SignInManager
@inject NavigationManager NavigationManager

<PageTitle>Login - TrueCV</PageTitle>

<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-5">
            <div class="card border-0 shadow">
                <div class="card-body p-5">
                    <div class="text-center mb-4">
                        <img src="/images/TrueCV_Logo.png" alt="TrueCV" class="mb-3" style="height: 60px;" />
                        <h3 class="fw-bold">Welcome Back</h3>
                        <p class="text-muted">Sign in to your TrueCV account</p>
                    </div>

                    @if (!string.IsNullOrEmpty(_errorMessage))
                    {
                        <div class="alert alert-danger" role="alert">
                            @_errorMessage
                        </div>
                    }

                    <form method="post" action="/account/perform-login">
                        <AntiforgeryToken />
                        <input type="hidden" name="returnUrl" value="@ReturnUrl" />

                        <div class="mb-3">
                            <label for="email" class="form-label">Email address</label>
                            <input id="email" name="email" type="email" class="form-control form-control-lg"
                                   placeholder="name@example.com" required />
                        </div>

                        <div class="mb-3">
                            <label for="password" class="form-label">Password</label>
                            <input id="password" name="password" type="password" class="form-control form-control-lg"
                                   placeholder="Enter your password" required />
                        </div>

                        <div class="mb-3 form-check">
                            <input id="rememberMe" name="rememberMe" type="checkbox" class="form-check-input" value="true" />
                            <label class="form-check-label" for="rememberMe">
                                Remember me
                            </label>
                        </div>

                        <div class="d-grid">
                            <button type="submit" class="btn btn-primary btn-lg">
                                Sign In
                            </button>
                        </div>
                    </form>

                    <hr class="my-4" />

                    <div class="text-center">
                        <p class="mb-0">
                            Don't have an account?
                            <a href="/account/register" class="text-decoration-none fw-medium">Create one</a>
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@code {
    private string? _errorMessage;

    [SupplyParameterFromQuery]
    public string? ReturnUrl { get; set; }

    [SupplyParameterFromQuery(Name = "error")]
    public string? Error { get; set; }

    protected override void OnInitialized()
    {
        _errorMessage = Error;
    }
}
