import {
    text,
    pgTable,
    numeric,
    index,
    timestamp,
    boolean,
    uuid,
    uniqueIndex,
} from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { z } from "zod";
import { users } from "./users.js";
import { roles } from "./roles.js";
import { modules } from "./modules.js";

/*
we will add the user
the module they have access to 
and there role for this module. default will be user role, and view for production.

systemAdmin will just get admin to all modules.
*/

export const userRoles = pgTable(
    "userRoles",
    {
        user_id: uuid("user_id")
            .notNull()
            .references(() => users.user_id),
        role_id: uuid("role_id")
            .notNull()
            .references(() => roles.role_id),
        module_id: uuid("module_id")
            .notNull()
            .references(() => modules.module_id),
        role: text("role").notNull(), // "view", "technician", "supervisor","manager", "admin", "systemAdmin"
        add_User: text("add_User").default("LST_System").notNull(),
        add_Date: timestamp("add_Date").defaultNow(),
        upd_user: text("upd_User").default("LST_System").notNull(),
        upd_date: timestamp("upd_date").defaultNow(),
    },
    (table) => {
        // ensures only one user gets permissions to one role
        return [
            uniqueIndex("user_module_unique").on(
                table.user_id,
                table.module_id
            ),
        ];
    }
);

// Schema for inserting a user - can be used to validate API requests
// export const insertUserRolesSchema = createInsertSchema(userRoles, {
//     role: z.string().min(3, {message: "Role must be at least 3 characters"}),
// });
// Schema for selecting a Expenses - can be used to validate API responses
export const selectUserRolesSchema = createSelectSchema(userRoles);
