import { relations } from 'drizzle-orm';
import { pgTable,serial,char, primaryKey,integer,index,uniqueIndex,timestamp,unique, varchar, uuid } from 'drizzle-orm/pg-core';
import { project } from './project';
import { materials } from './materials';
export const projectMaterials = pgTable("projectMaterials", {
id: uuid("id").primaryKey().defaultRandom().notNull(),
projectName:varchar('project_name',{length:255}).notNull(),
projectId:uuid('project_id').references(()=>project.id,{onDelete:'cascade',onUpdate:'cascade'}).notNull(),
materialId:uuid('material_id').references(()=>materials.id,{onDelete:'cascade',onUpdate:'cascade'}).notNull(),
requiredQuantity:integer('required_quantity').notNull(),
usedQuantity:integer('used_quantity').notNull().default(0),
availableQuantity:integer('available_quantity').notNull().default(0)
},(t)=>({
unique_material_project:unique().on(t.materialId,t.projectId),
projectMaterials_project:index("projectMaterials_project").on(t.projectId),
materialId:index('material_id').on(t.materialId)
}));
export const projectMaterialsRelations=relations(projectMaterials,({one})=>({
materials:one(materials,{
fields:[projectMaterials.materialId],
references:[materials.id]
}),
project:one(project,{
fields:[projectMaterials.projectId],
references:[project.id]
})
}))
export type ProjectMaterialsSelect= typeof projectMaterials.$inferInsert
export type ProjectMaterialsInsert= typeof projectMaterials.$inferInsert