import { pgTable, bigint, uuid, timestamp, unique } from "drizzle-orm/pg-core";
import { portfolio } from "./portfolio";
import { organisation } from "./organisation";
import { InferModel } from "drizzle-orm";

export const portfolioOrganisation = pgTable("portfolio_organisation", {
  id: uuid("id").defaultRandom().primaryKey(),
  portfolioId: bigint("portfolio_id", { mode: "bigint" })
    .notNull()
    .references(() => portfolio.id, { onDelete: "cascade" }),
  organisationId: uuid("organisation_id")
    .notNull()
    .references(() => organisation.id, { onDelete: "cascade" }),
  createdAt: timestamp("created_at", { precision: 6, withTimezone: true })
    .defaultNow()
    .notNull(),
  updatedAt: timestamp("updated_at", { precision: 6, withTimezone: true })
    .defaultNow()
    .notNull(),
}, (t) => ({
  portfolioOrgUnique: unique().on(t.portfolioId, t.organisationId),
}));

export type PortfolioOrganisation = InferModel<typeof portfolioOrganisation, "select">;
export type NewPortfolioOrganisation = InferModel<typeof portfolioOrganisation, "insert">;
