using e_suite.API.Common.repository;
using e_suite.Database.Audit;
using e_suite.Database.Core.Extensions;
using e_suite.Database.Core.Tables.Printer;
using e_suite.UnitTestCore;
using eSuite.Core.Miscellaneous;
using MockQueryable;

namespace e_suite.Modules.OrganisationManager.UnitTests.Repository;

public class FakeOrganisationManagerRepository : FakeRepository, IOrganisationsManagerRepository
{
    public List<Organisation> Organisations = [];
    public List<Site> Sites = [];
    public List<Specification> Specifications = [];

    public FakeOrganisationManagerRepository()
    {

    }

    public IQueryable<Organisation> GetOrganisationsList()
    {
        return Organisations.Where(x => x.Deleted == false).ToList().BuildMock();
    }

    public Task AddOrganisation(AuditUserDetails auditUserDetails, Organisation organisation, CancellationToken cancellationToken)
    {
        Organisations.Add(organisation);
        return Task.CompletedTask;
    }

    public Task<Organisation?> FindOrganisation(IGeneralIdRef generalIdRef, CancellationToken cancellationToken)
    {
        return Task.FromResult(Organisations.FindByGeneralIdRef(generalIdRef));
    }
        
    public Task DeleteOrganisation(AuditUserDetails auditUserDetails, IGeneralIdRef generalIdRef, CancellationToken cancellationToken)
    {
        var organisation = Organisations.FindByGeneralIdRef(generalIdRef);

        if (organisation != null)
        {
            organisation.Deleted = true;
            var sites = Sites.Where(x => x.OrganisationId == organisation.Id).ToList();

            foreach (var site in sites)
            {
                site.Deleted = true;

                var specs = Specifications.Where(x => x.SiteId == site.Id).ToList();
                specs.ForEach(x => x.Deleted = true);
            }
        }

        return Task.CompletedTask;
    }

    public Task EditOrganisation(AuditUserDetails auditUserDetails, Organisation organisation, CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }
}