import { SenderIdentitiesApi } from '../../api/senderIdentitiesApi';
import { CreateSenderIdentityRequest } from '../../model';
import * as fs from 'fs';
import config from '../config';


describe('SenderIdentityTestcase', () => {
  let brandingApi;
  let senderIdentitiesApi:SenderIdentitiesApi;
  let brandId;
  let createdSenderIdentityId:any
  let emailId:any
  const generateRandomEmail = () => {
    const randomNum = Math.floor(1000 + Math.random() * 9000);
    return `sdktesting${randomNum}@syncfusion.com`;
  };
  beforeAll(() => {
    createdSenderIdentityId = null;
    emailId = null;
    brandId = null;
    const apiKey = config.apiKey;
    const baseUrl = config.baseUrl;
    if (!baseUrl || !apiKey) {
      throw new Error("Environment variables 'HOST_URL' or 'API_KEY' are not set.");
    }
    senderIdentitiesApi = new SenderIdentitiesApi(baseUrl);
    senderIdentitiesApi.setApiKey(apiKey);
    emailId = generateRandomEmail();
    console.log(`Generated email: ${emailId}`);
  });
  test('Test1: should create a brand successfully', async () => {
    const brandName = "NodeSDK";
    const brandLogo = fs.createReadStream("tests/documents/logo.jpg");
    try {
      const createBrandApiResponse = await brandingApi.createBrand(brandName, brandLogo);
      brandId = createBrandApiResponse.body.brandId;
      console.log("Brand created successfully:", brandId);
      expect(createBrandApiResponse.body).toBeDefined();
      expect(createBrandApiResponse.response.status).toBe(200);
      expect(createBrandApiResponse.response.statusText).toBe("OK");
    } catch (error) {
      console.log("Error occurred while creating the brand:", error);
    }
  },20000);
  test('Test2: should create sender identity successfully', async () => {
    const createSenderIdentityRequest = new CreateSenderIdentityRequest();
    createSenderIdentityRequest.name = "SenderIdentity API";
    createSenderIdentityRequest.email = emailId;
    createSenderIdentityRequest.redirectUrl = "https://boldsign.com";
    try {
      const response = await senderIdentitiesApi.createSenderIdentities(createSenderIdentityRequest);
      console.log("Sender Identity created successfully:", response);
      createdSenderIdentityId = response;
      console.log("Created Sender Identity ID:", createdSenderIdentityId);
      expect(response).toBeDefined();
    } catch (error:any) {
      console.log("Error occurred while calling the API:", error.message);
      expect(error.message).toBeUndefined();
    }
  },20000);
  test('Test3:should fail when invalid email ID is provided', async () => {
    const createSenderIdentityRequest = new CreateSenderIdentityRequest();
    createSenderIdentityRequest.name = "Luther Cooper";
    createSenderIdentityRequest.email = "INVALID_EMAIL";  
    createSenderIdentityRequest.redirectUrl = "https://boldsign.com";
    try {
      const response = await senderIdentitiesApi.createSenderIdentities(createSenderIdentityRequest);
      console.error("Expected error due to invalid email ID, but the response was:", response);
      throw new Error("Test failed, invalid email should not be accepted.");
    } catch (error:any) {
      console.log("Error occurred while calling the API:", error.message);
      expect(error.message).toBeDefined();
    }
  },20000);
  test('Test4: should fail to create sender identity with missing name', async () => {
    const createSenderIdentityRequest = new CreateSenderIdentityRequest();
    createSenderIdentityRequest.name = "";
    createSenderIdentityRequest.email = emailId;
    createSenderIdentityRequest.redirectUrl = "https://boldsign.com";
    try {
      const response = await senderIdentitiesApi.createSenderIdentities(createSenderIdentityRequest);
      console.log("Unexpected success response:", response);
    } catch (error: any) {
      console.log("Expected error occurred:", error);
      expect(error).toBeDefined();
      expect(error.response).toBeDefined();
      expect(error.response.status).toBe(400);
      expect(error.response.statusText).toBe("Bad Request");
    }
  },20000);
  test('Test5: should fail to create sender identity with empty email', async () => {
    const createSenderIdentityRequest = new CreateSenderIdentityRequest();
    createSenderIdentityRequest.name = "Sender Identity API";
    createSenderIdentityRequest.email = "";
    createSenderIdentityRequest.redirectUrl = "https://boldsign.com";
    try {
      const response = await senderIdentitiesApi.createSenderIdentities(createSenderIdentityRequest);
      console.log("Unexpected success response:", response);
    } catch (error: any) {
      console.log("Expected error occurred:", error);
      expect(error.response).toBeDefined();
      expect(error.response.status).toBe(400);
      expect(error.response.statusText).toBe("Bad Request");
    }
  },20000);
  test('Test6:should update sender identity successfully', async () => {
    const updateSenderIdentityRequest = {
      name: "SDKTEST",
    };
    try {
      const response = await senderIdentitiesApi.updateSenderIdentities(emailId, updateSenderIdentityRequest);
      console.log("Sender Identity updated successfully:", response);
      expect(response).toBeDefined();
    } catch (error:any) {
      console.log("Error occurred while calling the API:", error.message);
      expect(error.message).toBeUndefined();
    }
  },20000);
  test('Test7:should fail to update sender identity with invalid email', async () => {
    const updateSenderIdentityRequest = {
      name: "SDKTEST",
    };
    const invalidEmail = "INVALID_EMAIL";
    try {
      const response = await senderIdentitiesApi.updateSenderIdentities(invalidEmail, updateSenderIdentityRequest);
      console.log("Sender Identity updated successfully:", response);
      throw new Error("Expected error but got success.");
    } catch (error:any) {
      console.log("Error occurred while calling the API:", error.message);
      expect(error.message).toBeDefined();
    }
  },20000);
  test('Test8: should fail to update sender identity with empty email', async () => {
    const updateSenderIdentityRequest = {
      name: "SDKTEST",
    };
    const emptyEmail = "";
    try {
      const response = await senderIdentitiesApi.updateSenderIdentities(emptyEmail, updateSenderIdentityRequest);
      console.log("Unexpected success response:", response);
      throw new Error("Expected error but got success.");
    } catch (error: any) {
      console.log("Expected error occurred:", error);
      expect(error).toBeDefined();
      expect(error.response).toBeDefined();
      expect(error.response.status).toBe(400);
      expect(error.response.statusText).toBe("Bad Request");
    }
  },20000);
  test('Test9: should fail to update sender identity with empty name', async () => {
    const updateSenderIdentityRequest = {
      name: "",
    };
    const validEmail = emailId;
    try {
      const response = await senderIdentitiesApi.updateSenderIdentities(validEmail, updateSenderIdentityRequest);
      console.log("Unexpected success response:", response);
      throw new Error("Expected error but got success.");
    } catch (error: any) {
      console.log("Expected error occurred:", error);
      expect(error.response).toBeDefined();
      expect(error.response.status).toBe(400);
      expect(error.response.statusText).toBe("Bad Request");
    }
  },20000);
  test('Test10:should resend invitation sender identity successfully', async () => {
    try {
      const response = await senderIdentitiesApi.resendInvitationSenderIdentities(emailId);
      console.log("Invitation resent successfully:", response);
      expect(response).toBeDefined();
    } catch (error:any) {
      console.log("Error occurred while resending invitation:", error.message);
      expect(error.message).toBeUndefined();
    }
  },20000);
  test('Test11:should fail to resend invitation with invalid email', async () => {
    const invalidEmail = "invalid-email-id";
    try {
      const response = await senderIdentitiesApi.resendInvitationSenderIdentities(invalidEmail);
      console.log("Invitation resent successfully:", response);
      throw new Error("Expected error but got success");
    } catch (error:any) {
      console.log("Error occurred while resending invitation:", error.message);
      expect(error.message).toBeDefined();
    }
  },20000);
  test('Test12:should fail to resend invitation with non existing sender identity email', async () => {
    const invalidEmail = "sdktestinabc@syncfusion.com";
    try {
      const response = await senderIdentitiesApi.resendInvitationSenderIdentities(invalidEmail);
      console.log("Invitation resent successfully:", response);
      throw new Error("Expected error but got success");
    } catch (error:any) {
      console.log("Error occurred while resending invitation:", error);
      expect(error.response).toBeDefined();
      expect(error.response?.status).toBe(400);
      expect(error.response.statusText).toBe("Bad Request");
    }
  },20000);
  test('Test13:should fail to resend invitation with empty email', async () => {
    const invalidEmail = "";
    try {
      const response = await senderIdentitiesApi.resendInvitationSenderIdentities(invalidEmail);
      console.log("Invitation resent successfully:", response);
      throw new Error("Expected error but got success");
    } catch (error:any) {
      console.log("Error occurred while resending invitation:", error);
      expect(error.response).toBeDefined();
      expect(error.response?.status).toBe(400);
      expect(error.response.statusText).toBe("Bad Request");
    }
  },20000);
  test('Test14:should list sender identities successfully', async () => {
    const page = 1;
    const pageSize = 10;
    const search = " ";
    const brandIds = [];
    try {
      const response = await senderIdentitiesApi.listSenderIdentities(page, pageSize, search, brandIds);
      expect(response).toBeDefined();
    } catch (error:any) {
      console.log("Error while listing sender identities:", error.message);
      expect(error.message).toBeUndefined();
    }
  },20000);
  test('Test15:should fail to list sender identities with negative page and page size', async () => {
    const page = -1;
    const pageSize = -10;
    const search = " ";
    const brandIds = [];
    try {
      await senderIdentitiesApi.listSenderIdentities(page, pageSize, search, brandIds);
      throw new Error("Expected error but got success");
    } catch (error:any) {
      console.log("Error while listing sender identities:", error.message);
      expect(error.message).toBeDefined();
    }
  },20000);
  test('Test16:should fail to list sender identities with invalid parameters', async () => {
    const page = 100;
    const pageSize = 10000;
    const search = " ";
    const brandIds = [];
    try {
      await senderIdentitiesApi.listSenderIdentities(page, pageSize, search, brandIds);
      throw new Error("Expected error but got success");
    } catch (error:any) {
      console.log("Error while listing sender identities:", error);
      expect(error.response).toBeDefined();
      expect(error.response?.status).toBe(400);
      expect(error.response.statusText).toBe("Bad Request");
    }
  },20000);
  test('Test17:should fail to process request with invalid email', async () => {
    const invalidEmail = "invalid-email";
    try {
      await senderIdentitiesApi.reRequestSenderIdentities(invalidEmail);
      throw new Error("Expected error but got success");
    } catch (error:any) {
      console.log("Error occurred while re-requesting sender identity:", error.message);
      expect(error.message).toBeDefined();
    }
  },20000);
  test('Test18:should fail to process request with empty email', async () => {
    const emptyEmail = " ";
    try {
      await senderIdentitiesApi.reRequestSenderIdentities(emptyEmail);
      throw new Error("Expected error but got success");
    } catch (error:any) {
      console.log("Error occurred while re-requesting sender identity:", error.message);
      expect(error.message).toBeDefined();
    }
  },20000);
  test('Test19:should fail to process request negative', async () => {
    const emptyEmail = emailId;
    try {
      await senderIdentitiesApi.reRequestSenderIdentities(emptyEmail);
      throw new Error("Expected error but got success");
    } catch (error:any) {
      console.log("Error occurred while re-requesting sender identity:", error);
      expect(error.response).toBeDefined();
      expect(error.response?.status).toBe(400);
      expect(error.response.statusText).toBe("Bad Request");
    }
  },20000);
  test('Test20:should delete sender identity successfully', async function() {
    try {
        const response = await senderIdentitiesApi.deleteSenderIdentities(emailId);
        console.log("Sender identity deleted successfully");
        expect(response).toBeDefined()
    } catch (error:any) {
        console.log("Error occurred while deleting sender identity:", error.message);
        expect(error.message).toBeUndefined();
    }
},20000);
test('Test21:should fail to delete sender identity with empty email', async () => {
    const email = "";
    try {
        await senderIdentitiesApi.deleteSenderIdentities(email);
        throw new Error("Expected error but got success");
    } catch (error:any) {
        console.log("Error occurred while deleting sender identity:", error.message);
        expect(error.message).toBeDefined();
    }
},20000);
test('Test22:should fail to delete sender identity with invalid email', async () => {
  const email = "invalid-email";
  try {
      await senderIdentitiesApi.deleteSenderIdentities(email);
      throw new Error("Expected error but got success");
  } catch (error:any) {
      console.log("Error occurred while deleting sender identity:", error);
      expect(error.response).toBeDefined();
      expect(error.response?.status).toBe(400);
      expect(error.response.statusText).toBe("Bad Request");
  }
},20000);
test('Test23:should fail to delete sender identity with non existing sender identity email', async () => {
  const email = "sdktestinabc@syncfusion.com";
  try {
      await senderIdentitiesApi.deleteSenderIdentities(email);
      throw new Error("Expected error but got success");
  } catch (error:any) {
      console.log("Error occurred while deleting sender identity:", error);
      expect(error.response).toBeDefined();
      expect(error.response?.status).toBe(403);
      expect(error.response.statusText).toBe("Forbidden");
  }
},20000);
test('Test24: should create sender identity successfully with existing brand ID', async () => {
  const createSenderIdentityRequest = new CreateSenderIdentityRequest();
  createSenderIdentityRequest.name = "SenderIdentity API";
  createSenderIdentityRequest.email = emailId;
  createSenderIdentityRequest.redirectUrl = "https://boldsign.com";
  createSenderIdentityRequest.brandId = brandId;
  try {
    const response = await senderIdentitiesApi.createSenderIdentities(createSenderIdentityRequest);
    console.log("Sender Identity created successfully:", response);
    createdSenderIdentityId = response.senderIdentityId;
    expect(response).toBeDefined();
  } catch (error) {
    console.log("Error occurred while calling the API:", error);
    expect(error).toBeUndefined();
  }
}, 20000);
test('Test25:should delete sender identity successfully', async function() {
  try {
      const response = await senderIdentitiesApi.deleteSenderIdentities(emailId);
      console.log("Sender identity deleted successfully.");
      expect(response).toBeDefined()
  } catch (error) {
      console.log("Error occurred while deleting sender identity:", error);
      expect(error).toBeUndefined();
  }
},20000);
test('Test26: should create sender identity successfully with 80-character name', async () => {
  const crypto = require('crypto'); 
  const generateRandomString = (length) => {
    return crypto.randomBytes(length).toString('hex').slice(0, length);
  };
  const senderIdentityName = generateRandomString(80);
  const createSenderIdentityRequest = new CreateSenderIdentityRequest();
  createSenderIdentityRequest.name = senderIdentityName;
  createSenderIdentityRequest.email = emailId;
  createSenderIdentityRequest.redirectUrl = "https://boldsign.com";
  try {
    const response = await senderIdentitiesApi.createSenderIdentities(createSenderIdentityRequest);
    console.log("Sender Identity created successfully:", response);
    createdSenderIdentityId = response.senderIdentityId;
    console.log("Created Sender Identity ID:", createdSenderIdentityId);
    expect(response).toBeDefined();
  } catch (error) {
    console.error("Error occurred while calling the API:", error);
    expect(error).toBeUndefined();
  }
}, 20000);
test('Test27: should fail to create sender identity successfully with 80-character name with duplicate name', async () => {
  const crypto = require('crypto'); 
  const generateRandomString = (length) => {
    return crypto.randomBytes(length).toString('hex').slice(0, length);
  };
  const senderIdentityName = generateRandomString(80);
  const createSenderIdentityRequest = new CreateSenderIdentityRequest();
  createSenderIdentityRequest.name = senderIdentityName;
  createSenderIdentityRequest.email = emailId;
  createSenderIdentityRequest.redirectUrl = "https://boldsign.com";
  try {
    const response = await senderIdentitiesApi.createSenderIdentities(createSenderIdentityRequest);
    console.log("Sender Identity created successfully:", response);
  } catch (error:any) {
    console.error("Error occurred while calling the API:", error);
    expect(error.response).toBeDefined();
    expect(error.response?.status).toBe(400);
    expect(error.response.statusText).toBe("Bad Request");
  }
}, 20000);
});