import {
  bigserial,
  timestamp,
  pgTable,
  real,
  pgEnum,
  bigint,
} from "drizzle-orm/pg-core";
import { InferModel } from "drizzle-orm";
import { plan } from "./recommendations";
import { material, materialTypeEnum } from "./materials"


export const SchemeEnum: [string, ...string[]] = ["eco4", "gbis", "whlg", "none"];
export const Scheme = pgEnum("scheme", SchemeEnum);

// funding package table which has the aggregate level data for the funding package. A funding package will be
// tied to a plan
export const fundingPackage = pgTable("funding_package", {
    id: bigserial("id", { mode: "bigint" }).primaryKey(),
    planId: bigint("plan_id", { mode: "bigint" })
        .notNull()
        .references(() => plan.id),
    scheme: Scheme("scheme"),
    createdAt: timestamp("created_at").notNull().defaultNow(),
    projectFunding: real("project_funding"),
    totalUplift: real("total_uplift"),
    fullProjectScore: real("full_project_score"),
    partialProjectScore: real("partial_project_score"),
    upliftProjectScore: real("uplift_project_score"),
})

export const fundingPackageMeasures = pgTable("funding_package_measures", {
    id: bigserial("id", { mode: "bigint" }).primaryKey(),
    fundingPackageId: bigint("funding_package_id", { mode: "bigint" })
        .notNull()
        .references(() => fundingPackage.id),
    measure: materialTypeEnum("measure").notNull(),
    materialId: bigint("material_id", { mode: "bigint" })
        .references(() => material.id),  // May be null initially
    innovationUplift: real("innovation_uplift"),
    partialProjectScore: real("partial_project_score"),
    upliftProjectScore: real("uplift_project_score"),
});


export type FundingPackage = typeof fundingPackage.$inferSelect;
export type FundingPackageMeasure = typeof fundingPackageMeasures.$inferSelect;

export type FundingPackageWithMeasures = FundingPackage & {
    fundingPackageMeasures: FundingPackageMeasure[];
};