using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Indotalent.Features.Thirdparty.Vendor.Cqrs;

public class VendorContactItemResponse
{
    public string? Id { get; set; }
    public string? Name { get; set; }
    public string? AutoNumber { get; set; }
    public string? JobTitle { get; set; }
    public string? PhoneNumber { get; set; }
    public string? EmailAddress { get; set; }
    public string? Description { get; set; }
}

public class GetVendorByIdResponse
{
    public string? Id { get; set; }
    public string? AutoNumber { get; set; }
    public string? Name { get; set; }
    public string? Description { get; set; }
    public string? Street { get; set; }
    public string? City { get; set; }
    public string? State { get; set; }
    public string? ZipCode { get; set; }
    public string? Country { get; set; }
    public string? PhoneNumber { get; set; }
    public string? FaxNumber { get; set; }
    public string? EmailAddress { get; set; }
    public string? Website { get; set; }
    public string? WhatsApp { get; set; }
    public string? LinkedIn { get; set; }
    public string? Facebook { get; set; }
    public string? Instagram { get; set; }
    public string? TwitterX { get; set; }
    public string? TikTok { get; set; }
    public string? VendorGroupId { get; set; }
    public string? VendorCategoryId { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
    public List<VendorContactItemResponse> Contacts { get; set; } = new();
}

public record GetVendorByIdQuery(string Id) : IRequest<GetVendorByIdResponse?>;

public class GetVendorByIdHandler : IRequestHandler<GetVendorByIdQuery, GetVendorByIdResponse?>
{
    private readonly AppDbContext _context;

    public GetVendorByIdHandler(AppDbContext context) => _context = context;

    public async Task<GetVendorByIdResponse?> Handle(GetVendorByIdQuery request, CancellationToken cancellationToken)
    {
        return await _context.Vendor
            .AsNoTracking()
            .Include(x => x.VendorContactList)
            .Where(x => x.Id == request.Id)
            .Select(x => new GetVendorByIdResponse
            {
                Id = x.Id,
                AutoNumber = x.AutoNumber,
                Name = x.Name,
                Description = x.Description,
                Street = x.Street,
                City = x.City,
                State = x.State,
                ZipCode = x.ZipCode,
                Country = x.Country,
                PhoneNumber = x.PhoneNumber,
                FaxNumber = x.FaxNumber,
                EmailAddress = x.EmailAddress,
                Website = x.Website,
                WhatsApp = x.WhatsApp,
                LinkedIn = x.LinkedIn,
                Facebook = x.Facebook,
                Instagram = x.Instagram,
                TwitterX = x.TwitterX,
                TikTok = x.TikTok,
                VendorGroupId = x.VendorGroupId,
                VendorCategoryId = x.VendorCategoryId,
                CreatedAt = x.CreatedAt,
                CreatedBy = x.CreatedBy,
                UpdatedAt = x.UpdatedAt,
                UpdatedBy = x.UpdatedBy,
                Contacts = x.VendorContactList
                    .OrderBy(c => c.Name)
                    .Select(c => new VendorContactItemResponse
                    {
                        Id = c.Id,
                        Name = c.Name,
                        AutoNumber = c.AutoNumber,
                        JobTitle = c.JobTitle,
                        PhoneNumber = c.PhoneNumber,
                        EmailAddress = c.EmailAddress,
                        Description = c.Description
                    }).ToList()
            })
            .FirstOrDefaultAsync(cancellationToken);
    }
}