import { z } from "zod";
import { DynaBridgeEntity } from 'dynabridge';
import { getFutureTTLInSeconds, isTTLInFuture } from "../common/TTLHelper";

export const LOCK_STATUS_SK = "lock";

export const LockStatusSchema = z.object({
  // keys
  PK: z.string(), // door name
  SK: z.literal(LOCK_STATUS_SK).default(LOCK_STATUS_SK),

  TTL: z.number(),
  fingerprint: z.string(),
});

export const getLockStatusID = (door: string): string[] => {
  return [door, LOCK_STATUS_SK];
}

export type LockStatus = z.infer<typeof LockStatusSchema>;

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

export const isLockOpen = (lock?: LockStatus) => {
  return isTTLInFuture(lock);
};

export const createLockStatusWithTimeout = (door: string, timeoutSeconds: number, fingerprint: any): LockStatus => {
  return {
    PK: door,
    SK: LOCK_STATUS_SK,
    TTL: getFutureTTLInSeconds(timeoutSeconds),
    fingerprint: JSON.stringify(fingerprint),
  };
};
