/* eslint-disable @typescript-eslint/no-explicit-any */
import { processBuild, processProjectBuild, processGatewayJson } from '../../src/index.js';
import fs from 'fs';
import path from 'path';
import { LogWrapper } from '../../src/service/log-wrapper.js';

jest.mock('@apic/studio-logger', () => ({
	LoggerConfig: {
		isLoggerEnabled: jest.fn(),
	},
}));

jest.mock('@apic/studio-shared', () => ({
	ErrorResponse: jest.fn(),
	Metadata_Ref: jest.fn(),
	SpecObject: jest.fn(),
	YamlContent: jest.fn(),
	UpperCaseKinds: jest.fn(),
}));

jest.mock('../../src/service/log-wrapper.ts');

describe('processBuild', () => {

	it('should handle processGatewayJson', async () => {
		const zipFilePath = path.resolve(__dirname, '../assets/gateway-multi-project-asset.zip');
		const zipBuffer = fs.readFileSync(zipFilePath);

		const res = await processGatewayJson(zipBuffer);
		expect(res).not.toBeNull;
	});

	it('should handle success and generate zip buffer correctly', async () => {
		const zipFilePath = path.resolve(__dirname, '../assets/deploy_200.zip');
		const zipBuffer = fs.readFileSync(zipFilePath);
		await processBuild(zipBuffer);
		expect(LogWrapper.logInfo).toHaveBeenCalledWith('0003', 'Starting build process.');
		expect(LogWrapper.logInfo).toHaveBeenCalledWith('0003', 'Processing zip file.');
	});
	it('should handle error in processing zip', async () => {
		const zipFilePath = path.resolve(__dirname, '../assets/gateway-asset-error.zip');
		const zipBuffer = fs.readFileSync(zipFilePath);

		await expect(processBuild(zipBuffer)).rejects.toThrow(
			JSON.stringify({ 'respCode': 400, 'message': 'Invalid Assets or Reference in the Zip', 'Endpoints': [], 'errors': [{ 'code': 'STU-VAL_ERR', 'field': 'cartAPI-spec:1.0', 'description': 'Validation failed for Reference cartAPI-spec:1.0' }] })
		);

	});
	it('should handle error in processing Project zip', async () => {
		const zipFilePath = path.resolve(__dirname, '../assets/gateway-asset-error.zip');
		const zipBuffer = fs.readFileSync(zipFilePath);
		await expect(processProjectBuild(zipBuffer)).rejects.toThrow();

	});

	it('should handle error in processing Project zip successful', async () => {
		const zipFilePath = path.resolve(__dirname, '../assets/gateway-multi-project-asset.zip');
		const zipBuffer = fs.readFileSync(zipFilePath);
		await processProjectBuild(zipBuffer);
		expect(LogWrapper.logInfo).toHaveBeenCalledWith('0003', 'Starting project build processing.');
	});

});
