-- =====================================================
-- SCRIPT 07: Rename pcstatus to machinestatus
-- =====================================================
-- Date: 2025-11-06
-- Purpose: Rename pcstatus table and columns for generic use
-- Status: REVERSIBLE (see ROLLBACK_07)
-- Estimated Time: 1 minute
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

-- =====================================================
-- STEP 1: Check current pcstatus table
-- =====================================================

SELECT '✓ Current pcstatus table:' AS status;
DESCRIBE pcstatus;
SELECT * FROM pcstatus;

-- =====================================================
-- STEP 2: Rename table
-- =====================================================

RENAME TABLE pcstatus TO machinestatus;

-- =====================================================
-- STEP 3: Rename primary key column
-- =====================================================

ALTER TABLE machinestatus
CHANGE COLUMN pcstatusid machinestatusid INT(11) NOT NULL AUTO_INCREMENT;

-- =====================================================
-- STEP 4: Rename status column
-- =====================================================

ALTER TABLE machinestatus
CHANGE COLUMN pcstatus machinestatus VARCHAR(50);

-- =====================================================
-- STEP 5: Update indexes (if needed)
-- =====================================================

-- Check existing indexes
SHOW INDEXES FROM machinestatus;

-- =====================================================
-- STEP 6: Add FK constraint to machines table
-- =====================================================

-- Now that machinestatus exists, add the foreign key
ALTER TABLE machines
ADD CONSTRAINT fk_machines_machinestatusid
FOREIGN KEY (machinestatusid) REFERENCES machinestatus(machinestatusid);

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

SELECT '✓ Table renamed to machinestatus' AS status;
DESCRIBE machinestatus;

SELECT '✓ Machine status values:' AS info;
SELECT * FROM machinestatus ORDER BY machinestatusid;

SELECT '✓ Foreign key added to machines table' AS status;

SELECT '✓ Script 07 completed successfully' AS status;

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- Next: Run script 08_create_machine_relationships_infrastructure.sql
-- Rollback: Run ROLLBACK_07_machinestatus_rename.sql
--
-- Table renamed: pcstatus → machinestatus
-- Column renamed: pcstatusid → machinestatusid
-- Column renamed: pcstatus → machinestatus
--
-- Existing status values (typically):
-- - In Use
-- - Spare
-- - Retired
-- - Broken
-- - Unknown
-- =====================================================
