export const FOLLOW_REQUEST_APPROVED_EVENT = 'follow.request.approved' as const;

export interface FollowRequestApprovedEvent {
  eventId: string;
  eventType: typeof FOLLOW_REQUEST_APPROVED_EVENT;
  occurredAt: string;
  payload: {
    requestId: string;
    actorId: string;
    recipientId: string;
  };
}

export function isFollowRequestApprovedEvent(value: unknown): value is FollowRequestApprovedEvent {
  if (!value || typeof value !== 'object') return false;
  const event = value as Partial<FollowRequestApprovedEvent>;
  const payload = event.payload as Partial<FollowRequestApprovedEvent['payload']> | undefined;
  return event.eventType === FOLLOW_REQUEST_APPROVED_EVENT
    && isNonEmptyString(event.eventId)
    && isIsoDateString(event.occurredAt)
    && isNonEmptyString(payload?.requestId)
    && isNonEmptyString(payload.actorId)
    && isNonEmptyString(payload.recipientId);
}

const isNonEmptyString = (value: unknown): value is string =>
  typeof value === 'string' && value.trim().length > 0;

const isIsoDateString = (value: unknown): value is string => {
  if (typeof value !== 'string') return false;
  const timestamp = Date.parse(value);
  if (Number.isNaN(timestamp)) return false;
  return new Date(timestamp).toISOString() === value;
};
