import { serverLogger } from "../../utils/serverLogger.js";
/**
 * Add support for listening statistics
 * Extends wave_play_history with duration tracking and additional indexes
 */ export function addStatsSupportTables(db) {
    try {
        serverLogger.info("Adding stats support columns and indexes");
        // Check if duration_seconds column already exists
        const tableInfo = db.prepare("PRAGMA table_info(wave_play_history)").all();
        const hasDurationColumn = tableInfo.some((col)=>col.name === "duration_seconds");
        const hasTrackPathColumn = tableInfo.some((col)=>col.name === "track_path");
        const hasRefIdColumn = tableInfo.some((col)=>col.name === "ref_id");
        if (!hasDurationColumn) {
            db.exec(`ALTER TABLE wave_play_history ADD COLUMN duration_seconds INTEGER`);
            serverLogger.info("Added duration_seconds column to wave_play_history");
        }
        if (!hasTrackPathColumn) {
            db.exec(`ALTER TABLE wave_play_history ADD COLUMN track_path TEXT`);
            serverLogger.info("Added track_path column to wave_play_history");
        }
        if (!hasRefIdColumn) {
            db.exec(`ALTER TABLE wave_play_history ADD COLUMN ref_id TEXT`);
            serverLogger.info("Added ref_id column to wave_play_history");
        }
        // Create additional indexes for stats queries
        db.exec(`
      -- Index for artist-based queries (top artists)
      CREATE INDEX IF NOT EXISTS idx_play_history_artist
      ON wave_play_history(artist);

      -- Index for track-based queries (top tracks)
      CREATE INDEX IF NOT EXISTS idx_play_history_track
      ON wave_play_history(track);

      -- Compound index for user + timestamp (per-user stats)
      CREATE INDEX IF NOT EXISTS idx_play_history_user_timestamp
      ON wave_play_history(user_id, timestamp DESC);
    `);
        serverLogger.info("Stats support tables and indexes created successfully");
    } catch (error) {
        serverLogger.error("Failed to add stats support", {
            error
        });
    // Don't throw - allow app to continue
    }
}
/**
 * Check if stats support columns exist
 */ export function hasStatsSupportColumns(db) {
    try {
        const tableInfo = db.prepare("PRAGMA table_info(wave_play_history)").all();
        return tableInfo.some((col)=>col.name === "duration_seconds");
    } catch  {
        return false;
    }
}
/**
 * Helper to get period start timestamp
 */ export function getPeriodStartTimestamp(period) {
    const now = Date.now();
    switch(period){
        case "week":
            return now - 7 * 24 * 60 * 60 * 1000;
        case "month":
            return now - 30 * 24 * 60 * 60 * 1000;
        case "year":
            return now - 365 * 24 * 60 * 60 * 1000;
        case "all":
        default:
            return 0;
    }
}
/**
 * Helper to get year boundaries for wrapped
 */ export function getYearBoundaries(year) {
    const start = new Date(year, 0, 1, 0, 0, 0, 0).getTime();
    const end = new Date(year, 11, 31, 23, 59, 59, 999).getTime();
    return {
        start,
        end
    };
}

//# sourceMappingURL=addStatsSupport.js.map