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

const name = 'chris';

const characteristics = {
  WS: 43,
  BS: 41,
  S: 40,
  T: 42,
  Ag: 39,
  Int: 48,
  Per: 45,
  Wp: 51,
  Fel: 37
};

function skill(trained = false) {
  return { trained, plus10: false, plus20: false };
}

const skills = {
  'Awareness (Per)': skill(true),
  'Common Lore (Int)': skill(true),
  'Dodge (Ag)': skill(true),
  'Forbidden Lore (Int) - Xenos': skill(true),
  'Intimidate (S)': skill(true),
  'Literacy (Int)': skill(true),
  'Psyniscience (Per)': skill(true),
  'Scholastic Lore (Int) - Codex Astartes': skill(true),
  'Scholastic Lore (Int) - Occult': skill(true),
  'Search (Per)': skill(true),
  'Speak Language (Int) - High Gothic': skill(true),
  'Speak Language (Int) - Low Gothic': skill(true),
  'Silent Move (Ag)': skill(false),
  'Tech-Use (Int)': skill(false),
  'Navigation (Int)': skill(false),
  'Command (Fel)': skill(false)
};

const tabInfo = {
  charName: 'Brother Malachiel',
  playerName: name,
  chapter: 'dark angels',
  speciality: 'librarian',
  rank: '1',
  characteristics,
  skills,
  weapons: [
    {
      name: 'Astartes Bolter',
      class: 'Basic',
      damage: '2d10+5',
      type: 'Explosive',
      pen: '5',
      range: '100',
      rof: 'S/2/4',
      clip: '28',
      rld: 'Full',
      special: 'Tearing, Fire selector'
    },
    {
      name: 'Astartes Bolt Pistol',
      class: 'Pistol',
      damage: '2d10+5',
      type: 'Explosive',
      pen: '5',
      range: '30',
      rof: 'S/2/-',
      clip: '14',
      rld: 'Full',
      special: 'Tearing'
    },
    {
      name: 'Astartes Force Sword',
      class: 'Melee',
      damage: '1d10+5 + SB',
      type: 'Rending',
      pen: '5',
      range: '',
      rof: '',
      clip: '',
      rld: '',
      special: 'Balanced, Special, Psy Rating 3 included'
    }
  ],
  gear: [
    { name: 'Astartes Power Armour', qty: 1 },
    { name: 'Astartes Bolter with Fire Selector', qty: 1 },
    { name: 'Astartes Bolt Pistol', qty: 1 },
    { name: 'Astartes Force Sword', qty: 1 },
    { name: 'Astartes Frag Grenade', qty: 3 },
    { name: 'Astartes Krak Grenade', qty: 3 },
    { name: 'Astartes Combat Knife', qty: 1 },
    { name: 'Emperor Tarot Deck', qty: 1 }
  ],
  armour: { body: 'Mark VII Power Armour', ap: 8, head: 8, ra: 8, la: 8, rl: 8, ll: 8 },
  talents: [
    'Astartes Weapon Training',
    'Deathwatch Training',
    'Psy Rating 3',
    'Resistance (Psychic Powers)',
    'Nerves of Steel',
    'True Grit',
    'Unnatural Strength (x2)',
    'Unnatural Toughness (x2)'
  ],
  psychicPowers: ['Smite', 'Inspire', 'Reading'],
  fate: 3,
  wounds: 20,
  movement: { half: 4, full: 8, charge: 12, halfAction: 4, fullAction: 8, run: 24 },
  renown: 'Initiated',
  rp: 0,
  xp: 0,
  xpSpent: 0,
  notes: 'Dark Angels Deathwatch Librarian introduced during Operation Vesalius Scene 4.'
};

(async function main() {
  const existing = await playerHelpers.getByName(name);
  if (existing) {
    await playerHelpers.update(name, {
      rollerInfo: existing.rollerInfo || {},
      shopInfo: existing.shopInfo || {},
      tabInfo,
      pw: existing.pw || '',
      pwHash: existing.pwHash || ''
    });
    console.log(JSON.stringify({ status: 'updated', name }, null, 2));
  } else {
    const id = await playerHelpers.create({
      name,
      rollerInfo: {},
      shopInfo: {},
      tabInfo,
      pw: '1234',
      pwHash: ''
    });
    console.log(JSON.stringify({ status: 'created', id, name }, null, 2));
  }
  await pool.end();
})();
