/*
 * Copyright (c) 2025-2026, WSO2 LLC. (http://www.wso2.com).
 *
 * WSO2 LLC. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.wso2.identity.integration.test.rest.api.server.flow.execution.v1;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.identity.integration.test.rest.api.server.flow.execution.v1.model.FlowExecutionRequest;
import org.wso2.identity.integration.test.rest.api.server.flow.execution.v1.model.FlowExecutionResponse;
import org.wso2.identity.integration.test.rest.api.server.flow.management.v1.model.Error;
import org.wso2.identity.integration.test.rest.api.user.common.model.Email;
import org.wso2.identity.integration.test.rest.api.user.common.model.Name;
import org.wso2.identity.integration.test.rest.api.user.common.model.UserObject;
import org.wso2.identity.integration.test.restclients.FlowManagementClient;
import org.wso2.identity.integration.test.restclients.IdentityGovernanceRestClient;
import org.wso2.identity.integration.test.restclients.FlowExecutionClient;
import org.wso2.identity.integration.test.restclients.SCIM2RestClient;

import java.util.HashMap;
import java.util.Map;

import static org.wso2.identity.integration.test.rest.api.server.flow.execution.v1.FlowExecutionTestBase.FlowTypes.REGISTRATION;

/**
 * Test class for Flow Execution API.
 */
public class FlowExecutionNegativeTest extends FlowExecutionTestBase {

    private static final String ACTION_ID = "button_5zqc";
    public static final String USER = "RegExecNegTestUser";
    private static final String SECONDARY_TEST_USER = "RegExecNegSecondaryUser";
    private static final String SECONDARY_TEST_USER_EMAIL = "reg.exec.neg.dup@example.com";
    private static final String SECONDARY_TEST_USER_PASSWORD = "Wso2@Test1";
    private FlowExecutionClient flowExecutionClient;
    private FlowManagementClient flowManagementClient;
    private IdentityGovernanceRestClient identityGovernanceRestClient;
    private SCIM2RestClient scim2RestClient;
    private static String flowId;
    private String secondaryTestUserId;

    @DataProvider(name = "restAPIUserConfigProvider")
    public static Object[][] restAPIUserConfigProvider() {

        return new Object[][]{
                {TestUserMode.SUPER_TENANT_ADMIN},
                {TestUserMode.TENANT_ADMIN}
        };
    }

    @Factory(dataProvider = "restAPIUserConfigProvider")
    public FlowExecutionNegativeTest(TestUserMode userMode) throws Exception {

        super.init(userMode);
        this.context = isServer;
        this.authenticatingUserName = context.getContextTenant().getTenantAdmin().getUserName();
        this.authenticatingCredential = context.getContextTenant().getTenantAdmin().getPassword();
        this.tenant = context.getContextTenant().getDomain();
    }

    @BeforeClass(alwaysRun = true)
    public void init() throws Exception {

        super.testInit(API_VERSION, swaggerDefinition, tenantInfo.getDomain());
        flowExecutionClient = new FlowExecutionClient(serverURL, tenantInfo);
        flowManagementClient = new FlowManagementClient(serverURL, tenantInfo);
        identityGovernanceRestClient = new IdentityGovernanceRestClient(serverURL, tenantInfo);
        scim2RestClient = new SCIM2RestClient(serverURL, tenantInfo);
        addRegistrationFlow(flowManagementClient);

        UserObject secondaryUser = new UserObject()
                .userName(SECONDARY_TEST_USER)
                .password(SECONDARY_TEST_USER_PASSWORD)
                .name(new Name().givenName("Secondary").familyName("User"))
                .addEmail(new Email().value(SECONDARY_TEST_USER_EMAIL));
        secondaryTestUserId = scim2RestClient.createUser(secondaryUser);
    }

