# Copyright 2024-2025 Ping Identity Corporation. All Rights Reserved
#
# This code is to be used exclusively in connection with Ping Identity
# Corporation software or services. Ping Identity Corporation only offers
# such software or services to legal entities who have entered into a
# binding license agreement with Ping Identity Corporation.

# -*- coding: utf-8 -*-

import time

# load tasks used by configuration file
from shared.lib.utils.exception import FailException
from shared.lib.cloud_utils import kubectl
from pyrock.lib.scheduler.tasks.StepTask import StepTask
from pyrock.lib.scheduler.tasks.ScaleTask import ScaleTask
from pyrock.lib.PyRockRun import get_pyrock_run
from pyrock.tasks.deployment.datainit import MakeOauth2ClientsTask
from pyrock.tasks.scenario.gatling import GatlingTask
from pyrock.tasks.idm.general import DumpIDMIDWithAPITask
from pyrock.tests.stress.am.authn_rest.tasks.mytasks import EnablePrometheusMonitoring
from pyrock.tasks.ds.replication import CheckReplicationTask
from pyrock.tasks.deployment.configuration_idm import PrepareWorkloadTask

pyrock_run = get_pyrock_run()


class AddOAuth2ClientIndexes(StepTask):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.commands_to_run = None

    def pre(self):
        if not self.target.component_type == "ds":
            raise FailException("Target must be ds component")

        ds_pwd = self.target.get_product_admin_password()
        self.commands_to_run = [
            # delete the existing index first
            f"""
dsconfig delete-backend-index \
--backend-name cfgStore \
--index-name ou \
--hostname localhost \
--port {self.target.admin_port} \
--bindDn {self.target.admin_user} \
--bindPassword {ds_pwd} \
--trustAll \
--no-prompt""".strip(),
            f"""
dsconfig create-backend-index \
--backend-name cfgStore \
--index-name ou \
--set index-type:equality \
--set index-type:substring \
--hostname localhost \
--port {self.target.admin_port} \
--bindDn {self.target.admin_user} \
--bindPassword {ds_pwd} \
--trustAll \
--no-prompt""".strip(),
            f"""
dsconfig create-backend-index \
--backend-name cfgStore \
--index-name sunserviceID \
--set index-type:big-equality \
--hostname localhost \
--port {self.target.admin_port} \
--bindDn {self.target.admin_user} \
--bindPassword {ds_pwd} \
--trustAll \
--no-prompt""".strip(),
            # finally, rebuild the indexes
            f"""
rebuild-index \
--rebuildAll \
--baseDN ou=am-config \
--hostname localhost \
--port {self.target.admin_port} \
--bindDn {self.target.admin_user} \
--bindPassword {ds_pwd} \
--trustAll \
--no-prompt""".strip(),
        ]

    def step1(self):
        for cmd in self.commands_to_run:
            pyrock_run.log(f"\nexecuting command: {cmd}")
            for pod in self.target.pods:
                pyrock_run.log(f"- pod: {pod.name}")
                kubectl(f"exec {pod.name} -c ds -- {cmd}", namespace=self.target.namespace, component=self.target)
            time.sleep(2)
