using Application.Abstractions.Data;
using Application.Abstractions.Cache;
using Application.Abstractions.Messaging;
using Application.Helpers;
using SharedKernel.Results;
using Microsoft.EntityFrameworkCore;

namespace Application.Features.Api.MyPage.ChangePassword;

internal sealed class Handler(
    IAppDbContext db,
    ICacheService cache
) : ICommandHandler<Command, Result>
{
    public async Task<Result> Handle(Command request, CancellationToken ct)
    {
        if (string.IsNullOrWhiteSpace(request.CurrentPassword))
        {
            return Result.Failure(Error.Problem("MyPage.CurrentPasswordRequired", "현재 비밀번호는 필수입니다."));
        }

        if (string.IsNullOrWhiteSpace(request.NewPassword))
        {
            return Result.Failure(Error.Problem("MyPage.NewPasswordRequired", "새 비밀번호는 필수입니다."));
        }

        if (request.NewPassword != request.ConfirmPassword)
        {
            return Result.Failure(Error.Problem("MyPage.PasswordMismatch", "새 비밀번호가 일치하지 않습니다."));
        }

        // Config 로드 + 비밀번호 복잡도 검증
        var accountConfig = await AccountConfigLoader.GetAccountConfigAsync(cache, db, ct);
        var passwordResult = PasswordPolicyValidator.Validate(request.NewPassword, accountConfig);
        if (!passwordResult.IsSuccess)
        {
            return passwordResult;
        }

        var member = await db.Member.FirstOrDefaultAsync(m => m.ID == request.MemberID, ct);
        if (member is null)
        {
            return Result.Failure(Error.NotFound("MyPage.MemberNotFound", "회원 정보를 찾을 수 없습니다."));
        }

        // 비밀번호 변경 주기 확인
        if (accountConfig.ChangePasswordDay is > 0)
        {
            var nextChangeDate = member.PasswordUpdatedAt.AddDays(accountConfig.ChangePasswordDay.Value);
            if (DateTime.UtcNow < nextChangeDate)
            {
                return Result.Failure(Error.Problem("MyPage.PasswordChangeTooSoon",
                    $"비밀번호는 {accountConfig.ChangePasswordDay}일마다 변경 가능합니다."));
            }
        }

        if (!member.VerifyPassword(request.CurrentPassword))
        {
            return Result.Failure(Error.Problem("MyPage.InvalidPassword", "현재 비밀번호가 올바르지 않습니다."));
        }

        member.SetPassword(request.NewPassword);

        await db.SaveChangesAsync(ct);

        return Result.Success();
    }
}
