using e_suite.API.Common.repository;
using e_suite.Database.Core;
using e_suite.Database.Core.Extensions;
using e_suite.Database.Core.Tables.CustomFields;
using e_suite.Database.Core.Tables.Domain;
using e_suite.Database.Core.Tables.Forms;
using e_suite.Database.Core.Tables.Glossaries;
using eSuite.Core.Miscellaneous;
using Microsoft.EntityFrameworkCore;

namespace e_suite.Service.CustomFieldValidation.Repository;

public class CustomFieldValidatorRepository : RepositoryBase, ICustomFieldValidatorRepository
{
    public CustomFieldValidatorRepository(IEsuiteDatabaseDbContext databaseDbContext) : base(databaseDbContext)
    {
    }

    public async Task<long> FindSequenceIdForCustomField(long customFieldId, CancellationToken cancellationToken)
    {
        var item = await DatabaseDbContext.CustomFieldSequences.SingleAsync(x => x.CustomFieldId == customFieldId, cancellationToken);

        return item.SequenceId;
    }

    public async Task<FormTemplate?> GetTemplateByGeneralRefId(GeneralIdRef generalIdRef, CancellationToken cancellationToken)
    {
        return await DatabaseDbContext.FormTemplates.FindByGeneralIdRefAsync(generalIdRef, cancellationToken);
    }

    public async Task<Glossary?> GetGlossaryByGeneralRefId(GeneralIdRef generalIdRef, CancellationToken cancellationToken)
    {
        return await DatabaseDbContext.Glossaries.FindByGeneralIdRefAsync(generalIdRef, cancellationToken);
    }

    public async Task<Domain?> GetDomainByGeneralRefId(GeneralIdRef generalIdRef, CancellationToken cancellationToken)
    {
        return await DatabaseDbContext.Domains.FindByGeneralIdRefAsync(generalIdRef, cancellationToken);
    }

    public async Task<CustomFieldNumber> GetCustomFieldNumbersAsync(long fieldId, CancellationToken cancellationToken)
    {
        return await DatabaseDbContext.CustomFieldNumber.SingleAsync(x => x.CustomFieldId == fieldId, cancellationToken);
    }

    public async Task<CustomFieldText> GetCustomFieldTextsAsync(long fieldId, CancellationToken cancellationToken)
    {
        return await DatabaseDbContext.CustomFieldText.SingleAsync(x => x.CustomFieldId == fieldId, cancellationToken);
    }
}