import db, { dbNamespace } from '@nangohq/database';
import configService from './config.service.js';
export const DEFAULT_GITHUB_CLIENT_ID = process.env['DEFAULT_GITHUB_CLIENT_ID'] || '';
export const DEFAULT_GITHUB_CLIENT_SECRET = process.env['DEFAULT_GITHUB_CLIENT_SECRET'] || '';
export const DEMO_GITHUB_CONFIG_KEY = 'github-demo';
export const DEMO_SYNC_NAME = 'issues-demo';
export const DEMO_ACTION_NAME = 'create-demo-issue';
export const DEMO_MODEL = 'GithubIssueDemo';
const TABLE = `${dbNamespace}onboarding_demo_progress`;
export const getOnboardingId = async (user_id) => {
    const result = await db.knex.from(TABLE).select('id').where({ user_id }).first();
    return result ? result.id : null;
};
export const initOnboarding = async (user_id) => {
    const onboardingId = await getOnboardingId(user_id);
    if (onboardingId) {
        return onboardingId;
    }
    const result = await db.knex
        .from(TABLE)
        .insert({
        user_id,
        progress: 0,
        complete: false
    })
        .returning('id');
    if (!result || result.length == 0 || !result[0]) {
        return null;
    }
    return result[0].id;
};
export const updateOnboardingProgress = async (id, progress) => {
    const q = db.knex.from(TABLE).update({ progress }).where({ id });
    if (progress >= 5) {
        void q.update('complete', true);
    }
    await q;
};
export const getOnboardingProgress = async (user_id) => {
    const result = await db.knex
        .from(TABLE)
        .select('progress', 'id', 'complete')
        .where({ user_id })
        .first();
    return result;
};
/**
 * Create Default Provider Config
 * @desc create a default Github config only for the dev environment
 */
export async function createOnboardingProvider({ envId }) {
    const config = {
        environment_id: envId,
        unique_key: DEMO_GITHUB_CONFIG_KEY,
        provider: 'github',
        oauth_client_id: DEFAULT_GITHUB_CLIENT_ID,
        oauth_client_secret: DEFAULT_GITHUB_CLIENT_SECRET,
        oauth_scopes: 'public_repo',
        created_at: new Date(),
        updated_at: new Date()
    };
    await configService.createProviderConfig(config);
}
export async function getOnboardingProvider({ envId }) {
    return await configService.getProviderConfig(DEMO_GITHUB_CONFIG_KEY, envId);
}
//# sourceMappingURL=onboarding.service.js.map