import { d as db, s as storageConfig } from '../../admin/create-upload-progress-table/POST-C-OoiMoW.js';
import { eq, desc } from 'drizzle-orm';
import { g as getBackupsDir, b as getPathInfo } from '../../auth/setup/POST-DYMc2GPx.js';
import fs from 'fs';
import path from 'path';

async function handler(req, res) {
  try {
    const configs = await db.select().from(storageConfig).where(eq(storageConfig.isActive, true)).orderBy(desc(storageConfig.createdAt)).limit(1);
    if (configs.length === 0) {
      return res.json({
        success: true,
        config: {
          id: 0,
          storageType: "local",
          config: JSON.stringify({ basePath: getBackupsDir() }),
          isActive: true
        }
      });
    }
    const config = configs[0];
    const pathInfo = getPathInfo();
    const limitsConfigPath = path.join(pathInfo.appDataDir, "storage-config.json");
    let storageConfigLimits = null;
    if (fs.existsSync(limitsConfigPath)) {
      const configData = fs.readFileSync(limitsConfigPath, "utf-8");
      storageConfigLimits = JSON.parse(configData);
    }
    res.json({
      success: true,
      config: {
        ...config,
        config: JSON.parse(config.config)
      },
      pathInfo,
      storageConfig: storageConfigLimits
    });
  } catch (error) {
    console.error("Error fetching storage config:", error);
    res.status(500).json({
      success: false,
      error: "Failed to fetch storage configuration",
      message: String(error)
    });
  }
}

export { handler as h };
