// db.ts
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
import * as portfolioSchema from "@/app/db/schema/portfolio";
import * as propertySchema from "@/app/db/schema/property";
import * as recommendationSchema from "@/app/db/schema/recommendations";
import * as materialSchema from "@/app/db/schema/materials";
import * as solarSchema from "@/app/db/schema/solar";
import * as EnergyAssessmentsSchema from "@/app/db/schema/energy_assessments";
import * as FundingSchema from "@/app/db/schema/funding";
import * as Relations from "@/app/db/schema/relations";
import * as Users from "@/app/db/schema/users";
import { tasks } from "@/app/db/schema/tasks/tasks";
import { subTasks } from "@/app/db/schema/tasks/subtask";
import * as CrmSchema from "@/app/db/schema/crm/hubspot_deal_table";
import * as UploadedFilesSchema from "@/app/db/schema/uploaded_files";
import * as PortfolioOrgSchema from "@/app/db/schema/portfolio_organisation";
import * as BulkAddressUploadsSchema from "@/app/db/schema/bulk_address_uploads";
import * as TagsSchema from "@/app/db/schema/tags";

export const pool = new Pool({
  host: process.env.DB_HOST,
  port: Number(process.env.DB_PORT),
  user: process.env.DB_USERNAME,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
  max: 20,
  idleTimeoutMillis: 30000,
});

const schema = {
  ...portfolioSchema,
  ...propertySchema,
  ...recommendationSchema,
  ...materialSchema,
  ...solarSchema,
  ...Relations,
  ...EnergyAssessmentsSchema,
  ...FundingSchema,
  ...Users,
  tasks,
  subTasks,
  ...CrmSchema,
  ...UploadedFilesSchema,
  ...PortfolioOrgSchema,
  ...BulkAddressUploadsSchema,
  ...TagsSchema,
};

export const db = drizzle(pool, {
  schema: schema,
});
