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

namespace Indotalent.Features.Pipeline.LeadContact.Cqrs;

public class UpdateLeadContactRequest
{
    public string? Id { get; set; }
    public string? LeadId { get; set; }
    public string? FullName { get; set; }
    public string? Description { get; set; }
    public string? AddressStreet { get; set; }
    public string? AddressCity { get; set; }
    public string? AddressState { get; set; }
    public string? AddressZipCode { get; set; }
    public string? AddressCountry { get; set; }
    public string? PhoneNumber { get; set; }
    public string? FaxNumber { get; set; }
    public string? MobileNumber { get; set; }
    public string? Email { get; set; }
    public string? Website { get; set; }
    public string? WhatsApp { get; set; }
    public string? LinkedIn { get; set; }
    public string? Facebook { get; set; }
    public string? Twitter { get; set; }
    public string? Instagram { get; set; }
    public string? AvatarName { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

public class UpdateLeadContactResponse
{
    public string? Id { get; set; }
    public bool Success { get; set; }
}

public record UpdateLeadContactCommand(UpdateLeadContactRequest Data) : IRequest<UpdateLeadContactResponse>;

public class UpdateLeadContactHandler : IRequestHandler<UpdateLeadContactCommand, UpdateLeadContactResponse>
{
    private readonly AppDbContext _context;

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

    public async Task<UpdateLeadContactResponse> Handle(UpdateLeadContactCommand request, CancellationToken cancellationToken)
    {
        var entity = await _context.LeadContact
            .FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);

        if (entity == null)
        {
            return new UpdateLeadContactResponse { Id = request.Data.Id, Success = false };
        }

        entity.LeadId = request.Data.LeadId;
        entity.FullName = request.Data.FullName;
        entity.Description = request.Data.Description;
        entity.AddressStreet = request.Data.AddressStreet;
        entity.AddressCity = request.Data.AddressCity;
        entity.AddressState = request.Data.AddressState;
        entity.AddressZipCode = request.Data.AddressZipCode;
        entity.AddressCountry = request.Data.AddressCountry;
        entity.PhoneNumber = request.Data.PhoneNumber;
        entity.FaxNumber = request.Data.FaxNumber;
        entity.MobileNumber = request.Data.MobileNumber;
        entity.Email = request.Data.Email;
        entity.Website = request.Data.Website;
        entity.WhatsApp = request.Data.WhatsApp;
        entity.LinkedIn = request.Data.LinkedIn;
        entity.Facebook = request.Data.Facebook;
        entity.Twitter = request.Data.Twitter;
        entity.Instagram = request.Data.Instagram;
        entity.AvatarName = request.Data.AvatarName;

        await _context.SaveChangesAsync(cancellationToken);

        return new UpdateLeadContactResponse
        {
            Id = entity.Id,
            Success = true
        };
    }
}