-- =====================================================
-- SCRIPT 03: Create Machine Types for PCs
-- =====================================================
-- Date: 2025-11-06
-- Purpose: Add PC-related machine types to support PC migration
-- Status: REVERSIBLE (see ROLLBACK_03)
-- Estimated Time: 1 minute
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

-- =====================================================
-- STEP 1: Ensure functional accounts exist
-- =====================================================

INSERT IGNORE INTO functionalaccounts (functionalaccount, description, isactive)
VALUES
('IT', 'Information Technology', 1),
('MFG', 'Manufacturing', 1),
('ENG', 'Engineering', 1);

-- =====================================================
-- STEP 2: Create PC machine types
-- =====================================================

-- Get functional account IDs
SET @it_id = (SELECT functionalaccountid FROM functionalaccounts WHERE functionalaccount = 'IT' LIMIT 1);
SET @mfg_id = (SELECT functionalaccountid FROM functionalaccounts WHERE functionalaccount = 'MFG' LIMIT 1);
SET @eng_id = (SELECT functionalaccountid FROM functionalaccounts WHERE functionalaccount = 'ENG' LIMIT 1);

-- Insert PC machine types if they don't exist
INSERT INTO machinetypes (machinetype, machinedescription, functionalaccountid, isactive)
SELECT 'PC - Standard', 'Standard office/engineering workstation', @it_id, 1 FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetype = 'PC - Standard');

INSERT INTO machinetypes (machinetype, machinedescription, functionalaccountid, isactive)
SELECT 'PC - Shopfloor', 'Shopfloor machine control PC', @mfg_id, 1 FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetype = 'PC - Shopfloor');

INSERT INTO machinetypes (machinetype, machinedescription, functionalaccountid, isactive)
SELECT 'PC - Engineer', 'Engineering workstation', @eng_id, 1 FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetype = 'PC - Engineer');

INSERT INTO machinetypes (machinetype, machinedescription, functionalaccountid, isactive)
SELECT 'PC - Server', 'Server or VM', @it_id, 1 FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetype = 'PC - Server');

INSERT INTO machinetypes (machinetype, machinedescription, functionalaccountid, isactive)
SELECT 'PC - Laptop', 'Laptop computer', @it_id, 1 FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM machinetypes WHERE machinetype = 'PC - Laptop');

-- =====================================================
-- VERIFICATION
-- =====================================================

SELECT '✓ Functional accounts verified' AS status;
SELECT * FROM functionalaccounts WHERE functionalaccount IN ('IT', 'MFG', 'ENG');

SELECT '✓ PC machine types created' AS status;
SELECT machinetypeid, machinetype, machinedescription, functionalaccountid, isactive
FROM machinetypes
WHERE machinetype LIKE 'PC -%'
ORDER BY machinetype;

SELECT '✓ Script 03 completed successfully' AS status;

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- Next: Run script 04_create_warranty_infrastructure.sql
-- Rollback: Run ROLLBACK_03_pc_machine_types.sql
--
-- Machine Type Mapping for PC Migration:
-- - pctype 'Standard' → 'PC - Standard'
-- - pctype 'Shopfloor' → 'PC - Shopfloor'
-- - pctype 'Engineer' → 'PC - Engineer'
-- - pctype 'Server' or 'VM' → 'PC - Server'
-- - pctype 'Laptop' → 'PC - Laptop'
-- =====================================================
