namespace SatelliteProvider.IntegrationTests;

public static class ComplexRouteTests
{
    public static async Task RunComplexRouteWithStitching(HttpClient httpClient)
    {
        RouteTestHelpers.PrintTestHeader("Test: Complex Route with 10 Points + 2 Geofences - Region Processing and Stitching");

        var routeId = Guid.NewGuid();
        var request = new CreateRouteRequest
        {
            Id = routeId,
            Name = "Complex Route with 10 Points + 2 Geofences",
            Description = "Test route with 10 action points and 2 geofence regions for complex map stitching",
            RegionSizeMeters = 300.0,
            ZoomLevel = 18,
            RequestMaps = true,
            Points = new List<RoutePointInput>
            {
                new() { Lat = 48.276067180586544, Lon = 37.38445758819581 },
                new() { Lat = 48.27074009522731, Lon = 37.374029159545906 },
                new() { Lat = 48.263312668696855, Lon = 37.37707614898682 },
                new() { Lat = 48.26539817051818, Lon = 37.36587524414063 },
                new() { Lat = 48.25851283439989, Lon = 37.35952377319337 },
                new() { Lat = 48.254426906081555, Lon = 37.374801635742195 },
                new() { Lat = 48.25914140977405, Lon = 37.39068031311036 },
                new() { Lat = 48.25354110233028, Lon = 37.401752471923835 },
                new() { Lat = 48.25902712391726, Lon = 37.416257858276374 },
                new() { Lat = 48.26828345053738, Lon = 37.402009963989265 }
            },
        };

        Console.WriteLine("Step 1: Creating complex route with RequestMaps=true and 2 geofences");
        Console.WriteLine($"  Action Points: {request.Points.Count}");
        Console.WriteLine($"  NO Geofences Regions");
        RouteTestHelpers.PrintRouteCreationDetails(request);
        RouteTestHelpers.PrintRoutePoints(request.Points);

        var route = await RouteTestHelpers.CreateRoute(httpClient, request);

        RouteTestHelpers.PrintRouteResponse(route);
        RouteTestHelpers.PrintPointTypeDistribution(route);

        RouteTestHelpers.ValidatePointTypes(route, 1, 1, 8);
        RouteTestHelpers.ValidateRequestMaps(route, false);

        Console.WriteLine("Step 2: Waiting for complex route maps to be ready");
        Console.WriteLine("  (Processing route point regions + 2 geofence regions SEQUENTIALLY to avoid API throttling)");
        Console.WriteLine("  (This will take several minutes as each region is processed one at a time)");
        Console.WriteLine();

        var finalRoute = await RouteTestHelpers.WaitForRouteReady(httpClient, routeId, 240, 3000);

        Console.WriteLine();
        Console.WriteLine("Step 3: Verifying generated files");

        RouteTestHelpers.ValidateFiles(finalRoute);

        var uniqueTileCount = await RouteTestHelpers.GetUniqueTileCount(finalRoute.CsvFilePath!);
        var stitchedInfo = new FileInfo(finalRoute.StitchedImagePath!);

        RouteTestHelpers.PrintGeneratedFiles(finalRoute, uniqueTileCount);
        RouteTestHelpers.PrintRouteSummary(finalRoute, uniqueTileCount, 2);

        if (uniqueTileCount < 10)
        {
            throw new Exception($"Expected at least 10 unique tiles for complex route with geofences, got {uniqueTileCount}");
        }

        if (stitchedInfo.Length < 1024)
        {
            throw new Exception($"Stitched image seems too small: {stitchedInfo.Length} bytes");
        }

        Console.WriteLine("✓ Complex Route with 10 Points + 2 Geofences Test: PASSED");
    }

