import { describe, expect, it } from 'vitest';
import {
  SERVE_DRINK_STEP,
  isRsaBreach,
  judgeCall,
} from '../../src/scenes/floor/barShift';
import { WATER_SOBER_STEP } from '../../src/scenes/floor/hazards';
import { BAR_ORDER_LINES } from '../../src/data/strings/floor';
import type { DrunkStage } from '../../src/data/types';

const STAGES: readonly DrunkStage[] = ['sober', 'tipsy', 'loose', 'messy', 'maggot'];

describe('isRsaBreach', () => {
  it('draws the line at messy — loose is the decide band, same as the floor cut-off', () => {
    expect(isRsaBreach('sober')).toBe(false);
    expect(isRsaBreach('tipsy')).toBe(false);
    expect(isRsaBreach('loose')).toBe(false);
    expect(isRsaBreach('messy')).toBe(true);
    expect(isRsaBreach('maggot')).toBe(true);
  });
});

describe('judgeCall / serve', () => {
  it('a legal pour pays vibe and adds a real drink', () => {
    const out = judgeCall('serve', 'tipsy');
    expect(out).toMatchObject({ vibe: 1, breach: false, sendsHome: false });
    expect(out.intoxDelta).toBe(SERVE_DRINK_STEP);
  });

  it('a breach pour STILL pays its vibe up front — the price arrives later', () => {
    for (const stage of ['messy', 'maggot'] as const) {
      const out = judgeCall('serve', stage);
      expect(out.vibe).toBe(1);
      expect(out.breach).toBe(true);
      expect(out.intoxDelta).toBe(SERVE_DRINK_STEP);
    }
  });

  it('never sends anyone home — a served patron stays a problem', () => {
    for (const stage of STAGES) expect(judgeCall('serve', stage).sendsHome).toBe(false);
  });
});

describe('judgeCall / water', () => {
  it('the good catch: loose and up take the cup and improve', () => {
    for (const stage of ['loose', 'messy', 'maggot'] as const) {
      const out = judgeCall('water', stage);
      expect(out.vibe).toBe(1);
      expect(out.aggro).toBeUndefined();
      expect(out.intoxDelta).toBe(-WATER_SOBER_STEP);
    }
  });

  it('watering the sober is a snub — they ordered a beer', () => {
    for (const stage of ['sober', 'tipsy'] as const) {
      const out = judgeCall('water', stage);
      expect(out.aggro).toBe(1);
      expect(out.vibe).toBeUndefined();
    }
  });

  it('is never a breach — water cannot be an offence', () => {
    for (const stage of STAGES) expect(judgeCall('water', stage).breach).toBe(false);
  });
});

describe('judgeCall / cutoff', () => {
  it('always sends them home, whoever they were', () => {
    for (const stage of STAGES) expect(judgeCall('cutoff', stage).sendsHome).toBe(true);
  });

  it('right on a breach-stage patron: a scene, but the room is relieved', () => {
    const out = judgeCall('cutoff', 'maggot');
    expect(out).toMatchObject({ vibe: 1, aggro: 1 });
  });

  it('early on someone upright: they tell the whole queue about you', () => {
    const out = judgeCall('cutoff', 'tipsy');
    expect(out).toMatchObject({ vibe: -1, aggro: 2 });
  });

  it('pours nothing either way', () => {
    for (const stage of STAGES) expect(judgeCall('cutoff', stage).intoxDelta).toBe(0);
  });
});

describe('BAR_ORDER_LINES', () => {
  it('every stage has lines — the order IS the sobriety test', () => {
    for (const stage of STAGES) expect(BAR_ORDER_LINES[stage].length).toBeGreaterThan(0);
  });
});
