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

namespace Application.Features.Director.LoginLog.Search;

public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
{
    public async Task<Response> Handle(Query request, CancellationToken ct)
    {
        var query = db.AdminLoginLog.AsNoTracking().AsQueryable();

        // 키워드 검색
        if (!string.IsNullOrWhiteSpace(request.Keyword))
        {
            var kw = request.Keyword.Trim();
            query = request.Search switch
            {
                1 => query.Where(x => x.Account.Contains(kw)),
                2 => query.Where(x => x.IpAddress != null && x.IpAddress.Contains(kw)),
                _ => query.Where(x => x.Account.Contains(kw))
            };
        }

        // 성공/실패 필터
        if (request.Success.HasValue)
        {
            query = query.Where(x => x.Success == request.Success.Value);
        }

        // 날짜 필터
        if (!string.IsNullOrWhiteSpace(request.StartAt) && DateTime.TryParse(request.StartAt, out var startAt))
        {
            query = query.Where(x => x.CreatedAt >= startAt);
        }

        if (!string.IsNullOrWhiteSpace(request.EndAt) && DateTime.TryParse(request.EndAt, out var endAt))
        {
            query = query.Where(x => x.CreatedAt <= endAt);
        }

        var total = await query.CountAsync(ct);
        var skip = (request.PageNum - 1) * request.PerPage;

        var list = await query
            .OrderByDescending(x => x.ID)
            .Skip(skip)
            .Take(request.PerPage)
            .Select(x => new
            {
                x.ID,
                x.Account,
                x.Success,
                x.Reason,
                x.IpAddress,
                x.UserAgent,
                x.CreatedAt
            })
            .ToListAsync(ct);

        var rows = list.Select((x, idx) => new Response.Row
        {
            Num = total - skip - idx,
            ID = x.ID,
            Account = x.Account,
            Success = x.Success,
            Reason = x.Reason,
            IpAddress = x.IpAddress,
            UserAgent = x.UserAgent,
            Browser = ParseUserAgent(x.UserAgent, "browser"),
            OS = ParseUserAgent(x.UserAgent, "os"),
            Device = ParseUserAgent(x.UserAgent, "device"),
            CreatedAt = x.CreatedAt
        }).ToList();

        return new Response
        {
            Total = total,
            List = rows
        };
    }

    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";
    }
}