using GraphQL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BodyshopPartner.Utils.Queries
{
    public static class VehicleQueries
    {
        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        public static async Task<string> GetVehicleUuidByVin(string vin)
        {
            if (string.IsNullOrEmpty(vin))
                return null;

            var r = new GraphQLRequest
            {
                Query = @"query QUERY_VEHICLE_BY_VIN($vin: String!) {
                                vehicles(where: {v_vin: {_eq: $vin}}) {
                                    id
                                }
                            }",
                Variables = new
                {
                    vin = vin
                }
            };

            try
            {
                var d = await Utils.GraphQL.ExecuteQuery(r);
                return d.vehicles?[0]?.id?.Value;

            }
            catch(ArgumentOutOfRangeException Ex)
            {
                logger.Trace(Ex,"No vehicles returned in GQL query.");
                return "";
            }
            catch (Exception Ex)
            {
                logger.Error(Ex, "Querying for vehicle by VIN failed.");
                return null;
            }
        }
    }
}