    @AfterClass(alwaysRun = true)
    public void testConclude() throws Exception {

        super.conclude();
        disableFlow(REGISTRATION, flowManagementClient);
        if (secondaryTestUserId != null) {
            scim2RestClient.deleteUser(secondaryTestUserId);
        }
        identityGovernanceRestClient.closeHttpClient();
        flowManagementClient.closeHttpClient();
        flowExecutionClient.closeHttpClient();
        scim2RestClient.closeHttpClient();
    }

    @Test
    public void initiateRegistrationFlowWithoutEnable() throws Exception {

        Object responseObj = flowExecutionClient.initiateFlowExecution(REGISTRATION);
        Assert.assertTrue(responseObj instanceof Error);
        Error error = (Error) responseObj;
        Assert.assertNotNull(error);
        Assert.assertNotNull(error.getCode());
        Assert.assertEquals(error.getCode(), "FE-60101");
    }

    @Test(dependsOnMethods = "initiateRegistrationFlowWithoutEnable")
    public void testExecuteFlowWithoutEnable() throws Exception {

        Object responseObj = flowExecutionClient.executeFlow(getFlowExecutionRequest(flowId));
        Assert.assertTrue(responseObj instanceof Error);
        Error error = (Error) responseObj;
        Assert.assertNotNull(error);
        Assert.assertNotNull(error.getCode());
        Assert.assertEquals(error.getCode(), "FE-60001");
    }

    @Test(dependsOnMethods = "testExecuteFlowWithoutEnable")
    public void testInitiateFlow() throws Exception {

        enableFlow(REGISTRATION, flowManagementClient);
        Object responseObj = flowExecutionClient.initiateFlowExecution(REGISTRATION);
        Assert.assertTrue(responseObj instanceof FlowExecutionResponse);
        FlowExecutionResponse response = (FlowExecutionResponse) responseObj;
        Assert.assertNotNull(response);
        Assert.assertNotNull(response.getFlowId());
        flowId = response.getFlowId();
        Assert.assertEquals(response.getFlowStatus(), STATUS_INCOMPLETE);
        Assert.assertEquals(response.getType().toString(), TYPE_VIEW);
        Assert.assertNotNull(response.getData());
        Assert.assertNotNull(response.getData().getComponents());
        Assert.assertEquals(response.getData().getComponents().size(), 2);
    }

    @Test(dependsOnMethods = "testInitiateFlow")
    public void testExecuteFlowWithInvalidFlowId() throws Exception {

        Object responseObj = flowExecutionClient.executeFlow(getFlowExecutionRequest(
                "INVALID_FLOW_ID"));
        Assert.assertTrue(responseObj instanceof Error);
        Error error = (Error) responseObj;
        Assert.assertNotNull(error);
        Assert.assertNotNull(error.getCode());
        Assert.assertEquals(error.getCode(), "FE-60001");
    }

    @Test(dependsOnMethods = "testExecuteFlowWithInvalidFlowId")
    public void testExecuteFlowWithEmptyInputs() throws Exception {

        Map<String, String> inputs = new HashMap<>();
        Object responseObj = flowExecutionClient
                .executeFlow(getFlowExecutionRequest(flowId, inputs));
        Assert.assertTrue(responseObj instanceof Error);
        Error error = (Error) responseObj;
        Assert.assertNotNull(error);
        Assert.assertNotNull(error.getCode());
        Assert.assertEquals(error.getCode(), "FE-60008");
    }

    private void initiateFlow() throws Exception {

        enableFlow(REGISTRATION, flowManagementClient);
        Object responseObj = flowExecutionClient.initiateFlowExecution(REGISTRATION);
        Assert.assertTrue(responseObj instanceof FlowExecutionResponse);
        FlowExecutionResponse response = (FlowExecutionResponse) responseObj;
        Assert.assertNotNull(response);
        Assert.assertNotNull(response.getFlowId());
        flowId = response.getFlowId();
        Assert.assertEquals(response.getFlowStatus(), STATUS_INCOMPLETE);
        Assert.assertEquals(response.getType().toString(), TYPE_VIEW);
        Assert.assertNotNull(response.getData());
        Assert.assertNotNull(response.getData().getComponents());
        Assert.assertEquals(response.getData().getComponents().size(), 2);
    }

