using Microsoft.EntityFrameworkCore;

namespace GeoRitmMover.Services.Implementations.TableMovers;
public class ClearAllTableMover(
	ILogger<ClearAllTableMover> logger,
	IContexts contexts
) : ITableMover
{
	public async Task Move(CancellationToken cancellationToken)
	{
		logger.LogInformation("Clear all tables");

		var eventCodeMaps = await contexts.Middleware.EventCodes
			.Select(x => x.To)
			.ToListAsync(cancellationToken);

		_ = await contexts.Destination.Faults.ExecuteDeleteAsync(cancellationToken);
		_ = await contexts.Destination.Alarms.ExecuteDeleteAsync(cancellationToken);
		_ = await contexts.Destination.Events.ExecuteDeleteAsync(cancellationToken);
		_ = await contexts.Destination.EventCodes
			.Where(x => eventCodeMaps.Contains(x.Id))
			.ExecuteDeleteAsync(cancellationToken);
		_ = await contexts.Destination.UserLogs.ExecuteDeleteAsync(cancellationToken);

		var accessMaps = await contexts.Middleware.Accesses
			.Select(x => x.To)
			.ToListAsync(cancellationToken);
		_ = await contexts.Destination.Accesses
			.Where(x => accessMaps.Contains(x.Id))
			.ExecuteDeleteAsync(cancellationToken);

		var zoneMaps = await contexts.Middleware.Zones
			.Select(x => x.To)
			.ToListAsync(cancellationToken);
		_ = await contexts.Destination.Zones
			.Where(x => zoneMaps.Contains(x.Id))
			.ExecuteDeleteAsync(cancellationToken);

		var areaMaps = await contexts.Middleware.Areas
			.Select(x => x.To)
			.ToListAsync(cancellationToken);
		_ = await contexts.Destination.Areas
			.Where(x => areaMaps.Contains(x.Id))
			.ExecuteDeleteAsync(cancellationToken);

		var photoMaps = await contexts.Middleware.Photos
			.Select(x => x.To)
			.ToListAsync(cancellationToken);
		_ = await contexts.Destination.Photos
			.Where(x => photoMaps.Contains(x.Id))
			.ExecuteDeleteAsync(cancellationToken);

		var assetMaps = await contexts.Middleware.Assets
			.Select(x => x.To)
			.ToListAsync(cancellationToken);
		_ = await contexts.Destination.ResponsiblePersons
			.Where(x => assetMaps.Contains(x.AssetId))
			.ExecuteDeleteAsync(cancellationToken);
		_ = await contexts.Destination.Assets
			.Where(x => assetMaps.Contains(x.Id))
			.ExecuteDeleteAsync(cancellationToken);

		var assetGroupMaps = await contexts.Middleware.Groups
			.Select(x => x.To)
			.ToListAsync(cancellationToken);
		_ = await contexts.Destination.AssetGroups
			.Where(x => assetGroupMaps.Contains(x.Id))
			.ExecuteDeleteAsync(cancellationToken);

		var userMaps = await contexts.Middleware.Users
			.Select(x => x.To)
			.ToListAsync(cancellationToken);
		_ = await contexts.Destination.Members
			.Where(x => userMaps.Contains(x.UserId))
			.ExecuteDeleteAsync(cancellationToken);
		_ = await contexts.Destination.Users
			.Where(x => userMaps.Contains(x.Id))
			.ExecuteDeleteAsync(cancellationToken);

		_ = await contexts.Middleware.Accesses
			.ExecuteDeleteAsync(cancellationToken);
		_ = await contexts.Middleware.Zones
			.ExecuteDeleteAsync(cancellationToken);
		_ = await contexts.Middleware.Areas
			.ExecuteDeleteAsync(cancellationToken);
		_ = await contexts.Middleware.Photos
			.ExecuteDeleteAsync(cancellationToken);
		_ = await contexts.Middleware.Assets
			.ExecuteDeleteAsync(cancellationToken);
		_ = await contexts.Middleware.Groups
			.ExecuteDeleteAsync(cancellationToken);
		_ = await contexts.Middleware.Users
			.ExecuteDeleteAsync(cancellationToken);
		_ = await contexts.Middleware.EventCodes
			.ExecuteDeleteAsync(cancellationToken);

		logger.LogInformation("Done clear all tables");
	}
}
