using Application.Abstractions.Messaging;
using Application.Abstractions.Data;
using Microsoft.EntityFrameworkCore;

namespace Application.Features.Admin.Member.Logs.Login.Get;

public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response?>
{
    public async Task<Response?> Handle(Query request, CancellationToken ct)
    {
        var entity = await db.MemberLoginLog
            .AsNoTracking()
            .Include(x => x.Member)
            .Where(x => x.ID == request.Id)
            .Select(x => new
            {
                x.ID,
                x.MemberID,
                MemberName = x.Member.Name,
                x.Account,
                x.Success,
                x.Reason,
                x.Referer,
                x.Url,
                x.IpAddress,
                x.UserAgent,
                x.CreatedAt
            })
            .FirstOrDefaultAsync(ct);

        if (entity == null) return null;

        return new Response
        {
            ID = entity.ID,
            MemberID = entity.MemberID,
            MemberName = entity.MemberName,
            Account = entity.Account,
            Success = entity.Success,
            Reason = entity.Reason,
            Referer = entity.Referer,
            Url = entity.Url,
            IpAddress = entity.IpAddress,
            UserAgent = entity.UserAgent,
            Browser = ParseUserAgent(entity.UserAgent, "browser"),
            OS = ParseUserAgent(entity.UserAgent, "os"),
            Device = ParseUserAgent(entity.UserAgent, "device"),
            CreatedAt = entity.CreatedAt
        };
    }

    private static string? ParseUserAgent(string? userAgent, string type)
    {
        if (string.IsNullOrWhiteSpace(userAgent)) return null;

        return type switch
        {
            "browser" => ExtractBrowser(userAgent),
            "os" => ExtractOS(userAgent),
            "device" => ExtractDevice(userAgent),
            _ => null
        };
    }

    private static string ExtractBrowser(string ua)
    {
        if (ua.Contains("Edg/")) return "Edge";
        if (ua.Contains("Chrome/")) return "Chrome";
        if (ua.Contains("Firefox/")) return "Firefox";
        if (ua.Contains("Safari/") && !ua.Contains("Chrome")) return "Safari";
        if (ua.Contains("MSIE") || ua.Contains("Trident/")) return "IE";
        return "Unknown";
    }

    private static string ExtractOS(string ua)
    {
        if (ua.Contains("Windows NT 10")) return "Windows 10";
        if (ua.Contains("Windows NT 6.3")) return "Windows 8.1";
        if (ua.Contains("Windows NT 6.1")) return "Windows 7";
        if (ua.Contains("Windows")) return "Windows";
        if (ua.Contains("Mac OS X")) return "macOS";
        if (ua.Contains("Android")) return "Android";
        if (ua.Contains("iPhone") || ua.Contains("iPad")) return "iOS";
        if (ua.Contains("Linux")) return "Linux";
        return "Unknown";
    }

    private static string ExtractDevice(string ua)
    {
        if (ua.Contains("Mobile") || ua.Contains("Android") && !ua.Contains("Tablet")) return "Mobile";
        if (ua.Contains("Tablet") || ua.Contains("iPad")) return "Tablet";
        return "Desktop";
    }
}
