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

namespace Indotalent.Features.Pipeline.LeadContact.Cqrs;

public class CreateLeadContactRequest
{
    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 class CreateLeadContactResponse
{
    public string? Id { get; set; }
    public string? FullName { get; set; }
}

public record CreateLeadContactCommand(CreateLeadContactRequest Data) : IRequest<CreateLeadContactResponse>;

public class CreateLeadContactHandler : IRequestHandler<CreateLeadContactCommand, CreateLeadContactResponse>
{
    private readonly AppDbContext _context;

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

    public async Task<CreateLeadContactResponse> Handle(CreateLeadContactCommand request, CancellationToken cancellationToken)
    {
        var entityName = nameof(Data.Entities.LeadContact);

        var autoNo = await _context.GenerateAutoNumberAsync(
            entityName: entityName,
            prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/",
            ct: cancellationToken
        );

        var entity = new Data.Entities.LeadContact
        {
            AutoNumber = autoNo,
            LeadId = request.Data.LeadId,
            FullName = request.Data.FullName,
            Description = request.Data.Description,
            AddressStreet = request.Data.AddressStreet,
            AddressCity = request.Data.AddressCity,
            AddressState = request.Data.AddressState,
            AddressZipCode = request.Data.AddressZipCode,
            AddressCountry = request.Data.AddressCountry,
            PhoneNumber = request.Data.PhoneNumber,
            FaxNumber = request.Data.FaxNumber,
            MobileNumber = request.Data.MobileNumber,
            Email = request.Data.Email,
            Website = request.Data.Website,
            WhatsApp = request.Data.WhatsApp,
            LinkedIn = request.Data.LinkedIn,
            Facebook = request.Data.Facebook,
            Twitter = request.Data.Twitter,
            Instagram = request.Data.Instagram,
            AvatarName = request.Data.AvatarName
        };

        _context.LeadContact.Add(entity);
        await _context.SaveChangesAsync(cancellationToken);

        return new CreateLeadContactResponse
        {
            Id = entity.Id,
            FullName = entity.FullName
        };
    }
}