import { db, visibleClause } from '../db.js';
import { config } from '../config.js';
import { settingsPayload } from './settingsModel.js';

export function getBootstrap(userId) {
  return {
    summary: {
      scripts: db.prepare(`SELECT COUNT(*) AS count FROM scripts WHERE ${visibleClause()}`).get(userId, userId).count,
      hosts: db.prepare(`SELECT COUNT(*) AS count FROM hosts WHERE ${visibleClause()}`).get(userId, userId).count,
      runplans: db.prepare(`SELECT COUNT(*) AS count FROM runplans WHERE ${visibleClause()}`).get(userId, userId).count,
      jobs: db.prepare(`
        SELECT COUNT(*) AS count FROM jobs j
        LEFT JOIN runplans r ON r.id = j.runplan_id
        WHERE j.triggered_by = ?
          OR r.visibility = 'shared'
          OR r.owner_user_id = ?
          OR r.group_id IN (SELECT group_id FROM user_groups WHERE user_id = ?)
      `).get(userId, userId, userId).count
    },
    settings: settingsPayload(),
    entra: { enabled: config.entra.enabled, configured: Boolean(config.entra.tenantId && config.entra.clientId) }
  };
}
