// SQLite setup and upsert logic for movie data
// Run: npm install better-sqlite3

const Database = require('better-sqlite3');
const db = new Database('movies.db');

db.exec(`
CREATE TABLE IF NOT EXISTS movies (
  title TEXT PRIMARY KEY,
  href TEXT,
  country TEXT,
  genre TEXT,
  details_json TEXT,
  m3u8_links_json TEXT
);
`);

function upsertMovie(movie) {
  const stmt = db.prepare(`
    INSERT OR REPLACE INTO movies (title, href, country, genre, details_json, m3u8_links_json)
    VALUES (?, ?, ?, ?, ?, ?)
  `);
  stmt.run(
    movie.title,
    movie.href,
    movie.country,
    movie.genre,
    JSON.stringify(movie.details),
    JSON.stringify(movie.m3u8_links || {})
  );
}

module.exports = { db, upsertMovie };