#!/usr/bin/env node

/**
 * Script to detect placeholder files
 * Detect files that only have // TODO or // Placeholder but not yet implemented
 */

const fs = require('fs');
const path = require('path');

const SRC_DIR = path.join(__dirname, '..', 'src');
const WARNINGS = [];
const ERRORS = [];

// Placeholder patterns to detect
const PLACEHOLDER_PATTERNS = [
	{
		pattern: /\/\/\s*TODO.*implement/i,
		message: 'contains "// TODO ... implement"',
	},
	{
		pattern: /\/\/\s*Placeholder/i,
		message: 'contains "// Placeholder"',
	},
	{
		pattern: /\/\/\s*TODO.*Move type definitions/i,
		message: 'needs to move type definitions',
	},
	{
		pattern: /class\s+\w+\s*\{\s*\/\/\s*Placeholder/i,
		message: 'class is just a placeholder',
	},
	{
		pattern: /export\s+const\s+\w+\s*=\s*\{\s*\/\/\s*Placeholder/i,
		message: 'object is just a placeholder',
	},
];

// Files allowed to have placeholders (currently in development)
const ALLOWED_PLACEHOLDERS = [
	'src/services/storage/cacheService.ts',
	'src/services/storage/sessionStorageService.ts',
	'src/services/scheduler/jobs/cleanupJob.ts',
	'src/services/scheduler/jobs/notificationJob.ts',
	'src/services/scheduler/jobs/passwordResetJob.ts',
	'src/services/notification/helpers/deliveryHelper.ts',
	'src/services/notification/helpers/templateHelper.ts',
	'src/services/scheduler/types.ts',
	'src/services/auth/anomalyDetectionService.ts',
	'src/services/auth/deviceTrustService.ts',
	'src/services/auth/serviceToServiceAuthService.ts',
];

function isAllowedPlaceholder(relativePath) {
	// Normalize path separators to forward slashes for cross-platform compatibility
	const normalizedPath = relativePath.replace(/\\/g, '/');
	return ALLOWED_PLACEHOLDERS.some((allowed) => normalizedPath.includes(allowed));
}

function checkPlaceholders(directory) {
	const items = fs.readdirSync(directory, { withFileTypes: true });

	for (const item of items) {
		const fullPath = path.join(directory, item.name);
		const relativePath = path.relative(path.join(__dirname, '..'), fullPath);

		if (item.isDirectory()) {
			// Skip node_modules, dist, etc.
			if (['node_modules', 'dist', 'coverage'].includes(item.name)) continue;
			checkPlaceholders(fullPath);
		} else if (item.isFile() && item.name.endsWith('.ts')) {
			const content = fs.readFileSync(fullPath, 'utf-8');

			for (const { pattern, message } of PLACEHOLDER_PATTERNS) {
				if (pattern.test(content)) {
					if (isAllowedPlaceholder(relativePath)) {
						WARNINGS.push(`[PLACEHOLDER] "${relativePath}": ${message} (allowed temporarily)`);
					} else {
						ERRORS.push(`[PLACEHOLDER] "${relativePath}": ${message}`);
					}
					break; // Only report once per file
				}
			}
		}
	}
}

function main() {
	console.log('📝 Checking placeholder files...\n');

	checkPlaceholders(SRC_DIR);

	// Report results
	if (WARNINGS.length > 0) {
		console.log('⚠️  Warnings (placeholders allowed temporarily):');
		WARNINGS.forEach((w) => console.log(`  ${w}`));
		console.log();
	}

	if (ERRORS.length > 0) {
		console.log('❌ Errors (placeholders not allowed):');
		ERRORS.forEach((e) => console.log(`  ${e}`));
		console.log(`\n❌ Placeholder check failed: ${ERRORS.length} file(s) with placeholders not allowed`);
		console.log('   Please implement these files or add to ALLOWED_PLACEHOLDERS if in development.');
		process.exit(1);
	}

	if (WARNINGS.length === 0 && ERRORS.length === 0) {
		console.log('✅ Placeholder check passed! No placeholders detected.');
	} else if (ERRORS.length === 0) {
		console.log('✅ Placeholder check passed with warnings.');
	}

	process.exit(0);
}

main();
