using ESuite.UI.E2E.Helpers;

namespace ESuite.UI.E2E.Hooks
{

    [Binding]
    public class ScenarioHooks
    {
        private readonly ScenarioContext _scenarioContext;
        private readonly APIHelper _apiHelper;

        public ScenarioHooks(ScenarioContext scenarioContext)
        {
            _scenarioContext = scenarioContext;
            _apiHelper = new APIHelper(_scenarioContext);

        }

        [BeforeScenario(Order = 2)]
        public void SetGUIDs()
        {
            string uniqueGuid = Guid.NewGuid().ToString().TrimStart('{').TrimEnd('}');
            _scenarioContext["GUID"] = uniqueGuid;
            _scenarioContext["GUIDPrefix"] = $"{uniqueGuid}-";
            _scenarioContext["GUIDPostfix"] = $"-{uniqueGuid}";
        }

        [BeforeScenario]
        public void CleanAuthToken()
        {
            _apiHelper.ClearAuthToken();
        }

        [BeforeStep]
        [AfterStep]
        public static void RetryFailedStep(ScenarioContext scenarioContext)
        {
            if (scenarioContext.TestError != null)
            {
                RetryHelper.Retry(() =>
                {
                    Console.WriteLine("Retrying failed step...");
                });
            }
        }
    }
}
