<?php
require_once 'includes/auth.php';
require_role(['admin']); // bulk-insert into the catalog — admin only

require_once 'db.php';

// Run this script to populate default core items into tbl_master_items
$defaultItems = [
    ['item_name' => 'FLAT WOVEN WEBBING SLING', 'category' => 'LLG', 'period' => 6, 'standards' => 'BS EN 1492-1:2000+A1:2008', 'procedure' => 'DOC/TS/014; ISSUE 02 / REV02; 26.03.2016', 'desc' => "Brand:\nColor:\nPly:\nSize:\nEffective working Length:\nSafety Factor: 7:1"],
    ['item_name' => 'STEEL WIRE ROPE', 'category' => 'LLG', 'period' => 6, 'standards' => "BS EN 12385-1,4:2002+A1:2008\n& BS EN 13414-1:2003", 'procedure' => 'DOC/WR/006; REV02', 'desc' => "Description:\nMBL:\nLength:"],
    ['item_name' => 'POLYESTER ROUND SLING', 'category' => 'LLG', 'period' => 6, 'standards' => 'BS EN 1492-2:2000+A1:2008', 'procedure' => 'DOC/TS/014; ISSUE 02 / REV02; 26.03.2016', 'desc' => "Brand:\nColor:\nEfective working Length:\nSafety Factor: 7:1"],
    ['item_name' => 'SHACKLE', 'category' => 'LLG', 'period' => 6, 'standards' => "US FED SPEC.RR-C-271D / BS EN 13889", 'procedure' => 'DOC/SH/005, ISSUE : 02/REV01, 22.02.2017', 'desc' => "Type:\nGrade:\nPin Diameter:\nBow Diameter:"],
    ['item_name' => 'Banksman Slinger Training', 'category' => 'Training', 'period' => 24, 'standards' => 'LEEA / OSHA Standards', 'procedure' => 'TP/BS/2026', 'desc' => "UNDERSTANDING HAND SIGNALS\nLOAD CONTROL, LOAD STABILITY & PINCH POINTS\nIDENTIFICATION OF LOOSE LIFTING GEARS\nSAFE USE & INSPECTION OF VARIOUS LIFTING GEARS\nSLING ANGLES AND THEIR EFFECT ON RATED LOAD"]
];

$currentUserId = current_user()['id'] ?? null;

$stmt = $pdo->prepare("INSERT INTO tbl_master_items (item_name, category, certificate_period_months, reference_standards, inspection_procedure, description_template, created_by) VALUES (?, ?, ?, ?, ?, ?, ?)");

$count = 0;
foreach ($defaultItems as $i) {
    $stmt->execute([$i['item_name'], $i['category'], $i['period'], $i['standards'], $i['procedure'], $i['desc'], $currentUserId]);
    $count++;
}

echo "Successfully imported/initialized {$count} Master Bank Items!";
