import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { defineConfig } from 'drizzle-kit';

const envPath = resolve(__dirname, '../../../.env');
try {
  const text = readFileSync(envPath, 'utf-8');
  for (const line of text.split('\n')) {
    const match = line.match(/^(\w+)=(.*)$/);
    if (match) {
      const [, key, val] = match;
      if (!process.env[key!]) process.env[key!] = val!.replace(/^["']|["']$/g, '');
    }
  }
} catch {}

export default defineConfig({
  schema: './src/schema.ts',
  out: './migrations',
  dialect: 'postgresql',
  dbCredentials: {
    url: process.env.POSTGRES_URL!,
  },
});
