// Imports
import { AuthProvider, Environment, ApiClient, AuthScopes, type AccountOptions, QuickbooksError } from '../src/app';
import { describe, expect, test } from 'bun:test';

// Describe the Account API
describe('Live API: Accounts', async () => {
	// Initialize the Auth Provider
	const authProvider = new AuthProvider(
		process.env.QB_CLIENT_ID!,
		process.env.QB_CLIENT_SECRET!,
		process.env.REDIRECT_URI!,
		[AuthScopes.Accounting],
		null,
		Environment.Sandbox,
	);

	// Deserialize the Token
	await authProvider.deserializeToken(process.env.SERIALIZED_TOKEN!, process.env.SECRET_KEY!);

	// Setup the API Client
	const apiClient = new ApiClient(authProvider, Environment.Sandbox);

	// Test retrieving all accounts
	test('should retrieve all accounts', async () => {
		// Get all accounts
		const searchResponse = await apiClient.accounts.getAllAccounts();

		// Test the Accounts
		expect(searchResponse.results).toBeInstanceOf(Array);

		// Test the Account length
		expect(searchResponse.results.length).toBeGreaterThan(0);

		// Test the Intuit TID
		expect(searchResponse.intuitTID).toBeDefined();
		expect(typeof searchResponse.intuitTID).toBe('string');
	});

	// Test Checking for Next Page
	test('should check for next page', async () => {
		// Get all accounts
		const searchResponse = await apiClient.accounts.getAllAccounts();

		// Test the Accounts
		expect(searchResponse.hasNextPage).toBe(true);

		// Test the Intuit TID
		expect(searchResponse.intuitTID).toBeDefined();
		expect(typeof searchResponse.intuitTID).toBe('string');
	});

	// Test retrieving a single account
	test('should retrieve a single account', async () => {
		// Get all accounts
		const searchResponse = await apiClient.accounts.getAllAccounts();
		const accounts = searchResponse?.results?.slice(0, 5);

		await Promise.all(
			accounts.map(async (account) => {
				// Get the Account
				const accountResponse = await apiClient.accounts.getAccountById(account.Id);

				// Test the Account Response Structure
				expect(accountResponse).toBeDefined();
				expect(accountResponse).toHaveProperty('account');
				expect(accountResponse).toHaveProperty('intuitTID');
				expect(typeof accountResponse.intuitTID).toBe('string');

				// Test the Account ID
				expect(accountResponse.account).toHaveProperty('Id');
			}),
		);
	});

	// Test retrieving accounts with limit
	test('should retrieve limited accounts', async () => {
		// Setup the Account Options
		const accountOptions: AccountOptions = { searchOptions: { maxResults: 10 } };

		// Get all accounts
		const searchResponse = await apiClient.accounts.getAllAccounts(accountOptions);

		// Test the Accounts
		expect(searchResponse.results).toBeInstanceOf(Array);

		// Test the Account length
		expect(searchResponse.results.length).toBeGreaterThan(0);
		expect(searchResponse.results.length).toBeLessThanOrEqual(10);

		// Test the Intuit TID
		expect(searchResponse.intuitTID).toBeDefined();
		expect(typeof searchResponse.intuitTID).toBe('string');
	});

	// Test pagination
	test('should handle pagination', async () => {
		// Setup the Account Options
		const accountOptions1: AccountOptions = { searchOptions: { maxResults: 10, page: 1 } };
		const accountOptions2: AccountOptions = { searchOptions: { maxResults: 10, page: 2 } };

		// Get all accounts
		const searchResponse1 = await apiClient.accounts.getAllAccounts(accountOptions1);
		const searchResponse2 = await apiClient.accounts.getAllAccounts(accountOptions2);

		// Test the Accounts
		expect(searchResponse1.results).toBeInstanceOf(Array);
		expect(searchResponse2.results).toBeInstanceOf(Array);

		// Test the Account length
		expect(searchResponse1.results.length).toBeGreaterThan(0);
		expect(searchResponse2.results.length).toBeGreaterThan(0);

		// Test the Accounts are different
		expect(searchResponse1.results).not.toEqual(searchResponse2.results);
	});

	// Test date range filtering
	test('should retrieve accounts within date range', async () => {
		// Get the End Date
		const endDate = new Date();

		// Get the Start Date
		const startDate = new Date();
		startDate.setDate(endDate.getDate() - 30);

		// Get the Accounts
		const searchResponse = await apiClient.accounts.getAccountsForDateRange(startDate, endDate);

		// Assert the Accounts
		expect(searchResponse.results).toBeInstanceOf(Array);

		// Test the Intuit TID
		expect(searchResponse.intuitTID).toBeDefined();
		expect(typeof searchResponse.intuitTID).toBe('string');
	});

	// Test updated accounts
	test('should retrieve updated accounts', async () => {
		// Get the End Date
		const lastUpdated = new Date();
		lastUpdated.setDate(lastUpdated.getDate() - 30);

		// Get the Updated Accounts
		const searchResponse = await apiClient.accounts.getUpdatedAccounts(lastUpdated);

		// Assert the Accounts
		expect(searchResponse.results).toBeInstanceOf(Array);

		// Test the Intuit TID
		expect(searchResponse.intuitTID).toBeDefined();
		expect(typeof searchResponse.intuitTID).toBe('string');
	});

	// Test error handling for invalid ID
	test('should throw QuickbooksError for invalid account ID', async () => {
		try {
			await apiClient.accounts.getAccountById('invalid');
			expect(false).toBe(true); // Should not reach here
		} catch (error) {
			// Assert the Error is a QuickbooksError
			expect(error).toBeInstanceOf(QuickbooksError);
			expect(error).toBeInstanceOf(Error);

			// Assert the Error has the correct structure
			expect(error.message).toBeDefined();
			expect(error.details).toBeDefined();
			expect(error.details.statusCode).toBeDefined();
			expect(typeof error.details.statusCode).toBe('number');
			expect(error.details.intuitError).toBeDefined();
			expect(Array.isArray(error.details.intuitError)).toBe(true);
			expect(error.details.intuitTID).toBeDefined();
			expect(typeof error.details.intuitTID).toBe('string');
		}
	});

	// Test error handling for invalid raw query
	test('should throw QuickbooksError for invalid raw query', async () => {
		// Get the Query Builder
		const queryBuilder = await apiClient.accounts.getQueryBuilder();

		// Add an invalid ID filter that will cause an error
		queryBuilder.whereId('invalid-id-that-does-not-exist');

		try {
			await apiClient.accounts.rawAccountQuery(queryBuilder);
			expect(false).toBe(true); // Should not reach here
		} catch (error) {
			// Assert the Error is a QuickbooksError
			expect(error).toBeInstanceOf(QuickbooksError);
			expect(error).toBeInstanceOf(Error);

			// Assert the Error has the correct structure
			expect(error.message).toBeDefined();
			expect(error.details).toBeDefined();
			expect(error.details.statusCode).toBeDefined();
			expect(typeof error.details.statusCode).toBe('number');
			expect(error.details.intuitError).toBeDefined();
			expect(Array.isArray(error.details.intuitError)).toBe(true);
			expect(error.details.intuitTID).toBeDefined();
			expect(typeof error.details.intuitTID).toBe('string');
		}
	});

	// Should handle all Search Options
	test('should handle all search options', async () => {
		// Setup the Account Options
		const accountOptions: AccountOptions = {
			searchOptions: {
				maxResults: 10,
				page: 1,
				orderBy: { field: 'Id', direction: 'DESC' },
			},
		};

		// Get all accounts
		const searchResponse = await apiClient.accounts.getAllAccounts(accountOptions);

		// Test the Accounts
		expect(searchResponse.results).toBeInstanceOf(Array);

		// Test the Account length
		expect(searchResponse.results.length).toBeGreaterThan(0);
	});

	// Test retrieving updated accounts
	test('should retrieve updated accounts', async () => {
		// Get the Last Updated Time
		const lastUpdatedTime = new Date('2012-01-08');

		// Get the Accounts
		const searchResponse = await apiClient.accounts.getUpdatedAccounts(lastUpdatedTime);

		// Test the Accounts
		expect(searchResponse.results).toBeInstanceOf(Array);

		// Test the Account length
		expect(searchResponse.results.length).toBeGreaterThan(0);

		// Test the Intuit TID
		expect(searchResponse.intuitTID).toBeDefined();
		expect(typeof searchResponse.intuitTID).toBe('string');
	});

	// Test returning an empty array if no accounts are updated
	test('should return an empty array if no accounts are updated', async () => {
		// Setup the Future Date
		const futureDate = new Date();

		// Set the New Full Year
		futureDate.setFullYear(futureDate.getFullYear() + 20);

		// Get the Accounts
		const searchResponse = await apiClient.accounts.getUpdatedAccounts(futureDate);

		// Assert the Customers
		expect(searchResponse.results).toBeArray();

		// Assert the Customers Length
		expect(searchResponse.results.length).toBe(0);

		// Test the Intuit TID
		expect(searchResponse.intuitTID).toBeDefined();
		expect(typeof searchResponse.intuitTID).toBe('string');
	});
});
