import { z } from "zod";
import { DynaBridge, DynaBridgeEntity } from 'dynabridge';

export const DOOR_CONFIG_SK = "config";
export const EDIT_DOOR_CONFIG_SK = "config-update";

export const DoorConfigSchema = z.object({
  // keys
  PK: z.string().startsWith("door-", "Invalid door key"),
  SK: z.literal(DOOR_CONFIG_SK).default(DOOR_CONFIG_SK),

  buzzer: z.string(),
  buzzerCode: z.string(),
  discordUsers: z.array(z.string()),
  fallbackNumbers: z.array(z.string()),
  pin: z.string(),
  pressKey: z.string(),
  greeting: z.string(),
  timeout: z.number(),
});

export const getDoorConfigID = (doorName: string): string[] => {
  return ['door-' + doorName, DOOR_CONFIG_SK];
}

export const EditDoorConfigSchema = DoorConfigSchema.extend({
  SK: z.literal(EDIT_DOOR_CONFIG_SK).default(EDIT_DOOR_CONFIG_SK),

  approvalId: z.string(),
});

export const getEditDoorConfigID = (doorName: string): string[] => {
  return getDoorConfigID(doorName).with(1, EDIT_DOOR_CONFIG_SK);
}

export type DoorConfig = z.infer<typeof DoorConfigSchema>;
export type EditDoorConfig = z.infer<typeof EditDoorConfigSchema>;

export const DoorConfigEntity: DynaBridgeEntity<DoorConfig> = {
  tableName: "doorman",
  id: ["PK", "SK"],
};

export const EditDoorConfigEntity: DynaBridgeEntity<EditDoorConfig> = {
  tableName: "doorman",
  id: ["PK", "SK"],
};
