import { BrandingApi } from '../../api/brandingApi';
import config from '../config';
import * as fs from 'fs';

describe('Test suite', () => {
  let brandingApi;
  let brandId;
  beforeAll(() => {
    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");
    }
    brandingApi = new BrandingApi(baseUrl);
    brandingApi.setApiKey(apiKey);
  });
  test('Test1:should create a brand successfully', async () => {
    const brandName = "NodeSDK";
    const brandLogo = fs.createReadStream("tests/documents/logo.jpg");
    const backgroundColor = "Blue";
    const buttonColor = "Black";
    const buttonTextColor = "White";
    const emailDisplayName = "{SenderName} from Syncfusion";
    try {
      const createBrandApiResponse = await brandingApi.createBrand(brandName, brandLogo, backgroundColor, buttonColor, buttonTextColor, emailDisplayName);
      console.log("Brand created successfully:", createBrandApiResponse.brandId);
      brandId = createBrandApiResponse.brandId;
      expect(brandId).toBeDefined();
      expect(brandId).toBeGreaterThan(0);
    } catch (error:any) {
      console.log("Error occurred while creating the brand:", error.message);
    }
  }, 20000);
  test('Test2:should create a brand only with required fields', async () => {
    const brandName = "NodeSDK";
    const brandLogo = fs.createReadStream("tests/documents/logo.jpg");
    try {
      const createBrandApiResponse = await brandingApi.createBrand(brandName, brandLogo);
      console.log("Brand created successfully:", createBrandApiResponse.brandId);
      expect(createBrandApiResponse).toBeDefined();
    } catch (error) {
      console.log("Error occurred while creating the brand:", error);
      expect(error).toBeUndefined();
    }
  }, 20000);
  test('Test3:should fail to create a brand without brand name', async () => {
    const brandName = "";
    const brandLogo = fs.createReadStream("tests/documents/logo.jpg");
    const backgroundColor = "Blue";
    const buttonColor = "Black";
    const buttonTextColor = "White";
    const emailDisplayName = "{SenderName} from Syncfusion";
    try {
      await brandingApi.createBrand(brandName, brandLogo, backgroundColor, buttonColor, buttonTextColor, emailDisplayName);
      throw new Error("API call should have failed");
    } catch (error:any) {
      console.error("Error occurred while creating the brand:", error.message);
      expect(error.message).toBeDefined();
    }
  }, 20000);
  test('Test4:should fail to create a brand with invalid background color', async () => {
    const brandName = "Node SDK";
    const brandLogo = fs.createReadStream("tests/documents/logo.jpg");
    const backgroundColor = "invalid-color";
    const buttonColor = "Black";
    const buttonTextColor = "White";
    const emailDisplayName = "{SenderName} from Syncfusion";
    try {
      await brandingApi.createBrand(brandName,brandLogo,backgroundColor,buttonColor,buttonTextColor,emailDisplayName);
      throw new Error("API call should have failed due to invalid background color");
    } catch (error: any) {
      console.error("Expected error for invalid background color:", error);
      expect(error.response).toBeDefined();
      expect(error.response?.status).toBe(400);
      expect(error.response.statusText).toBe("Bad Request");
    }
  }, 20000);
  test('Test5:should fail to create a brand with invalid button color', async () => {
    const brandName = "Node SDK";
    const brandLogo = fs.createReadStream("tests/documents/logo.jpg");
    const backgroundColor = "Blue";
    const buttonColor = "not-a-color";
    const buttonTextColor = "White";
    const emailDisplayName = "{SenderName} from Syncfusion";
    try {
      await brandingApi.createBrand(
        brandName,
        brandLogo,
        backgroundColor,
        buttonColor,
        buttonTextColor,
        emailDisplayName
      );
      throw new Error("API call should have failed due to invalid button color");
    } catch (error: any) {
      console.error("Expected error for invalid button color:", error);
      expect(error.response).toBeDefined();
      expect(error.response?.status).toBe(400);
      expect(error.response.statusText).toBe("Bad Request");
    }
  }, 20000);
  test('Test6:should fail to create a brand with invalid button text color', async () => {
    const brandName = "Node SDK";
    const brandLogo = fs.createReadStream("tests/documents/logo.jpg");
    const backgroundColor = "Blue";
    const buttonColor = "Black";
    const buttonTextColor = "invalid-text-color";
    const emailDisplayName = "{SenderName} from Syncfusion";
    try {
      await brandingApi.createBrand(
        brandName,
        brandLogo,
        backgroundColor,
        buttonColor,
        buttonTextColor,
        emailDisplayName
      );
      throw new Error("API call should have failed due to invalid button text color");
    } catch (error: any) {
      console.error("Expected error for invalid button text color:", error);
      expect(error.response).toBeDefined();
      expect(error.response?.status).toBe(400);
      expect(error.response.statusText).toBe("Bad Request");
    }
  }, 20000);
  test('Test7:should fail to create a brand with invalid logo path', async () => {
    const brandName = "Node SDK";
    const brandLogo = fs.createReadStream("tests/documents/agreement.pdf");
    try {
      await brandingApi.createBrand(brandName,brandLogo);
      throw new Error("API call should have failed due to invalid button text color");
    } catch (error: any) {
      console.error("Expected error for invalid button text color:", error);
      expect(error.response).toBeDefined();
      expect(error.response?.status).toBe(400);
      expect(error.response.statusText).toBe("Bad Request");
    }
  }, 20000);
  test('Test8:should update a brand successfully', async () => {
    const brandName = "Node-SDK-Test";
    const brandLogo = fs.createReadStream("tests/documents/logo.jpg");
    const backgroundColor = "Blue";
    const buttonColor = "Black";
    const buttonTextColor = "White";
    const emailDisplayName = "divya.boopathy+12@syncfusion.com";
    try {
      const updateBrandApiResponse = await brandingApi.editBrand(brandId, brandName, brandLogo, backgroundColor, buttonColor, buttonTextColor, emailDisplayName);
      console.log("Brand updated successfully:", updateBrandApiResponse.brandId);
      expect(updateBrandApiResponse).toBeDefined();
    } catch (error:any) {
      console.error("Error occurred while updating the brand:", error.message);
      throw new Error("API call failed");
    }
  }, 20000);
  test('Test9: should fail to update a brand with invalid logo path', async () => {
    const brandName = "Node-SDK-Test";
    try {
      const brandLogo = fs.createReadStream("tests/documents/agreement.pdf");
      await brandingApi.editBrand(brandId,brandName,brandLogo);
    } catch (error:any) {
      console.error("Expected error while updating with invalid logo path:");
      expect(error.response).toBeDefined();
      expect(error.response?.status).toBe(400);
      expect(error.response.statusText).toBe("Bad Request");
    }
  }, 20000);
  test('Test10: should fail to update a brand with invalid brand ID', async () => {
    const brandName = "Node-SDK-Test";
    try {
      const invalidBrandId = "invalid-brand-id";
      const brandLogo = fs.createReadStream("tests/documents/logo.jpg");
      await brandingApi.editBrand(invalidBrandId,brandName,brandLogo);
      throw new Error("API call should have failed due to invalid brand ID");
    } catch (error: any) {
      console.error("Expected error while updating with invalid brand ID:", error.message);
      expect(error.response).toBeDefined();
      expect(error.response?.status).toBe(400);
      expect(error.response.statusText).toBe("Bad Request");
    }
  }, 20000);
  test('Test11:should get brand details successfully', async () => {
    try {
      const brandDetailsResponse = await brandingApi.getBrand(brandId);
      console.log("Brand details retrieved successfully:", brandDetailsResponse);
      expect(brandDetailsResponse).toBeDefined();
      expect(brandDetailsResponse.id).toBe(brandId);
    } catch (error:any) {
      console.log("Error occurred while calling the API:", error.message);
    }
  }, 20000);
  test('Test12:should fail to retrieve brand details with non-existent brand ID', async () => {
    const brandId = "INVALID_BRAND_ID";
    try {
      const brandDetailsResponse = await brandingApi.getBrand(brandId);
      console.log("Brand details retrieved successfully:", brandDetailsResponse);
      expect(brandDetailsResponse).toBeUndefined();
    } catch (error:any) {
      console.error("Error occurred while calling the API:", error.message);
      expect(error.message).toBeDefined();
    }
  }, 20000);
  test('Test13:should retrieve the brand list successfully', async () => {
    try {
      const brandListResponse = await brandingApi.brandList();
      console.log("Brand List:", brandListResponse);
    } catch (error:any) {
      console.error("Error occurred while calling the API:", error.message);
      throw new Error("API call failed");
    }
  }, 20000);
  test('Test14:should reset the default brand successfully', async () => {
    try {
      const response = await brandingApi.resetDefaultBrand(brandId);
      console.log("Brand reset as default successfully:", response);
      expect(response).toBeDefined();
    } catch (error:any) {
      console.error("Error occurred while calling the API:", error.message);
      throw new Error("API call failed to reset default brand");
    }
  }, 30000);
  test('should fail to reset the default brand with non-existent brand ID', async () => {
    const brandId = "NON_EXISTENT_BRAND_ID";
    try {
      const response = await brandingApi.resetDefaultBrand(brandId);
      console.log("Brand reset unexpectedly succeeded:", response);
      throw new Error("Test failed. API call should have failed with non-existent brand ID.");
    } catch (error:any) {
      console.error("Expected error occurred while calling the API:", error.message);
    }
  }, 20000);
  test('Tes16:should delete a brand successfully', async () => {
    try {
      const deleteResponse = await brandingApi.deleteBrand(brandId);
      console.log("Brand deleted successfully:", deleteResponse);
      expect(deleteResponse).toBeDefined();
      expect(deleteResponse.success).toBe(true);
    } catch (error:any) {
      console.log("Error occurred while calling the API:", error.message);
    }
  }, 20000);
  test('Test17:should fail to delete a brand with invalid brand ID', async () => {
    const brandId = "invalid-brand-id";
    try {
      const deleteResponse = await brandingApi.deleteBrand(brandId);
      console.log("Brand deleted unexpectedly:", deleteResponse);
      expect(deleteResponse).toBeDefined();
      expect(deleteResponse.success).toBe(false);
    } catch (error:any) {
      console.error("Error occurred while calling the API:", error.message);
      expect(error.message).toBeDefined();
    }
  }, 20000);
});