    public static async Task RunComplexRouteWithStitchingAndGeofences(HttpClient httpClient)
    {
        RouteTestHelpers.PrintTestHeader("Test: Complex Route with 10 Points + 2 Geofences - Region Processing and Stitching");

        var routeId = Guid.NewGuid();
        var request = new CreateRouteRequest
        {
            Id = routeId,
            Name = "Complex Route with 10 Points + 2 Geofences",
            Description = "Test route with 10 action points and 2 geofence regions for complex map stitching",
            RegionSizeMeters = 300.0,
            ZoomLevel = 18,
            RequestMaps = true,
            Points = new List<RoutePointInput>
            {
                new() { Lat = 48.276067180586544, Lon = 37.38445758819581 },
                new() { Lat = 48.27074009522731, Lon = 37.374029159545906 },
                new() { Lat = 48.263312668696855, Lon = 37.37707614898682 },
                new() { Lat = 48.26539817051818, Lon = 37.36587524414063 },
                new() { Lat = 48.25851283439989, Lon = 37.35952377319337 },
                new() { Lat = 48.254426906081555, Lon = 37.374801635742195 },
                new() { Lat = 48.25914140977405, Lon = 37.39068031311036 },
                new() { Lat = 48.25354110233028, Lon = 37.401752471923835 },
                new() { Lat = 48.25902712391726, Lon = 37.416257858276374 },
                new() { Lat = 48.26828345053738, Lon = 37.402009963989265 }
            },
            Geofences = new GeofencesInput
            {
                Polygons = new List<GeofencePolygonInput>
                {
                    new()
                    {
                        NorthWest = new GeoPointInput { Lat = 48.28022277841604, Lon = 37.37548828125001 },
                        SouthEast = new GeoPointInput { Lat = 48.2720540660028, Lon = 37.3901653289795 }
                    },
                    new()
                    {
                        NorthWest = new GeoPointInput { Lat = 48.2614270732573, Lon = 37.35239982604981 },
                        SouthEast = new GeoPointInput { Lat = 48.24988342757033, Lon = 37.37943649291993 }
                    }
                }
            }
        };

        Console.WriteLine("Step 1: Creating complex route with RequestMaps=true and 2 geofences");
        RouteTestHelpers.PrintRouteCreationDetails(request, true);
        RouteTestHelpers.PrintGeofences(request.Geofences.Polygons);
        RouteTestHelpers.PrintRoutePoints(request.Points);

        var route = await RouteTestHelpers.CreateRoute(httpClient, request);

        RouteTestHelpers.PrintRouteResponse(route);
        RouteTestHelpers.PrintPointTypeDistribution(route);

        RouteTestHelpers.ValidatePointTypes(route, 1, 1, 8);
        RouteTestHelpers.ValidateRequestMaps(route, false);

        Console.WriteLine("Step 2: Waiting for complex route maps to be ready");
        Console.WriteLine("  (Processing route point regions + 2 geofence regions SEQUENTIALLY to avoid API throttling)");
        Console.WriteLine("  (This will take several minutes as each region is processed one at a time)");
        Console.WriteLine();

        var finalRoute = await RouteTestHelpers.WaitForRouteReady(httpClient, routeId, 240, 3000);

        Console.WriteLine();
        Console.WriteLine("Step 3: Verifying generated files");

        RouteTestHelpers.ValidateFiles(finalRoute);

        var uniqueTileCount = await RouteTestHelpers.GetUniqueTileCount(finalRoute.CsvFilePath!);
        var stitchedInfo = new FileInfo(finalRoute.StitchedImagePath!);

        RouteTestHelpers.PrintGeneratedFiles(finalRoute, uniqueTileCount);
        RouteTestHelpers.PrintRouteSummary(finalRoute, uniqueTileCount, 2);

        if (uniqueTileCount < 10)
        {
            throw new Exception($"Expected at least 10 unique tiles for complex route with geofences, got {uniqueTileCount}");
        }

        if (stitchedInfo.Length < 1024)
        {
            throw new Exception($"Stitched image seems too small: {stitchedInfo.Length} bytes");
        }

        Console.WriteLine("✓ Complex Route with 10 Points + 2 Geofences Test: PASSED");
    }
}

