// The run's regulars: a small stable cast generated from the RUN seed, so the
// same faces turn up Thursday, Friday, and next week at the better venue.
// Design §4.1: deny one twice and the third appearance arrives in a bad fake
// moustache — same walk, same card, new "identity".

import { SeededRNG } from '../core/SeededRNG';
import { generatePatron } from './generator';
import type { Patron } from '../data/types';

export const REGULAR_CAST_SIZE = 6;

/**
 * Deterministic per run seed and independent of every night stream (its own
 * SeededRNG instance). The cast members are full Patron templates — the queue
 * adopts their IDENTITY (face, age, licence) onto freshly rolled nights
 * (tonight's outfit, tonight's drinks) at spawn time.
 */
export function buildRegularCast(runSeed: number, nightDate: Date): Patron[] {
  const rng = new SeededRNG(runSeed ^ 0x5eed);
  const cast: Patron[] = [];
  for (let i = 0; i < REGULAR_CAST_SIZE; i++) {
    const p = generatePatron({ rng, nightDate }, 0, 'regular');
    p.flags.regularId = `reg${i}`;
    cast.push(p);
  }
  return cast;
}

/**
 * Dress tonight's rolled regular in a cast member's identity. The roll keeps
 * its outfit/intoxication (people change clothes and moods); the cast supplies
 * who they ARE: face, age, licence, tolerance — and the grudge bookkeeping.
 */
export function adoptCastIdentity(
  rolled: Patron,
  member: Patron,
  timesDenied: number,
  disguised: boolean,
): Patron {
  return {
    ...rolled,
    dollSeed: member.dollSeed,
    age: member.age,
    idCard: { ...member.idCard },
    tolerance: member.tolerance,
    flags: {
      ...rolled.flags,
      regularId: member.flags.regularId!,
      ...(timesDenied > 0 ? { timesDeniedBefore: timesDenied } : {}),
      ...(disguised ? { disguised: true } : {}),
    },
  };
}
