import { z } from "zod";
import { DynaBridgeEntity } from 'dynabridge';
import { randomUUID } from "crypto";

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

export const ONBOARDING_DOOR_NAME = "onboardingflag";
export const ONBOARDING_DOOR_PIN = "1234";

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().default(""),
  pressKey: z.string(),
  greeting: z.string().optional(),
  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[] => {
  const id = getDoorConfigID(doorName);
  id[1] = EDIT_DOOR_CONFIG_SK;
  return id;
}

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

export const EditDoorConfigReqSchema = EditDoorConfigSchema.omit({ "PK": true, "SK": true, "approvalId": true });
export type EditDoorConfigReq = z.infer<typeof EditDoorConfigReqSchema>;

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

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

export const editDoorToDoorConfig = (updateDoor: EditDoorConfig): DoorConfig => {
  const newItem: any = {
    ...updateDoor,
    SK: DOOR_CONFIG_SK,
  };

  delete newItem.approvalId;

  return newItem;
}

export const createEditDoorConfig = (door: string, newConfig: EditDoorConfigReq): EditDoorConfig => {
  return {
    ...newConfig,
    PK: "door-" + door,
    SK: EDIT_DOOR_CONFIG_SK,
    approvalId: randomUUID().toString(),
  };
}

// onboard a new door
export const ONBOARD_DOOR_CONFIG_SK = "onboard-config";

export const OnboardDoorConfigSchema = DoorConfigSchema.omit({ discordUsers: true }).extend({
  PK: z.string(), // this is the door name
  SK: z.literal(ONBOARD_DOOR_CONFIG_SK).default(ONBOARD_DOOR_CONFIG_SK),
  name: z.string(),
  nonce: z.string(), // random UUID, used to check the oauth is actually from this user who initiated it
});

export type OnboardDoorConfig = z.infer<typeof OnboardDoorConfigSchema>;

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

export const getOnboardDoorId = (code: string): string[] => {
  return [
    code,
    ONBOARD_DOOR_CONFIG_SK,
  ];
}

export const OnboardDoorReqSchema = OnboardDoorConfigSchema.omit({ PK: true, SK: true, nonce: true });
export type OnboardDoorReq = z.infer<typeof OnboardDoorReqSchema>;

export const createOnboardDoorConfig = (req: OnboardDoorReq): OnboardDoorConfig => {
  return {
    ...req,
    PK: req.name,
    SK: ONBOARD_DOOR_CONFIG_SK,
    nonce: randomUUID(),
  };
}
