-- =====================================================
-- SCRIPT 08: Create Machine Relationships Infrastructure
-- =====================================================
-- Date: 2025-11-06
-- Purpose: Create relationshiptypes and machinerelationships tables
-- Status: REVERSIBLE (see ROLLBACK_08)
-- Estimated Time: 2 minutes
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

-- =====================================================
-- STEP 1: Create relationshiptypes table
-- =====================================================

CREATE TABLE IF NOT EXISTS relationshiptypes (
    relationshiptypeid INT(11) PRIMARY KEY AUTO_INCREMENT,
    relationshiptype VARCHAR(50) NOT NULL UNIQUE,
    description VARCHAR(255),
    isactive TINYINT(1) DEFAULT 1,
    displayorder INT(11) DEFAULT 0,
    dateadded DATETIME DEFAULT CURRENT_TIMESTAMP,

    -- Indexes
    KEY idx_isactive (isactive),
    KEY idx_displayorder (displayorder)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
COMMENT='Types of relationships between machines';

-- Insert relationship types
INSERT INTO relationshiptypes (relationshiptype, description, displayorder) VALUES
('Dualpath', 'Machines sharing the same controller (dualpath configuration)', 1),
('Controlled By', 'PC controls this machine', 2),
('Controls', 'This PC controls another machine', 3),
('Cluster Member', 'Part of a machine cluster', 4),
('Backup For', 'Serves as backup for another machine', 5),
('Master-Slave', 'Master-slave relationship', 6);

-- =====================================================
-- STEP 2: Create machinerelationships table
-- =====================================================

CREATE TABLE IF NOT EXISTS machinerelationships (
    relationshipid INT(11) PRIMARY KEY AUTO_INCREMENT,
    machineid INT(11) NOT NULL,                     -- The machine
    related_machineid INT(11) NOT NULL,             -- Related machine
    relationshiptypeid INT(11) NOT NULL,            -- Relationship type
    relationship_notes TEXT,                        -- Additional context
    isactive TINYINT(1) DEFAULT 1,

    -- Audit fields
    dateadded DATETIME DEFAULT CURRENT_TIMESTAMP,
    lastupdated DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

    -- Indexes
    KEY idx_machineid (machineid),
    KEY idx_related_machineid (related_machineid),
    KEY idx_relationshiptypeid (relationshiptypeid),
    KEY idx_isactive (isactive),

    -- Composite index for common queries
    KEY idx_machine_relationship (machineid, relationshiptypeid),

    -- Foreign Keys
    CONSTRAINT fk_machinerel_machineid FOREIGN KEY (machineid) REFERENCES machines(machineid)
        ON DELETE CASCADE,
    CONSTRAINT fk_machinerel_related FOREIGN KEY (related_machineid) REFERENCES machines(machineid)
        ON DELETE CASCADE,
    CONSTRAINT fk_machinerel_type FOREIGN KEY (relationshiptypeid) REFERENCES relationshiptypes(relationshiptypeid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
COMMENT='Relationships between machines (dualpath, controller, cluster, etc.)';

-- =====================================================
-- STEP 3: Create machine relationships view
-- =====================================================

CREATE OR REPLACE VIEW vw_machine_relationships AS
SELECT
    mr.relationshipid,
    mr.machineid,
    m1.machinenumber AS machine_number,
    m1.hostname AS machine_hostname,
    mr.related_machineid,
    m2.machinenumber AS related_machine_number,
    m2.hostname AS related_machine_hostname,
    rt.relationshiptype,
    rt.description AS relationship_description,
    mr.relationship_notes,
    mr.isactive
FROM machinerelationships mr
JOIN machines m1 ON mr.machineid = m1.machineid
JOIN machines m2 ON mr.related_machineid = m2.machineid
JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid
WHERE mr.isactive = 1;

-- =====================================================
-- STEP 4: Create dualpath management view
-- =====================================================

CREATE OR REPLACE VIEW vw_dualpath_machines AS
SELECT
    mr.relationshipid,
    m1.machineid AS machine1_id,
    m1.machinenumber AS machine1_number,
    m1.hostname AS machine1_hostname,
    m2.machineid AS machine2_id,
    m2.machinenumber AS machine2_number,
    m2.hostname AS machine2_hostname,
    mr.relationship_notes
FROM machinerelationships mr
JOIN machines m1 ON mr.machineid = m1.machineid
JOIN machines m2 ON mr.related_machineid = m2.machineid
JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid
WHERE rt.relationshiptype = 'Dualpath'
  AND mr.isactive = 1
ORDER BY m1.machinenumber;

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

SELECT '✓ relationshiptypes table created' AS status;
DESCRIBE relationshiptypes;

SELECT '✓ Relationship types:' AS info;
SELECT * FROM relationshiptypes ORDER BY displayorder;

SELECT '✓ machinerelationships table created' AS status;
DESCRIBE machinerelationships;

SELECT '✓ Relationship views created' AS status;
SHOW TABLES LIKE 'vw_%relationship%';
SHOW TABLES LIKE 'vw_dualpath%';

SELECT '✓ Script 08 completed successfully' AS status;

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- Next: Review all 8 scripts, then run ROLLBACK scripts creation
-- Rollback: Run ROLLBACK_08_machine_relationships_infrastructure.sql
--
-- Migration Notes:
-- - pc_dualpath_assignments will migrate to machinerelationships
--   with relationshiptypeid for 'Dualpath'
-- - PC-to-machine control relationships can be added using
--   'Controlled By' relationship type
--
-- Usage Examples:
-- - Find all dualpath pairs: SELECT * FROM vw_dualpath_machines
-- - Find machines controlled by a PC:
--   SELECT * FROM vw_machine_relationships
--   WHERE relationshiptype = 'Controlled By'
-- =====================================================
