using Microsoft.EntityFrameworkCore;
using WorkClub.Application.Interfaces;
using WorkClub.Application.Members.DTOs;
using WorkClub.Infrastructure.Data;

namespace WorkClub.Api.Services;

public class MemberService
{
    private readonly AppDbContext _context;
    private readonly ITenantProvider _tenantProvider;
    private readonly IHttpContextAccessor _httpContextAccessor;

    public MemberService(
        AppDbContext context,
        ITenantProvider tenantProvider,
        IHttpContextAccessor httpContextAccessor)
    {
        _context = context;
        _tenantProvider = tenantProvider;
        _httpContextAccessor = httpContextAccessor;
    }

    public async Task<List<MemberListDto>> GetMembersAsync()
    {
        var members = await _context.Members.ToListAsync();

        return members.Select(m => new MemberListDto(
            m.Id,
            m.DisplayName,
            m.Email,
            m.Role.ToString()
        )).ToList();
    }

    public async Task<MemberDetailDto?> GetMemberByIdAsync(Guid id)
    {
        var member = await _context.Members.FindAsync(id);

        if (member == null)
            return null;

        return new MemberDetailDto(
            member.Id,
            member.DisplayName,
            member.Email,
            member.Role.ToString(),
            member.ClubId,
            member.CreatedAt,
            member.UpdatedAt
        );
    }

    public async Task<MemberDetailDto?> GetCurrentMemberAsync()
    {
        var userIdClaim = _httpContextAccessor.HttpContext?.User.FindFirst("sub")?.Value;
        if (string.IsNullOrEmpty(userIdClaim))
        {
            return null;
        }

        var tenantId = _tenantProvider.GetTenantId();

        var member = await _context.Members
            .FirstOrDefaultAsync(m => m.ExternalUserId == userIdClaim && m.TenantId == tenantId);

        if (member == null)
            return null;

        return new MemberDetailDto(
            member.Id,
            member.DisplayName,
            member.Email,
            member.Role.ToString(),
            member.ClubId,
            member.CreatedAt,
            member.UpdatedAt
        );
    }
}
