import fs from 'fs';
import path from 'path';
import Database from 'better-sqlite3';
import dotenv from 'dotenv';

dotenv.config({ path: '../.env' });
const jsonFolder = path.resolve(process.env.STREAM_JSON_LOCATION);
const dbName = 'strimserve.db';
const db = new Database(dbName);

// Map JSON attribute names to database column names
// Not needed today, but makes schema changes easier
const lookup = {
	id: 'id',
	date: 'stream_date',
	filename: 'filename',
	format: 'format',
	title: 'title',
	tags: 'tags',
	description: 'description',
	length_seconds: 'length_seconds',
	tracks: 'tracks'
};

// Retrieve existing IDs from Stream
const existingIds = db.prepare('SELECT id FROM Stream').pluck().all();
// Create a set of existing IDs for efficient lookup
const idSet = new Set(existingIds);

// Check if --overwrite flag is set
const shouldOverwrite = process.argv.includes('--overwrite');

// Process JSON files and insert data into Stream
fs.readdir(jsonFolder, (err, files) => {
	if (err) throw err;
	console.log(`Found ${files.length} JSON file(s) in ${jsonFolder}.`);

	files.forEach(file => {
		if (!file.endsWith('.json')) return;
		if (file.endsWith('.sideload.json')) return;

		const jsonString = fs.readFileSync(path.join(jsonFolder, file), 'utf8');
		let jsonData = JSON.parse(jsonString);

		const sideloadPath = path.join(jsonFolder, file.slice(0, -5) + ".sideload.json");
		if (fs.existsSync(sideloadPath, 'utf8')) {
			const sideloadData = JSON.parse(fs.readFileSync(sideloadPath));
			jsonData = { ...jsonData, ...sideloadData };
		}
		// Skip if ID already exists in Stream
		if (idSet.has(jsonData.id)) {
			if (!shouldOverwrite) {
				console.log(`Skipped data for ID ${jsonData.id} (already exists).`);
				return;
			}
			console.log(`Overwriting data for ID ${jsonData.id}.`);
		}

		// Prepare attributes for insertion
		const values = Object.keys(jsonData).map(
			// Serialize value if array
			key => Array.isArray(jsonData[key]) ? JSON.stringify(jsonData[key]) : jsonData[key]
		);
		const columns = Object.keys(jsonData).map(key => lookup[key]);


		const sql = `INSERT OR REPLACE INTO Stream (${columns.join(', ')}) VALUES (${columns.map(() => '?').join(', ')})`;
		db.prepare(sql).run(...values);
		console.log(`Inserted data for ID ${jsonData.id}.`);
	});
});
