@page "/account/register"
@using TrueCV.Web.Components.Layout
@layout MainLayout
@rendermode InteractiveServer

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

@inject UserManager<ApplicationUser> UserManager
@inject SignInManager<ApplicationUser> SignInManager
@inject NavigationManager NavigationManager

<PageTitle>Register - 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">Create Account</h3>
                        <p class="text-muted">Start verifying CVs with confidence</p>
                    </div>

                    @if (!string.IsNullOrEmpty(_errorMessage))
                    {
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            @_errorMessage
                            <button type="button" class="btn-close" @onclick="() => _errorMessage = null" aria-label="Close"></button>
                        </div>
                    }

                    <EditForm Model="_model" OnValidSubmit="HandleRegister" FormName="register">
                        <DataAnnotationsValidator />

                        <div class="mb-3">
                            <label for="email" class="form-label">Email address</label>
                            <InputText id="email" class="form-control form-control-lg" @bind-Value="_model.Email"
                                       placeholder="name@example.com" />
                            <ValidationMessage For="() => _model.Email" class="text-danger" />
                        </div>

                        <div class="mb-3">
                            <label for="password" class="form-label">Password</label>
                            <InputText id="password" type="password" class="form-control form-control-lg"
                                       @bind-Value="_model.Password" placeholder="Create a password" />
                            <ValidationMessage For="() => _model.Password" class="text-danger" />
                            <div class="form-text">Password must be at least 12 characters with uppercase, lowercase, number, and symbol.</div>
                        </div>

                        <div class="mb-4">
                            <label for="confirmPassword" class="form-label">Confirm Password</label>
                            <InputText id="confirmPassword" type="password" class="form-control form-control-lg"
                                       @bind-Value="_model.ConfirmPassword" placeholder="Confirm your password" />
                            <ValidationMessage For="() => _model.ConfirmPassword" class="text-danger" />
                        </div>

                        <div class="d-grid">
                            <button type="submit" class="btn btn-primary btn-lg" disabled="@_isLoading">
                                @if (_isLoading)
                                {
                                    <span class="spinner-border spinner-border-sm me-2" role="status" aria-hidden="true"></span>
                                    <span>Creating account...</span>
                                }
                                else
                                {
                                    <span>Create Account</span>
                                }
                            </button>
                        </div>
                    </EditForm>

                    <hr class="my-4" />

                    <div class="text-center">
                        <p class="mb-0">
                            Already have an account?
                            <a href="/account/login" class="text-decoration-none fw-medium">Sign in</a>
                        </p>
                    </div>
                </div>
            </div>

            <div class="text-center mt-4">
                <small class="text-muted">
                    By creating an account, you agree to our
                    <a href="#" class="text-decoration-none">Terms of Service</a>
                    and
                    <a href="#" class="text-decoration-none">Privacy Policy</a>
                </small>
            </div>
        </div>
    </div>
</div>

@code {
    private RegisterModel _model = new();
    private bool _isLoading;
    private string? _errorMessage;

    private async Task HandleRegister()
    {
        _isLoading = true;
        _errorMessage = null;

        try
        {
            if (_model.Password != _model.ConfirmPassword)
            {
                _errorMessage = "Passwords do not match.";
                _isLoading = false;
                return;
            }

            var user = new ApplicationUser
            {
                UserName = _model.Email,
                Email = _model.Email,
                Plan = Domain.Enums.UserPlan.Free,
                ChecksUsedThisMonth = 0
            };

            var result = await UserManager.CreateAsync(user, _model.Password);

            if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent: false);
                NavigationManager.NavigateTo("/dashboard", forceLoad: true);
            }
            else
            {
                var errors = result.Errors.Select(e => e.Description);
                _errorMessage = string.Join(" ", errors);
            }
        }
        catch (Exception ex)
        {
            _errorMessage = $"An error occurred: {ex.Message}";
        }
        finally
        {
            _isLoading = false;
        }
    }

    private sealed class RegisterModel
    {
        [System.ComponentModel.DataAnnotations.Required(ErrorMessage = "Email is required")]
        [System.ComponentModel.DataAnnotations.EmailAddress(ErrorMessage = "Invalid email format")]
        public string Email { get; set; } = string.Empty;

        [System.ComponentModel.DataAnnotations.Required(ErrorMessage = "Password is required")]
        [System.ComponentModel.DataAnnotations.MinLength(12, ErrorMessage = "Password must be at least 12 characters")]
        public string Password { get; set; } = string.Empty;

        [System.ComponentModel.DataAnnotations.Required(ErrorMessage = "Please confirm your password")]
        [System.ComponentModel.DataAnnotations.Compare(nameof(Password), ErrorMessage = "Passwords do not match")]
        public string ConfirmPassword { get; set; } = string.Empty;
    }
}
