const { missionHelpers, pool } = require('../mariadb');

const scenes = [
  {
    title: 'The Living Vault',
    type: 'investigation',
    mood: 'The Sample vault collapses, but its last heartbeat becomes a transmission.',
    description: 'Da energifeltet kollapser, falder stilheden ikke. Den bliver dybere. Væggene omkring vaulten trækker sig sammen som muskler, og panelerne begynder at bløde sort væske ned over runer og datakabler. Auspexen skriger kort. Ikke en alarm. Et hjerteslag. Brother Malachiel drejer langsomt hovedet mod prøvens kammer. "Det her var ikke kun et laboratorium. Det var en sender."',
    objectives: [
      'Stop or copy the vault datapulse.',
      'Identify the destination marker: THETA-92.',
      'Keep Malachiel clear of the psychic feedback.'
    ],
    checks: [
      { name: 'Stop/copy datapulse', skill: 'Tech-Use', characteristic: 'Int', target: 45, modifier: -10, reward: 'Recover full THETA-92 coordinates.' },
      { name: 'Read psychic carrier wave', skill: 'Psyniscience', characteristic: 'Per', target: 45, modifier: 0, reward: 'Confirm the signal is warp-amplified.' },
      { name: 'Interpret forbidden pattern', skill: 'Forbidden Lore', characteristic: 'Int', target: 45, modifier: -10, reward: 'Learn the signal points to a second Bile site.' }
    ],
    complications: [
      '3 successes before 2 failures gives full coordinates.',
      'On failure, the team only gets partial coordinates and starts the next mission with weaker intel.',
      'Critical failure gives one marine a vision of Bile smiling in a white laboratory.'
    ],
    rewards: { xp: 75, rp: 1 },
    extraChallenges: [
      { id: 'full-coordinates', text: 'Recover full THETA-92 coordinates before the vault dies.', rewardType: 'xp', rewardValue: 75, completed: false },
      { id: 'clean-copy', text: 'Copy the signal without letting it ping the Kill-team biometrics.', rewardType: 'renown', rewardValue: 1, completed: false }
    ],
    outro: 'På skærmen står kun én markør tilbage: THETA-92 // ACTIVE // RECEIVING'
  },
  {
    title: 'Prisoner Signal',
    type: 'social',
    mood: 'A weak vox-call cuts through the alarms. Someone is still alive.',
    description: 'Mens skibet ryster, bryder en svag vox-kanal gennem alarmstøjen. Først kun statisk. Så hosten. Så en stemme. "Imperial signal... hvis nogen hører dette... vi er stadig i live. Ikke mange. Ikke længe." Kortet opdaterer med en sektion tæt på jeres flugtrute: SPECIMEN HOLDING.',
    objectives: [
      'Decide whether to spend escape time on the prisoners.',
      'Rescue Magos-Biologis Ceryx Vorn if possible.',
      'Identify any biomorphic trap among the prisoners.'
    ],
    checks: [
      { name: 'Open stasis cages safely', skill: 'Tech-Use', characteristic: 'Int', target: 45, modifier: 0, reward: 'Release prisoners without killing support systems.' },
      { name: 'Triage movable prisoners', skill: 'Medicae', characteristic: 'Int', target: 45, modifier: 0, reward: 'Save the prisoners who can survive extraction.' },
      { name: 'Spot biomorphic trap', skill: 'Awareness', characteristic: 'Per', target: 45, modifier: 10, reward: 'Detect the altered prisoner before it wakes.' }
    ],
    enemies: [
      { name: 'Gene-Warped Prisoner', type: 'mutant trap', count: 1 }
    ],
    combat: {
      enemy: 'Gene-Warped Prisoner',
      weapon: 'Bone talons',
      ws: 35,
      bs: 0,
      modifier: 0,
      damage: '1d10+5',
      targetTB: 4,
      targetArmour: 2,
      mode: 'single',
      rof: 1
    },
    complications: [
      'If the trap is missed, one prisoner wakes as a Fear 1 biomorphic attacker.',
      'Each extra round spent here tightens the escape timer.',
      'Magos Vorn knows Vesalius was transport, not source.'
    ],
    rewards: { xp: 100, rp: 2 },
    extraChallenges: [
      { id: 'save-vorn', text: 'Extract Magos-Biologis Ceryx Vorn alive.', rewardType: 'xp', rewardValue: 100, completed: false },
      { id: 'save-four', text: 'Rescue four or more prisoners.', rewardType: 'renown', rewardValue: 1, completed: false }
    ],
    outro: 'Magos Vorn griber jeres arm med en rystende metalhånd. "Vesalius var ikke kilden. Det var kun transporten."'
  },
  {
    title: 'Engine Shrine Sabotage',
    type: 'puzzle_combat',
    mood: 'The engine room is a chapel of wet steel, plasma heat, and living machine-hate.',
    description: 'Motorsektionen føles ikke som et maskinrum. Den føles som et kapel bygget af kød og stål. Plasmaledninger hænger som tarme fra loftet. Tandhjul drejer i våde rytmer. Midt i det hele står tre reaktorkerner, hver omviklet af sort organisk væv. Hvis Vesalius skal dø, skal det ske her.',
    objectives: [
      'Plant charges on all three engine cores.',
      'Overload reactor couplings so Vesalius cannot be repaired.',
      'Survive plasma venting and released Gland Hounds.'
    ],
    checks: [
      { name: 'Place demolition charges', skill: 'Demolitions', characteristic: 'Int', target: 45, modifier: 0, reward: 'Arm the charges in the right sequence.' },
      { name: 'Overload reactor couplings', skill: 'Tech-Use', characteristic: 'Int', target: 45, modifier: -10, reward: 'Make the destruction permanent.' },
      { name: 'Rip open armour panels', skill: 'Strength', characteristic: 'S', target: 45, modifier: 10, reward: 'Expose the core without losing time.' },
      { name: 'Avoid plasma venting', skill: 'Dodge', characteristic: 'Ag', target: 45, modifier: 0, reward: 'Avoid 1d10+4 Energy Pen 3.' }
    ],
    enemies: [
      { name: 'Gland Hound', type: 'bio-hound', count: 2 }
    ],
    combat: {
      enemy: 'Gland Hound',
      weapon: 'Bite',
      ws: 40,
      bs: 0,
      modifier: 0,
      damage: '1d10+5',
      targetTB: 4,
      targetArmour: 2,
      mode: 'single',
      rof: 1
    },
    complications: [
      'Skill challenge: 5 successes before 3 failures.',
      'Failure causes 1d10+4 Energy Pen 3 or lost time.',
      'At 2 failures, two Gland Hounds enter the shrine.'
    ],
    rewards: { xp: 100, rp: 2 },
    extraChallenges: [
      { id: 'clean-sabotage', text: 'Complete sabotage before three failures.', rewardType: 'xp', rewardValue: 100, completed: false },
      { id: 'no-delay', text: 'Leave the engine shrine without reducing the escape timer.', rewardType: 'renown', rewardValue: 1, completed: false }
    ],
    outro: 'Den første ladning armeres. Så den næste. Så den tredje. Hele skibet skriger.'
  },
  {
    title: 'The Theta Transmission',
    type: 'investigation',
    mood: 'Bile leaves them coordinates not as a gift, but as contempt.',
    description: 'På vej mod flugtfartøjet tænder alle skærme omkring jer samtidigt. Biles ansigt vises ikke klart. Det er forvrænget, kopieret, skåret sammen af flere stemmer. "I ødelagde et bur. Ikke dyret." Bag stemmen ser I et glimt: sne, sort klippe, et underjordisk laboratorium, og rækker af stasis-kapsler mærket med imperiale genetiske segl. Så kommer koordinaterne. Ikke som gave. Som hån.',
    objectives: [
      'Choose one intel priority for the next mission.',
      'Confirm Facility THETA-92 as the next target.',
      'Record whether the Sample was extracted or destroyed.'
    ],
    checks: [
      { name: 'Lock coordinates', skill: 'Navigation', characteristic: 'Int', target: 45, modifier: 0, reward: '+10 to first infiltration/navigation test next mission.' },
      { name: 'Scrub vox corruption', skill: 'Tech-Use', characteristic: 'Int', target: 45, modifier: -10, reward: 'Preserve enemy/facility intel cleanly.' },
      { name: 'Frame strategic report', skill: 'Command', characteristic: 'Fel', target: 45, modifier: 10, reward: 'Turn the discovery into actionable Deathwatch orders.' }
    ],
    complications: [
      'Intel priority choices: enemy intel, facility map, sample data, or prisoner testimony.',
      'If Sample extracted, quarantine becomes a next-session pressure.',
      'If Sample destroyed, Mechanicus demands data restitution.'
    ],
    rewards: { xp: 50, rp: 1 },
    extraChallenges: [
      { id: 'intel-priority', text: 'Choose and record a clear intel priority for THETA-92.', rewardType: 'xp', rewardValue: 50, completed: false },
      { id: 'strategic-edge', text: 'Frame the report so Deathwatch command gains a strategic edge.', rewardType: 'renown', rewardValue: 1, completed: false }
    ],
    outro: 'THETA-92 blinker på hololithen. Vesalius brænder bag jer. Men krigen er allerede flyttet videre.'
  }
];

(async function main() {
  const name = 'Vesalius Echo: Theta-92';
  const existing = (await missionHelpers.getAll()).find(m => m.name === name);
  if (existing) {
    console.log(JSON.stringify({ status: 'exists', id: existing.id, name: existing.name }, null, 2));
    await pool.end();
    return;
  }

  const id = await missionHelpers.create({
    name,
    theme: 'chaos_biotech',
    sceneCount: scenes.length,
    enemyCount: 4,
    threatLevel: 'High',
    playerCount: 4,
    gmPlayer: 'gm',
    scenes
  });

  console.log(JSON.stringify({ status: 'created', id, name }, null, 2));
  await pool.end();
})();
