// #region TargetSchemaApiTest [C:2] [TYPE Module] [SEMANTICS test, translate, target-schema, api]
// @BRIEF Unit tests for checkTargetTableSchema API client fetch wrapper.
// @LAYER Tests
// @RELATION BINDS_TO -> [TranslateTargetSchemaApi]
// @TEST_CONTRACT: checkTargetTableSchema -> returns schema validation result with all fields
// @TEST_EDGE: api_error -> Returns fallback response with error message
// @TEST_EDGE: success_response -> Returns parsed response with all fields

import { describe, it, expect, vi, beforeEach } from 'vitest';

vi.mock('$lib/api.js', () => ({
	api: {
		postApi: vi.fn(),
	},
}));

describe('checkTargetTableSchema', () => {
	beforeEach(() => {
		vi.clearAllMocks();
	});

	// #region test_success [TYPE Function]
	// @PURPOSE: Happy path — API returns full validation result.
	it('returns result with all fields on success', async () => {
		const { api } = await import('$lib/api.js');
		api.postApi.mockResolvedValue({
			table_exists: true,
			error: null,
			expected_columns: [
				{ name: 'id', data_type: 'integer', is_nullable: true },
				{ name: 'name', data_type: 'text', is_nullable: true },
			],
			actual_columns: [
				{ name: 'id', data_type: 'integer', is_nullable: true },
				{ name: 'name', data_type: 'text', is_nullable: true },
			],
			missing_columns: [],
			extra_columns: [],
			all_present: true,
		});

		const { checkTargetTableSchema } = await import(
			'$lib/api/translate/target-schema.js'
		);
		const result = await checkTargetTableSchema({
			environment_id: 'env-1',
			target_database_id: 'db-1',
			target_table: 'my_table',
		});

		expect(api.postApi).toHaveBeenCalledWith('/translate/check-target-schema', {
			environment_id: 'env-1',
			target_database_id: 'db-1',
			target_table: 'my_table',
		});
		expect(result.table_exists).toBe(true);
		expect(result.all_present).toBe(true);
		expect(result.error).toBeNull();
		expect(result.expected_columns).toHaveLength(2);
	});

	// #endregion test_success

	// #region test_api_error [TYPE Function]
	// @PURPOSE: API error returns fallback response with error message.
	it('returns fallback response with error on API failure', async () => {
		const { api } = await import('$lib/api.js');
		api.postApi.mockRejectedValue(new Error('Network error'));

		const { checkTargetTableSchema } = await import(
			'$lib/api/translate/target-schema.js'
		);
		const result = await checkTargetTableSchema({
			environment_id: 'env-1',
			target_database_id: 'db-1',
			target_table: 'my_table',
		});

		expect(result.table_exists).toBe(false);
		expect(result.all_present).toBe(false);
		expect(result.error).toBe('Network error');
		expect(result.expected_columns).toEqual([]);
		expect(result.missing_columns).toEqual([]);
	});

	// #endregion test_api_error

	// #region test_error_string_fallback [TYPE Function]
	// @PURPOSE: String error is captured as message.
	it('handles string error value', async () => {
		const { api } = await import('$lib/api.js');
		api.postApi.mockRejectedValue('Server says no');

		const { checkTargetTableSchema } = await import(
			'$lib/api/translate/target-schema.js'
		);
		const result = await checkTargetTableSchema({
			environment_id: 'env-1',
			target_database_id: 'db-1',
			target_table: 'my_table',
		});

		expect(result.error).toBe('Server says no');
		expect(result.table_exists).toBe(false);
	});

	// #endregion test_error_string_fallback
});
// #endregion TargetSchemaApiTest
