import {
    pgTable,
    uuid,
    bigint,
    timestamp,
    integer,
    text,
} from "drizzle-orm/pg-core"

import { stafftimeentries } from "./staff_time_entries"
import {sql} from "drizzle-orm";

export const stafftimenetryconnects = pgTable("staff_time_entry_connects", {
    id: uuid("id").primaryKey().defaultRandom(),

    stafftimeentry: uuid("time_entry_id")
        .notNull()
        .references(() => stafftimeentries.id, { onDelete: "cascade" }),

    project_id: bigint("project_id", { mode: "number" }), // referenziert später projects.id

    started_at: timestamp("started_at", { withTimezone: true }).notNull(),
    stopped_at: timestamp("stopped_at", { withTimezone: true }).notNull(),

    durationMinutes: integer("duration_minutes").generatedAlwaysAs(
        sql`(EXTRACT(epoch FROM (stopped_at - started_at)) / 60)`
    ),

    notes: text("notes"),

    created_at: timestamp("created_at", { withTimezone: true }).defaultNow(),
    updated_at: timestamp("updated_at", { withTimezone: true }).defaultNow(),
})

export type StaffTimeEntryConnect =
    typeof stafftimenetryconnects.$inferSelect
export type NewStaffTimeEntryConnect =
    typeof stafftimenetryconnects.$inferInsert