    @Test(dependsOnMethods = "testExecuteFlowWithEmptyInputs")
    public void testExecuteFlowWithInvalidInputs() throws Exception {

        initiateFlow();
        Map<String, String> inputs = new HashMap<>();
        inputs.put("http://wso2.org/claims/username", "");
        inputs.put("password", "test@test.com");
        inputs.put("http://wso2.org/claims/emailaddress", "test@test.com");
        inputs.put("http://wso2.org/claims/givenname", "John");
        inputs.put("http://wso2.org/claims/lastname", "Doe");
        Object responseObj = flowExecutionClient
                .executeFlow(getFlowExecutionRequest(flowId, inputs));
        Assert.assertTrue(responseObj instanceof Error);
        Error error = (Error) responseObj;
        Assert.assertNotNull(error);
        Assert.assertNotNull(error.getCode());
        Assert.assertEquals(error.getCode(), "FE-60008");
    }

    @Test(dependsOnMethods = "testExecuteFlowWithInvalidInputs")
    public void testExecuteFlowWithDuplicateUsername() throws Exception {

        initiateFlow();
        Map<String, String> inputs = new HashMap<>();
        inputs.put("http://wso2.org/claims/username", SECONDARY_TEST_USER);
        inputs.put("password", "Wso2@Test2");
        inputs.put("http://wso2.org/claims/emailaddress", "another.dup@example.com");
        inputs.put("http://wso2.org/claims/givenname", "Another");
        inputs.put("http://wso2.org/claims/lastname", "User");
        Object responseObj = flowExecutionClient.executeFlow(getFlowExecutionRequest(flowId, inputs));
        Assert.assertTrue(responseObj instanceof FlowExecutionResponse,
                "Expected FlowExecutionResponse for duplicate username but got: " +
                        (responseObj != null ? responseObj.getClass().getName() : "null"));
        FlowExecutionResponse response = (FlowExecutionResponse) responseObj;
        Assert.assertNotNull(response);
        Assert.assertEquals(response.getFlowStatus(), STATUS_INCOMPLETE,
                "Expected INCOMPLETE status when duplicate username is submitted.");
        Assert.assertEquals(response.getType().toString(), TYPE_VIEW,
                "Expected VIEW type when duplicate username is submitted.");
        Assert.assertNotNull(response.getData());
        Assert.assertNotNull(response.getData().getAdditionalData(),
                "Expected additionalData with validation error for duplicate username.");
        Assert.assertNotNull(response.getData().getAdditionalData().get("error"),
                "Expected 'error' key in additionalData for duplicate username.");
    }

    private static FlowExecutionRequest getFlowExecutionRequest(String flowId,
                                                                Map<String, String> inputs) {

        FlowExecutionRequest flowExecutionRequest = new FlowExecutionRequest();
        flowExecutionRequest.setFlowId(flowId != null ? flowId : "FLOW_ID");
        flowExecutionRequest.setFlowType(REGISTRATION);
        flowExecutionRequest.setActionId(FlowExecutionNegativeTest.ACTION_ID);
        flowExecutionRequest.setInputs(inputs);
        return flowExecutionRequest;
    }

    private static FlowExecutionRequest getFlowExecutionRequest(String flowId) {

        Map<String, String> inputs = new HashMap<>();
        inputs.put("http://wso2.org/claims/username", USER);
        inputs.put("password", "Wso2@test");
        inputs.put("http://wso2.org/claims/emailaddress", "test@wso2.com");
        inputs.put("http://wso2.org/claims/givenname", "RegExecNegJohn");
        inputs.put("http://wso2.org/claims/lastname", "RegExecNegDoe");
        return getFlowExecutionRequest(flowId, inputs);
    }
}
