var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var SqlLoggingConfigResolver_1;
import { Arg, Ctx, Field, InputType, Mutation, ObjectType, Query, Int, Resolver } from 'type-graphql';
import { UserCache } from '@memberjunction/sqlserver-dataprovider';
import * as path from 'path';
import * as fs from 'fs/promises';
import { loadConfig } from '../config.js';
import { ResolverBase } from '../generic/ResolverBase.js';
import { GetReadOnlyProvider } from '../util.js';
/**
 * Configuration options for SQL logging sessions.
 * These options control how SQL statements are captured, formatted, and saved.
 */
let SqlLoggingOptions = class SqlLoggingOptions {
};
__decorate([
    Field(() => Boolean, { nullable: true }),
    __metadata("design:type", Boolean)
], SqlLoggingOptions.prototype, "formatAsMigration", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingOptions.prototype, "description", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingOptions.prototype, "statementTypes", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingOptions.prototype, "batchSeparator", void 0);
__decorate([
    Field(() => Boolean, { nullable: true }),
    __metadata("design:type", Boolean)
], SqlLoggingOptions.prototype, "prettyPrint", void 0);
__decorate([
    Field(() => Boolean, { nullable: true }),
    __metadata("design:type", Boolean)
], SqlLoggingOptions.prototype, "logRecordChangeMetadata", void 0);
__decorate([
    Field(() => Boolean, { nullable: true }),
    __metadata("design:type", Boolean)
], SqlLoggingOptions.prototype, "retainEmptyLogFiles", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingOptions.prototype, "filterByUserId", void 0);
__decorate([
    Field(() => [String], { nullable: true }),
    __metadata("design:type", Array)
], SqlLoggingOptions.prototype, "filterPatterns", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingOptions.prototype, "filterType", void 0);
__decorate([
    Field(() => Boolean, { nullable: true }),
    __metadata("design:type", Boolean)
], SqlLoggingOptions.prototype, "verboseOutput", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingOptions.prototype, "defaultSchemaName", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingOptions.prototype, "sessionName", void 0);
SqlLoggingOptions = __decorate([
    ObjectType()
], SqlLoggingOptions);
export { SqlLoggingOptions };
let SqlLoggingSession = class SqlLoggingSession {
};
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], SqlLoggingSession.prototype, "id", void 0);
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], SqlLoggingSession.prototype, "filePath", void 0);
__decorate([
    Field(() => Date),
    __metadata("design:type", Date)
], SqlLoggingSession.prototype, "startTime", void 0);
__decorate([
    Field(() => Int),
    __metadata("design:type", Number)
], SqlLoggingSession.prototype, "statementCount", void 0);
__decorate([
    Field(() => SqlLoggingOptions),
    __metadata("design:type", SqlLoggingOptions)
], SqlLoggingSession.prototype, "options", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingSession.prototype, "sessionName", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingSession.prototype, "filterByUserId", void 0);
SqlLoggingSession = __decorate([
    ObjectType()
], SqlLoggingSession);
export { SqlLoggingSession };
let SqlLoggingOptionsInput = class SqlLoggingOptionsInput {
};
__decorate([
    Field(() => Boolean, { nullable: true }),
    __metadata("design:type", Boolean)
], SqlLoggingOptionsInput.prototype, "formatAsMigration", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingOptionsInput.prototype, "description", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingOptionsInput.prototype, "statementTypes", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingOptionsInput.prototype, "batchSeparator", void 0);
__decorate([
    Field(() => Boolean, { nullable: true }),
    __metadata("design:type", Boolean)
], SqlLoggingOptionsInput.prototype, "prettyPrint", void 0);
__decorate([
    Field(() => Boolean, { nullable: true }),
    __metadata("design:type", Boolean)
], SqlLoggingOptionsInput.prototype, "logRecordChangeMetadata", void 0);
__decorate([
    Field(() => Boolean, { nullable: true }),
    __metadata("design:type", Boolean)
], SqlLoggingOptionsInput.prototype, "retainEmptyLogFiles", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingOptionsInput.prototype, "filterByUserId", void 0);
__decorate([
    Field(() => [String], { nullable: true }),
    __metadata("design:type", Array)
], SqlLoggingOptionsInput.prototype, "filterPatterns", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingOptionsInput.prototype, "filterType", void 0);
__decorate([
    Field(() => Boolean, { nullable: true }),
    __metadata("design:type", Boolean)
], SqlLoggingOptionsInput.prototype, "verboseOutput", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingOptionsInput.prototype, "defaultSchemaName", void 0);
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], SqlLoggingOptionsInput.prototype, "sessionName", void 0);
SqlLoggingOptionsInput = __decorate([
    InputType()
], SqlLoggingOptionsInput);
export { SqlLoggingOptionsInput };
let StartSqlLoggingInput = class StartSqlLoggingInput {
};
__decorate([
    Field(() => String, { nullable: true }),
    __metadata("design:type", String)
], StartSqlLoggingInput.prototype, "fileName", void 0);
__decorate([
    Field(() => SqlLoggingOptionsInput, { nullable: true }),
    __metadata("design:type", SqlLoggingOptionsInput)
], StartSqlLoggingInput.prototype, "options", void 0);
__decorate([
    Field(() => Boolean, { nullable: true }),
    __metadata("design:type", Boolean)
], StartSqlLoggingInput.prototype, "filterToCurrentUser", void 0);
StartSqlLoggingInput = __decorate([
    InputType()
], StartSqlLoggingInput);
export { StartSqlLoggingInput };
let SqlLoggingConfig = class SqlLoggingConfig {
};
__decorate([
    Field(() => Boolean),
    __metadata("design:type", Boolean)
], SqlLoggingConfig.prototype, "enabled", void 0);
__decorate([
    Field(() => SqlLoggingOptions),
    __metadata("design:type", SqlLoggingOptions)
], SqlLoggingConfig.prototype, "defaultOptions", void 0);
__decorate([
    Field(() => String),
    __metadata("design:type", String)
], SqlLoggingConfig.prototype, "allowedLogDirectory", void 0);
__decorate([
    Field(() => Int),
    __metadata("design:type", Number)
], SqlLoggingConfig.prototype, "maxActiveSessions", void 0);
__decorate([
    Field(() => Boolean),
    __metadata("design:type", Boolean)
], SqlLoggingConfig.prototype, "autoCleanupEmptyFiles", void 0);
__decorate([
    Field(() => Int),
    __metadata("design:type", Number)
], SqlLoggingConfig.prototype, "sessionTimeout", void 0);
__decorate([
    Field(() => Int),
    __metadata("design:type", Number)
], SqlLoggingConfig.prototype, "activeSessionCount", void 0);
SqlLoggingConfig = __decorate([
    ObjectType()
], SqlLoggingConfig);
export { SqlLoggingConfig };
/**
 * GraphQL resolver for SQL logging configuration and session management.
 * Provides queries and mutations for controlling SQL logging functionality.
 *
 * **Security**: All operations require Owner-level privileges.
 *
 * @example
 * ```typescript
 * // Start a new logging session
 * const session = await startSqlLogging({
 *   fileName: "my-session.sql",
 *   filterToCurrentUser: true,
 *   options: {
 *     prettyPrint: true,
 *     statementTypes: "both"
 *   }
 * });
 *
 * // Get current configuration
 * const config = await sqlLoggingConfig();
 *
 * // List active sessions
 * const sessions = await activeSqlLoggingSessions();
 *
 * // Stop a session
 * await stopSqlLogging(session.id);
 * ```
 */
