@page "/login"
@page "/"
@attribute [AllowAnonymous]
@rendermode InteractiveWebAssembly

@inject NavigationManager Navigation
@inject HttpClient Http

<PageTitle>고객 포탈 로그인</PageTitle>

<div class="portal-login-container">
    <div class="portal-login-card">
        <h1>고객 포탈</h1>
        <p>이메일과 비밀번호로 로그인하세요</p>

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

        <form @onsubmit="HandleLogin">
            <div class="form-group mb-3">
                <label for="email" class="form-label">이메일</label>
                <input type="email" class="form-control" id="email"
                    @bind="Email" required />
            </div>

            <div class="form-group mb-3">
                <label for="password" class="form-label">비밀번호</label>
                <input type="password" class="form-control" id="password"
                    @bind="Password" required />
            </div>

            <button type="submit" class="btn btn-primary w-100" disabled="@IsLoading">
                @if (IsLoading) { <span>로그인 중...</span> } else { <span>로그인</span> }
            </button>
        </form>

        <div class="mt-3 text-center">
            <p class="text-muted">계정이 없으신가요? <a href="/register">가입하기</a></p>
        </div>

        <hr class="my-4" />

        <div class="oauth-section">
            <p class="text-muted mb-3">또는 소셜 계정으로 로그인</p>
            <button type="button" class="btn btn-outline-secondary w-100 mb-2" @onclick="LoginWithGoogle">
                Google로 로그인
            </button>
            <button type="button" class="btn btn-outline-secondary w-100 mb-2" @onclick="LoginWithNaver">
                Naver로 로그인
            </button>
            <button type="button" class="btn btn-outline-secondary w-100" @onclick="LoginWithKakao">
                Kakao로 로그인
            </button>
        </div>
    </div>
</div>

<style>
    .portal-login-container {
        display: flex;
        align-items: center;
        justify-content: center;
        min-height: 100vh;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        padding: 20px;
    }

    .portal-login-card {
        background: white;
        border-radius: 8px;
        box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
        padding: 40px;
        width: 100%;
        max-width: 400px;
    }

    .portal-login-card h1 {
        text-align: center;
        font-size: 24px;
        margin-bottom: 10px;
        color: #333;
    }

    .portal-login-card p {
        text-align: center;
        color: #666;
        margin-bottom: 30px;
    }

    .form-group label {
        font-weight: 500;
        color: #333;
    }

    .form-control {
        border-radius: 4px;
        border: 1px solid #ddd;
        padding: 10px 12px;
    }

    .btn {
        border-radius: 4px;
        font-weight: 500;
        padding: 10px 16px;
    }

    .oauth-section {
        margin-top: 20px;
    }

    .alert {
        padding: 12px;
        border-radius: 4px;
        margin-bottom: 20px;
    }

    .alert-danger {
        background-color: #f8d7da;
        color: #721c24;
        border: 1px solid #f5c6cb;
    }
</style>

@code {
    private string Email = "";
    private string Password = "";
    private string? ErrorMessage;
    private bool IsLoading = false;

    private async Task HandleLogin()
    {
        IsLoading = true;
        ErrorMessage = null;

        try
        {
            var loginRequest = new { email = Email, password = Password };
            var response = await Http.PostAsJsonAsync("/api/portal/login", loginRequest);

            if (response.IsSuccessStatusCode)
            {
                Navigation.NavigateTo("/dashboard", forceLoad: true);
            }
            else
            {
                ErrorMessage = "로그인 정보를 확인할 수 없습니다.";
            }
        }
        catch
        {
            ErrorMessage = "로그인 중 오류가 발생했습니다.";
        }
        finally
        {
            IsLoading = false;
        }
    }

    private void LoginWithGoogle() => Navigation.NavigateTo("/api/portal/login/google");
    private void LoginWithNaver() => Navigation.NavigateTo("/api/portal/login/naver");
    private void LoginWithKakao() => Navigation.NavigateTo("/api/portal/login/kakao");
}
