using e_suite.API.Common.repository;
using e_suite.Database.Audit;
using e_suite.UnitTestCore;
using MockQueryable;

namespace e_suite.Modules.CustomFieldManagerUnitTests;

public class FakeCustomFieldRepository : FakeRepository, ICustomFieldRepository
{
    public List<CustomField> CustomFields { get; set; } = [];
    
    public Task CreateAsync(AuditUserDetails auditUserDetails, CustomField customField, CancellationToken cancellationToken)
    {
        CustomFields.Add(customField);
        customField.Id = CustomFields.Max(x => x.Id) + 1;
        return Task.CompletedTask;
    }

    public IQueryable<CustomField> GetCustomFieldList()
    {
        return CustomFields.Where(x => !x.Deleted).ToList().BuildMock();
    }

    public Task EditAsync(AuditUserDetails auditUserDetails, CustomField customField, CancellationToken cancellationToken)
    {
        var customFieldReal = CustomFields.Single(x => x.Guid == customField.Guid || x.Id == customField.Id);
        customFieldReal.Name = customField.Name;
        customFieldReal.DefaultValue = customField.DefaultValue;
        customFieldReal.FieldType = customField.FieldType;
        customFieldReal.Deleted = customField.Deleted;
        return Task.CompletedTask;
    }

    public Task<CustomField?> GetByIdAsync(IGeneralIdRef id, CancellationToken cancellationToken)
    {
        return Task.FromResult(CustomFields.FirstOrDefault(x => x.Id == id.Id || x.Guid == id.Guid));
    }

    public Task<CustomField?> GetByNameAsync(string name, CancellationToken cancellationToken)
    {
        return Task.FromResult(CustomFields.FirstOrDefault(x => x.Name == name));
    }
}
