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

namespace Indotalent.Features.Thirdparty.Vendor.Cqrs;

public class CreateVendorRequest
{
    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 class CreateVendorResponse
{
    public string? Id { get; set; }
    public string? Name { get; set; }
}

public record CreateVendorCommand(CreateVendorRequest Data) : IRequest<CreateVendorResponse>;

public class CreateVendorHandler : IRequestHandler<CreateVendorCommand, CreateVendorResponse>
{
    private readonly AppDbContext _context;

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

    public async Task<CreateVendorResponse> Handle(CreateVendorCommand request, CancellationToken cancellationToken)
    {
        var isExists = await _context.Vendor
            .AnyAsync(x => x.Name == request.Data.Name, cancellationToken);

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

        var entityName = nameof(Data.Entities.Vendor);

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

        var entity = new Data.Entities.Vendor
        {
            AutoNumber = autoNo,
            Name = request.Data.Name,
            Description = request.Data.Description,
            Street = request.Data.Street,
            City = request.Data.City,
            State = request.Data.State,
            ZipCode = request.Data.ZipCode,
            Country = request.Data.Country,
            PhoneNumber = request.Data.PhoneNumber,
            FaxNumber = request.Data.FaxNumber,
            EmailAddress = request.Data.EmailAddress,
            Website = request.Data.Website,
            WhatsApp = request.Data.WhatsApp,
            LinkedIn = request.Data.LinkedIn,
            Facebook = request.Data.Facebook,
            Instagram = request.Data.Instagram,
            TwitterX = request.Data.TwitterX,
            TikTok = request.Data.TikTok,
            VendorGroupId = request.Data.VendorGroupId,
            VendorCategoryId = request.Data.VendorCategoryId
        };

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

        return new CreateVendorResponse
        {
            Id = entity.Id,
            Name = entity.Name
        };
    }
}