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

const DB_PATH = process.env.DB_PATH || path.resolve(process.cwd(), '..', 'spotify.db');

class DBWrapper {
  private instance: Database.Database | null = null;

  get db(): Database.Database {
    if (!this.instance) {
      this.instance = new Database(DB_PATH);
      this.instance.pragma('journal_mode = WAL');
      this.instance.pragma('foreign_keys = ON');
    }
    return this.instance;
  }

  initialize(): void {
    // Ensure file exists
    if (!fs.existsSync(DB_PATH)) {
      fs.writeFileSync(DB_PATH, '');
    }

    const sql = `
      CREATE TABLE IF NOT EXISTS users (
        id TEXT PRIMARY KEY,
        display_name TEXT,
        email TEXT,
        avatar_url TEXT,
        country TEXT,
        product TEXT,
        created_at INTEGER NOT NULL,
        updated_at INTEGER NOT NULL,
        access_token TEXT,
        refresh_token TEXT,
        token_expires_at INTEGER,
        last_synced_at INTEGER
      );

      CREATE TABLE IF NOT EXISTS friendships (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_a_id TEXT NOT NULL,
        user_b_id TEXT NOT NULL,
        created_at INTEGER NOT NULL,
        UNIQUE(user_a_id, user_b_id)
      );

      CREATE TABLE IF NOT EXISTS friend_requests (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        from_user_id TEXT NOT NULL,
        to_user_id TEXT NOT NULL,
        status TEXT NOT NULL CHECK(status IN ('pending','accepted','declined')),
        created_at INTEGER NOT NULL,
        updated_at INTEGER NOT NULL,
        UNIQUE(from_user_id, to_user_id)
      );

      CREATE TABLE IF NOT EXISTS recently_played (
        id TEXT PRIMARY KEY,
        user_id TEXT NOT NULL,
        played_at INTEGER NOT NULL,
        track_json TEXT NOT NULL
      );

      CREATE TABLE IF NOT EXISTS top_tracks (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id TEXT NOT NULL,
        time_range TEXT NOT NULL,
        rank INTEGER NOT NULL,
        track_json TEXT NOT NULL,
        UNIQUE(user_id, time_range, rank)
      );

      CREATE TABLE IF NOT EXISTS top_artists (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id TEXT NOT NULL,
        time_range TEXT NOT NULL,
        rank INTEGER NOT NULL,
        artist_json TEXT NOT NULL,
        UNIQUE(user_id, time_range, rank)
      );
    `;

    this.db.exec(sql);

    // Migrations for existing DBs
    try {
      const cols = this.db.prepare("PRAGMA table_info(users)").all() as Array<{ name: string }>;
      const hasLastSynced = cols.some(c => c.name === 'last_synced_at');
      if (!hasLastSynced) {
        this.db.exec('ALTER TABLE users ADD COLUMN last_synced_at INTEGER');
      }
    } catch {}
  }
}

export const db = new DBWrapper();