let SqlLoggingConfigResolver = class SqlLoggingConfigResolver extends ResolverBase {
    static { SqlLoggingConfigResolver_1 = this; }
    /** Default prefix for auto-generated SQL log filenames */
    static { this.LOG_FILE_PREFIX = 'sql-log-'; }
    /** Track active session timeouts for proper cleanup when sessions are manually stopped */
    static { this.sessionTimeouts = new Map(); }
    /**
     * Validates that the current user has Owner-level privileges required for SQL logging operations.
     *
     * This method performs authentication and authorization checks:
     * - Verifies user is authenticated (has email in context)
     * - Looks up user in UserCache by email (case-insensitive)
     * - Checks that user Type field equals 'Owner' (trimmed for nchar padding)
     *
     * @param context - The GraphQL application context containing user authentication data
     * @returns Promise resolving to the authenticated UserInfo object
     * @throws Error if user is not authenticated, not found, or lacks Owner privileges
     *
     * @private
     */
    async checkOwnerAccess(context) {
        const userEmail = context.userPayload?.email;
        if (!userEmail) {
            throw new Error('User not authenticated');
        }
        // Get the user from cache
        const users = UserCache.Instance.Users;
        const user = users.find(u => u.Email.toLowerCase() === userEmail.toLowerCase());
        if (!user) {
            throw new Error('User not found');
        }
        // Debug logging
        console.log('SQL Logging access check:', {
            email: user.Email,
            type: user.Type,
            typeLength: user.Type?.length,
            typeTrimmed: user.Type?.trim(),
            isOwner: user.Type?.trim().toLowerCase() === 'owner'
        });
        // Check if user has Type = 'Owner' (trim and case-insensitive for nchar fields)
        if (user.Type?.trim().toLowerCase() !== 'owner') {
            throw new Error('Access denied. This feature requires Owner privileges.');
        }
        return user;
    }
    /**
     * Retrieves the current SQL logging configuration and status information.
     *
     * Returns comprehensive configuration details including:
     * - Whether SQL logging is enabled in server config
     * - Default logging options (formatting, statement types, etc.)
     * - File system settings (log directory, cleanup options)
     * - Session limits and timeout settings
     * - Count of currently active logging sessions
     *
     * @param context - GraphQL context (requires Owner privileges)
     * @returns Promise resolving to complete SQL logging configuration
     * @throws Error if user lacks Owner privileges
     *
     * @example
     * ```graphql
     * query {
     *   sqlLoggingConfig {
     *     enabled
     *     activeSessionCount
     *     maxActiveSessions
     *     allowedLogDirectory
     *     defaultOptions {
     *       prettyPrint
     *       statementTypes
     *     }
     *   }
     * }
     * ```
     */
    async sqlLoggingConfig(context) {
        await this.checkOwnerAccess(context);
        const config = await loadConfig();
        const provider = GetReadOnlyProvider(context.providers, { allowFallbackToReadWrite: true });
        const activeSessions = provider.GetActiveSqlLoggingSessions();
        return {
            enabled: config.sqlLogging?.enabled ?? false,
            defaultOptions: config.sqlLogging?.defaultOptions ?? {
                formatAsMigration: false,
                statementTypes: 'both',
                batchSeparator: 'GO',
                prettyPrint: true,
                logRecordChangeMetadata: false,
                retainEmptyLogFiles: false
            },
            allowedLogDirectory: config.sqlLogging?.allowedLogDirectory ?? './logs/sql',
            maxActiveSessions: config.sqlLogging?.maxActiveSessions ?? 5,
            autoCleanupEmptyFiles: config.sqlLogging?.autoCleanupEmptyFiles ?? true,
            sessionTimeout: config.sqlLogging?.sessionTimeout ?? 3600000,
            activeSessionCount: activeSessions.length
        };
    }
    /**
     * Retrieves a list of all currently active SQL logging sessions.
     *
     * Returns detailed information for each active session including:
     * - Unique session identifier and file path
     * - Start time and statement count
     * - Session configuration options
     * - User filtering settings
     *
     * @param context - GraphQL context (requires Owner privileges)
     * @returns Promise resolving to array of active SqlLoggingSession objects
     * @throws Error if user lacks Owner privileges
     *
     * @example
     * ```graphql
     * query {
     *   activeSqlLoggingSessions {
     *     id
     *     sessionName
     *     filePath
     *     startTime
     *     statementCount
     *     filterByUserId
     *     options {
     *       prettyPrint
     *       statementTypes
     *     }
     *   }
     * }
     * ```
     */
    async activeSqlLoggingSessions(context) {
        await this.checkOwnerAccess(context);
        const provider = GetReadOnlyProvider(context.providers, { allowFallbackToReadWrite: true });
        const sessions = provider.GetActiveSqlLoggingSessions();
        return sessions.map(session => ({
            id: session.id,
            filePath: session.filePath,
            startTime: session.startTime,
            statementCount: session.statementCount,
            options: this.convertOptionsToGraphQL(session.options),
            sessionName: session.options.sessionName,
            filterByUserId: session.options.filterByUserId
        }));
    }
    /**
     * Creates and starts a new SQL logging session with specified configuration.
     *
     * This mutation:
     * - Validates SQL logging is enabled and session limits
     * - Creates a secure file path within the allowed log directory
     * - Configures session options (filtering, formatting, etc.)
     * - Starts the logging session in SQLServerDataProvider
     * - Sets up automatic cleanup after session timeout
     *
     * @param input - Configuration for the new logging session
     * @param input.fileName - Optional custom filename for the log file
     * @param input.filterToCurrentUser - Whether to filter SQL to current user only
     * @param input.options - Logging options (formatting, statement types, etc.)
     * @param context - GraphQL context (requires Owner privileges)
     * @returns Promise resolving to the created SqlLoggingSession object
     * @throws Error if logging disabled, session limit reached, or invalid file path
     *
     * @example
     * ```graphql
     * mutation {
     *   startSqlLogging(input: {
     *     fileName: "debug-session.sql"
     *     filterToCurrentUser: true
     *     options: {
     *       prettyPrint: true
     *       statementTypes: "both"
     *       sessionName: "Debug Session"
     *     }
     *   }) {
     *     id
     *     filePath
     *     sessionName
     *   }
     * }
     * ```
     */
    async startSqlLogging(input, context) {
        await this.checkOwnerAccess(context);
        const config = await loadConfig();
        // Check if SQL logging is enabled
        if (!config.sqlLogging?.enabled) {
            throw new Error('SQL logging is not enabled in the server configuration');
        }
        // Check max active sessions
        const provider = GetReadOnlyProvider(context.providers, { allowFallbackToReadWrite: true });
        const activeSessions = provider.GetActiveSqlLoggingSessions();
        if (activeSessions.length >= (config.sqlLogging.maxActiveSessions ?? 5)) {
            throw new Error(`Maximum number of active SQL logging sessions (${config.sqlLogging.maxActiveSessions}) reached`);
        }
        // Prepare file path
        const allowedDir = path.resolve(config.sqlLogging.allowedLogDirectory ?? './logs/sql');
        await this.ensureDirectoryExists(allowedDir);
        const fileName = input.fileName || `${SqlLoggingConfigResolver_1.LOG_FILE_PREFIX}${new Date().toISOString().replace(/[:.]/g, '-')}.sql`;
        const filePath = path.join(allowedDir, fileName);
        // Validate file path is within allowed directory
        const resolvedPath = path.resolve(filePath);
        if (!resolvedPath.startsWith(allowedDir)) {
            throw new Error('Invalid file path - must be within allowed log directory');
        }
        // Merge options with defaults
        const defaultOptions = config.sqlLogging.defaultOptions || {};
        const userInfo = input.filterToCurrentUser ? this.GetUserFromPayload(context.userPayload) : undefined;
        const sessionOptions = {
            ...defaultOptions,
            ...input.options,
            sessionName: input.options?.sessionName || `Session started by ${context.userPayload.email}`,
            filterByUserId: input.filterToCurrentUser ? userInfo?.ID : input.options?.filterByUserId
        };
        // Create the logging session
        const session = await provider.CreateSqlLogger(filePath, sessionOptions);
        // Set up auto-cleanup after timeout with proper tracking
        if (config.sqlLogging.sessionTimeout > 0) {
            const timeoutId = setTimeout(async () => {
                try {
                    await session.dispose();
                    SqlLoggingConfigResolver_1.sessionTimeouts.delete(session.id);
                }
                catch (e) {
                    // Session might already be disposed - log for debugging
                    console.warn(`Auto-cleanup failed for SQL logging session ${session.id}:`, e);
                }
            }, config.sqlLogging.sessionTimeout);
            // Track the timeout so we can cancel it if session is manually stopped
            SqlLoggingConfigResolver_1.sessionTimeouts.set(session.id, timeoutId);
        }
        return {
            id: session.id,
            filePath: session.filePath,
            startTime: session.startTime,
            statementCount: session.statementCount,
            options: this.convertOptionsToGraphQL(session.options),
            sessionName: session.options.sessionName,
            filterByUserId: session.options.filterByUserId,
        };
    }
    /**
     * Stops and disposes of a specific SQL logging session.
     *
     * This mutation:
     * - Validates the session exists and user has access
     * - Calls dispose() on the session to close file handles
     * - Removes the session from the active sessions map
     * - Performs any configured cleanup operations
     *
     * @param sessionId - Unique identifier of the session to stop
     * @param context - GraphQL context (requires Owner privileges)
     * @returns Promise resolving to true if session was successfully stopped
     * @throws Error if session not found or user lacks Owner privileges
     *
     * @example
     * ```graphql
     * mutation {
     *   stopSqlLogging(sessionId: "session-123-456")
     * }
     * ```
     */
    async stopSqlLogging(sessionId, context) {
        await this.checkOwnerAccess(context);
        const provider = GetReadOnlyProvider(context.providers, { allowFallbackToReadWrite: true });
        // Use the public method to get and dispose the session
        const session = provider.GetSqlLoggingSessionById(sessionId);
        if (!session) {
            throw new Error(`SQL logging session ${sessionId} not found`);
        }
        // Clear any scheduled timeout for this session
        const timeoutId = SqlLoggingConfigResolver_1.sessionTimeouts.get(sessionId);
        if (timeoutId) {
            clearTimeout(timeoutId);
            SqlLoggingConfigResolver_1.sessionTimeouts.delete(sessionId);
        }
        await session.dispose();
        return true;
    }
    /**
     * Stops and disposes of all currently active SQL logging sessions.
     *
     * This is a convenience method that:
     * - Calls DisposeAllSqlLoggingSessions() on the data provider
     * - Ensures all file handles are properly closed
     * - Clears the active sessions map
     * - Performs cleanup for all sessions at once
     *
     * @param context - GraphQL context (requires Owner privileges)
     * @returns Promise resolving to true if all sessions were successfully stopped
     * @throws Error if user lacks Owner privileges
     *
     * @example
     * ```graphql
     * mutation {
     *   stopAllSqlLogging
     * }
     * ```
     */
    async stopAllSqlLogging(context) {
        await this.checkOwnerAccess(context);
        const provider = GetReadOnlyProvider(context.providers, { allowFallbackToReadWrite: true });
        await provider.DisposeAllSqlLoggingSessions();
        return true;
    }
    /**
     * Updates the default SQL logging options for new sessions.
     *
     * **Note**: This updates runtime configuration only, not the static config file.
     * Changes apply to new sessions but do not persist across server restarts.
     * In a production system, consider persisting changes to a database.
     *
     * @param options - New default options to apply (partial update supported)
     * @param context - GraphQL context (requires Owner privileges)
     * @returns Promise resolving to the updated SqlLoggingOptions object
     * @throws Error if SQL logging not configured or user lacks Owner privileges
     *
     * @example
     * ```graphql
     * mutation {
     *   updateSqlLoggingDefaults(options: {
     *     prettyPrint: true
     *     statementTypes: "both"
     *     logRecordChangeMetadata: false
     *   }) {
     *     prettyPrint
     *     statementTypes
     *     formatAsMigration
     *   }
     * }
     * ```
     */
    async updateSqlLoggingDefaults(options, context) {
        await this.checkOwnerAccess(context);
        // Note: This updates the runtime configuration only, not the file
        // In a production system, you might want to persist this to a database
        const config = await loadConfig();
        if (!config.sqlLogging) {
            throw new Error('SQL logging configuration not found');
        }
        config.sqlLogging.defaultOptions = {
            ...config.sqlLogging.defaultOptions,
            ...options
        };
        return config.sqlLogging.defaultOptions;
    }
    /**
     * Reads the contents of a specific SQL log file.
     *
     * This method:
     * - Validates the session exists and user has access
     * - Ensures the file path is within the allowed log directory
     * - Reads the file content with optional line limits
     * - Returns the content as a string
     *
     * @param sessionId - Unique identifier of the logging session
     * @param maxLines - Maximum number of lines to read (optional, defaults to all)
     * @param context - GraphQL context (requires Owner privileges)
     * @returns Promise resolving to the log file content
     * @throws Error if session not found, file not accessible, or user lacks privileges
     *
     * @example
     * ```graphql
     * query {
     *   readSqlLogFile(sessionId: "session-123", maxLines: 100)
     * }
     * ```
     */
    async readSqlLogFile(sessionId, maxLines, context) {
        await this.checkOwnerAccess(context);
        const config = await loadConfig();
        // Check if SQL logging is enabled
        if (!config.sqlLogging?.enabled) {
            throw new Error('SQL logging is not enabled in the server configuration');
        }
        // Find the session
        const provider = GetReadOnlyProvider(context.providers, { allowFallbackToReadWrite: true });
        const sessions = provider.GetActiveSqlLoggingSessions();
        const session = sessions.find((s) => s.id === sessionId);
        if (!session) {
            throw new Error(`SQL logging session ${sessionId} not found`);
        }
        // Validate file path is within allowed directory
        const allowedDir = path.resolve(config.sqlLogging.allowedLogDirectory ?? './logs/sql');
        const resolvedPath = path.resolve(session.filePath);
        if (!resolvedPath.startsWith(allowedDir)) {
            throw new Error('Access denied - file path outside allowed directory');
        }
        try {
            // Check if file exists
            await fs.access(session.filePath);
            // Read file content
            const content = await fs.readFile(session.filePath, 'utf-8');
            // Apply line limit if specified
            if (maxLines && maxLines > 0) {
                const lines = content.split('\n');
                if (lines.length > maxLines) {
                    // Return the last N lines
                    return lines.slice(-maxLines).join('\n');
                }
            }
            return content;
        }
        catch (error) {
            if (error.code === 'ENOENT') {
                return '-- Log file not yet created or is empty --';
            }
            throw new Error(`Failed to read log file: ${error.message}`);
        }
    }
    /**
     * Debug query to check what the current user email is in the SQL provider.
     * This helps diagnose user filtering issues when SQL statements aren't being captured.
     *
     * Returns a comparison of the user email stored in the SQLServerDataProvider
     * versus the user email from the GraphQL context, which helps identify mismatches
     * that could prevent SQL filtering from working correctly.
     *
     * @param context - GraphQL context containing user information
     * @returns Formatted string showing both email values and whether they match
     * @throws Error if user doesn't have Owner privileges
     */
    async debugCurrentUserEmail(context) {
        await this.checkOwnerAccess(context);
        const contextUserEmail = context.userPayload?.email || 'NOT_SET';
        return `Context User Email: "${contextUserEmail}" | Note: Provider no longer stores user email - uses contextUser parameter for SQL logging`;
    }
    /**
     * Ensures the specified log directory exists, creating it if necessary.
     *
     * This method:
     * - Attempts to access the directory to check if it exists
     * - Creates the directory recursively if it doesn't exist
     * - Handles permission and file system errors gracefully
     *
     * @param dir - Absolute path to the directory to ensure exists
     * @throws Error if directory cannot be created due to permissions or other issues
     *
     * @private
     */
    async ensureDirectoryExists(dir) {
        try {
            await fs.access(dir);
        }
        catch {
            await fs.mkdir(dir, { recursive: true });
        }
    }
    /**
     * Converts SqlLoggingOptions from the provider to the GraphQL-compatible type.
     * The provider's filterPatterns can contain RegExp objects, but GraphQL only supports strings.
     *
     * @param options - Options from SQLServerDataProvider
     * @returns GraphQL-compatible SqlLoggingOptions
     * @private
     */
    convertOptionsToGraphQL(options) {
        return {
            formatAsMigration: options.formatAsMigration,
            description: options.description,
            statementTypes: options.statementTypes,
            batchSeparator: options.batchSeparator,
            prettyPrint: options.prettyPrint,
            logRecordChangeMetadata: options.logRecordChangeMetadata,
            retainEmptyLogFiles: options.retainEmptyLogFiles,
            filterByUserId: options.filterByUserId,
            sessionName: options.sessionName,
            verboseOutput: options.verboseOutput,
            defaultSchemaName: options.defaultSchemaName,
            // Convert RegExp objects to their string representation
            filterPatterns: options.filterPatterns?.map((p) => (p instanceof RegExp ? p.toString() : String(p))),
            filterType: options.filterType,
        };
    }
};
__decorate([
    Query(() => SqlLoggingConfig),
    __param(0, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SqlLoggingConfigResolver.prototype, "sqlLoggingConfig", null);
__decorate([
    Query(() => [SqlLoggingSession]),
    __param(0, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SqlLoggingConfigResolver.prototype, "activeSqlLoggingSessions", null);
__decorate([
    Mutation(() => SqlLoggingSession),
    __param(0, Arg('input', () => StartSqlLoggingInput)),
    __param(1, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [StartSqlLoggingInput, Object]),
    __metadata("design:returntype", Promise)
], SqlLoggingConfigResolver.prototype, "startSqlLogging", null);
__decorate([
    Mutation(() => Boolean),
    __param(0, Arg('sessionId', () => String)),
    __param(1, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], SqlLoggingConfigResolver.prototype, "stopSqlLogging", null);
__decorate([
    Mutation(() => Boolean),
    __param(0, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SqlLoggingConfigResolver.prototype, "stopAllSqlLogging", null);
__decorate([
    Mutation(() => SqlLoggingOptions),
    __param(0, Arg('options', () => SqlLoggingOptionsInput)),
    __param(1, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [SqlLoggingOptionsInput, Object]),
    __metadata("design:returntype", Promise)
], SqlLoggingConfigResolver.prototype, "updateSqlLoggingDefaults", null);
__decorate([
    Query(() => String),
    __param(0, Arg('sessionId', () => String)),
    __param(1, Arg('maxLines', () => Int, { nullable: true })),
    __param(2, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Number, Object]),
    __metadata("design:returntype", Promise)
], SqlLoggingConfigResolver.prototype, "readSqlLogFile", null);
__decorate([
    Query(() => String),
    __param(0, Ctx()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SqlLoggingConfigResolver.prototype, "debugCurrentUserEmail", null);
SqlLoggingConfigResolver = SqlLoggingConfigResolver_1 = __decorate([
    Resolver()
], SqlLoggingConfigResolver);
export { SqlLoggingConfigResolver };
//# sourceMappingURL=SqlLoggingConfigResolver.js.map