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

namespace Indotalent.Features.Thirdparty.Customer.Cqrs;

public class UpdateCustomerRequest
{
    public string? Id { 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? CustomerGroupId { get; set; }
    public string? CustomerCategoryId { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public string? UpdatedBy { get; set; }
}

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

public record UpdateCustomerCommand(UpdateCustomerRequest Data) : IRequest<UpdateCustomerResponse>;

public class UpdateCustomerHandler : IRequestHandler<UpdateCustomerCommand, UpdateCustomerResponse>
{
    private readonly AppDbContext _context;

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

    public async Task<UpdateCustomerResponse> Handle(UpdateCustomerCommand request, CancellationToken cancellationToken)
    {
        var isExists = await _context.Customer
            .AnyAsync(x => x.Name == request.Data.Name && x.Id != request.Data.Id, cancellationToken);

        if (isExists)
        {
            throw new AlreadyExistsException("Customer", request.Data.Name ?? string.Empty);
        }

        var entity = await _context.Customer
            .FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);

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

        entity.Name = request.Data.Name;
        entity.Description = request.Data.Description;
        entity.Street = request.Data.Street;
        entity.City = request.Data.City;
        entity.State = request.Data.State;
        entity.ZipCode = request.Data.ZipCode;
        entity.Country = request.Data.Country;
        entity.PhoneNumber = request.Data.PhoneNumber;
        entity.FaxNumber = request.Data.FaxNumber;
        entity.EmailAddress = request.Data.EmailAddress;
        entity.Website = request.Data.Website;
        entity.WhatsApp = request.Data.WhatsApp;
        entity.LinkedIn = request.Data.LinkedIn;
        entity.Facebook = request.Data.Facebook;
        entity.Instagram = request.Data.Instagram;
        entity.TwitterX = request.Data.TwitterX;
        entity.TikTok = request.Data.TikTok;
        entity.CustomerGroupId = request.Data.CustomerGroupId;
        entity.CustomerCategoryId = request.Data.CustomerCategoryId;

        await _context.SaveChangesAsync(cancellationToken);

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