-- USB Device Checkout System Schema
-- Created: 2025-12-07
--
-- This script adds:
-- 1. New machine type for USB devices (machinetypeid = 44)
-- 2. New usb_checkouts table for tracking checkout/check-in records

-- ============================================
-- 1. Add USB Device machine type
-- ============================================
INSERT INTO machinetypes (machinetypeid, machinetype, isactive)
VALUES (44, 'USB Device', 1)
ON DUPLICATE KEY UPDATE machinetype = 'USB Device', isactive = 1;

-- ============================================
-- 2. Create usb_checkouts table
-- ============================================
CREATE TABLE IF NOT EXISTS usb_checkouts (
    checkoutid INT(11) NOT NULL AUTO_INCREMENT,
    machineid INT(11) NOT NULL,                     -- FK to machines (USB device)
    sso VARCHAR(20) NOT NULL,                       -- Who checked it out (9-digit SSO)
    checkout_reason TEXT,                           -- Why they need it
    checkout_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    checkin_time DATETIME DEFAULT NULL,             -- NULL = still checked out
    was_wiped TINYINT(1) DEFAULT NULL,              -- 1=yes, 0=no, NULL=not checked in yet
    checkin_notes TEXT,                             -- Notes on check-in
    PRIMARY KEY (checkoutid),
    KEY idx_usb_machineid (machineid),
    KEY idx_usb_sso (sso),
    KEY idx_usb_checkout_time (checkout_time),
    KEY idx_usb_checkin_time (checkin_time),
    CONSTRAINT fk_usb_checkouts_machine FOREIGN KEY (machineid)
        REFERENCES machines(machineid) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ============================================
-- 3. Sample USB devices for testing (optional)
-- ============================================
-- Uncomment to add test data:
-- INSERT INTO machines (machinenumber, serialnumber, alias, machinetypeid, businessunitid, isactive)
-- VALUES
--     ('USB-001', 'SN-USB-001', 'Blue 32GB', 44, 1, 1),
--     ('USB-002', 'SN-USB-002', 'Red 64GB', 44, 1, 1),
--     ('USB-003', 'SN-USB-003', 'White 16GB', 44, 1, 1);

-- ============================================
-- Verification queries
-- ============================================
-- Check machine type was added:
-- SELECT * FROM machinetypes WHERE machinetypeid = 44;

-- Check table was created:
-- DESCRIBE usb_checkouts;

-- List USB devices:
-- SELECT * FROM machines WHERE machinetypeid = 44;
