/**
 * Character Availability System
 *
 * Determines whether an NPC would realistically respond to a message
 * based on the current game time, their relationship to the player,
 * and their affinity level.
 */

/** Relationship types where the NPC has professional boundaries */
const PROFESSIONAL_RELATIONSHIPS = [
  'teacher', 'professor', 'instructor',
  'boss', 'manager', 'supervisor',
  'coach', 'doctor', 'therapist', 'counselor',
];

/** Relationship types considered distant/casual */
const DISTANT_RELATIONSHIPS = [
  'classmate', 'coworker', 'colleague',
  'neighbor', 'acquaintance', 'stranger',
];

/** Relationship types considered romantic/very close */
const ROMANTIC_RELATIONSHIPS = [
  'dating_match', 'partner', 'dating',
  'girlfriend', 'boyfriend',
  'fiancé', 'fiancée', 'engaged',
];

/** Spouse-level relationships (always available) */
const SPOUSE_RELATIONSHIPS = ['spouse', 'wife', 'husband'];

/** Close non-romantic relationships */
const CLOSE_RELATIONSHIPS = [
  'best friend', 'bestfriend', 'bff',
  'crush',
];

/** Family relationships */
const FAMILY_RELATIONSHIPS = [
  'father', 'mother', 'child', 'sibling', 'family',
  'brother', 'sister', 'son', 'daughter',
  'aunt', 'uncle', 'cousin',
  'paternal_grandmother', 'paternal_grandfather',
  'maternal_grandmother', 'maternal_grandfather',
];

export interface AvailabilityResult {
  available: boolean;
  message?: string;
}

/**
 * Determine if a character would realistically respond at the current game hour.
 *
 * Tier 1 — Professional: 8am-5pm only
 * Tier 2 — Acquaintance/Distant: 8am-10pm
 * Tier 3 — Friend/Family: 7am-11pm
 * Tier 4 — Close/Romantic (affinity > 60): 7am-1am
 * Tier 5 — Spouse (always available)
 */
export function getCharacterAvailability(
  hour: number,
  relationships: string[],
  affinity: number,
  firstname: string,
): AvailabilityResult {
  const rels = relationships.map(r => r.toLowerCase());

  // Tier 5 — Spouse: always available
  if (SPOUSE_RELATIONSHIPS.some(r => rels.includes(r)) && affinity > 50) {
    return { available: true };
  }

  // Tier 1 — Professional: 8am-5pm
  if (PROFESSIONAL_RELATIONSHIPS.some(r => rels.includes(r))) {
    if (hour >= 8 && hour <= 17) {
      return { available: true };
    }
    return {
      available: false,
      message: `Delivered. ${firstname} will see this during their working hours.`,
    };
  }

  // Tier 4 — Close/Romantic: 7am-1am
  const isRomantic = ROMANTIC_RELATIONSHIPS.some(r => rels.includes(r));
  const isClose = CLOSE_RELATIONSHIPS.some(r => rels.includes(r));
  if (isRomantic || isClose || affinity > 60) {
    // Available 7am through 1am (hours 7-23 and hour 0)
    if ((hour >= 7 && hour <= 23) || hour === 0) {
      return { available: true };
    }
    return {
      available: false,
      message: `${firstname} is probably asleep.`,
    };
  }

  // Tier 2 — Acquaintance/Distant: 8am-10pm
  const isDistant = DISTANT_RELATIONSHIPS.some(r => rels.includes(r));
  if (isDistant || affinity < 25) {
    if (hour >= 8 && hour <= 21) {
      return { available: true };
    }
    return {
      available: false,
      message: `${firstname} is probably asleep. They'll see your message later.`,
    };
  }

  // Tier 3 — Friend/Family (default): 7am-11pm
  if (hour >= 7 && hour <= 22) {
    return { available: true };
  }
  return {
    available: false,
    message: `${firstname} is probably asleep.`,
  };
}
