-- =====================================================
-- SCRIPT 06: Update Views for PC→Machines Migration
-- =====================================================
-- Date: 2025-11-07
-- Purpose: Update 19 views to query machines table instead of pc table
-- Status: REQUIRES TESTING - Depends on Scripts 01-05
-- Estimated Time: 2-3 minutes
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

-- =====================================================
-- STEP 1: Pre-update information
-- =====================================================

SELECT '========================================' AS '';
SELECT 'VIEW UPDATES FOR MACHINES TABLE' AS '';
SELECT '========================================' AS '';
SELECT '' AS '';

SELECT 'Views to update:' AS '';
SELECT COUNT(*) as view_count
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'shopdb'
  AND VIEW_DEFINITION LIKE '%`pc`%';

SELECT '' AS '';

-- =====================================================
-- STEP 2: Drop and recreate views
-- =====================================================

-- View 1: vw_active_pcs
-- Purpose: Show active PCs with basic info and warranty status
SELECT 'Updating vw_active_pcs...' AS '';

DROP VIEW IF EXISTS vw_active_pcs;

CREATE VIEW vw_active_pcs AS
SELECT
    pcmap.pcid AS pcid,
    m.hostname AS hostname,
    m.serialnumber AS serialnumber,
    COALESCE(v.vendor, 'Unknown') AS manufacturer,
    md.modelnumber AS model,
    m.loggedinuser AS loggedinuser,
    m.machinenumber AS machinenumber,
    COALESCE(os.operatingsystem, 'Unknown') AS operatingsystem,
    COALESCE(pt.typename, 'Unknown') AS pctype,
    COALESCE(pt.description, 'Unknown') AS typedescription,
    CASE
        WHEN w.enddate IS NULL THEN 'Unknown'
        WHEN w.enddate < CURDATE() THEN 'Expired'
        WHEN w.enddate < DATE_ADD(CURDATE(), INTERVAL 90 DAY) THEN 'Expiring Soon'
        ELSE 'Active'
    END AS warrantystatus,
    w.enddate AS warrantyenddate,
    CASE
        WHEN w.enddate IS NULL THEN NULL
        ELSE (TO_DAYS(w.enddate) - TO_DAYS(CURDATE()))
    END AS warrantydaysremaining,
    m.lastupdated AS lastupdated,
    (TO_DAYS(NOW()) - TO_DAYS(m.lastupdated)) AS daysold
FROM machines m
JOIN pc_to_machine_id_mapping pcmap ON m.machineid = pcmap.new_machineid
LEFT JOIN models md ON m.modelnumberid = md.modelnumberid
LEFT JOIN vendors v ON md.vendorid = v.vendorid
LEFT JOIN pctype pt ON m.pctypeid = pt.pctypeid
LEFT JOIN operatingsystems os ON m.osid = os.osid
LEFT JOIN warranties w ON m.machineid = w.machineid
WHERE m.lastupdated > (NOW() - INTERVAL 30 DAY)
  AND m.pctypeid IS NOT NULL;

SELECT '  ✓ vw_active_pcs updated' AS status;

-- View 2: vw_pc_summary
-- Purpose: Summary statistics of PCs by type
SELECT 'Updating vw_pc_summary...' AS '';

DROP VIEW IF EXISTS vw_pc_summary;

CREATE VIEW vw_pc_summary AS
SELECT
    pt.typename AS PCType,
    pt.description AS Description,
    COUNT(m.machineid) AS Count,
    ROUND((COUNT(m.machineid) * 100.0) / NULLIF(
        (SELECT COUNT(*) FROM machines
         WHERE pctypeid IS NOT NULL
         AND lastupdated > (NOW() - INTERVAL 30 DAY)), 0
    ), 2) AS Percentage
FROM pctype pt
LEFT JOIN machines m ON pt.pctypeid = m.pctypeid
    AND m.lastupdated > (NOW() - INTERVAL 30 DAY)
    AND m.pctypeid IS NOT NULL
WHERE pt.isactive = '1'
GROUP BY pt.pctypeid, pt.typename, pt.description, pt.displayorder
ORDER BY pt.displayorder;

SELECT '  ✓ vw_pc_summary updated' AS status;

-- View 3: vw_shopfloor_pcs
-- Purpose: Show shopfloor PCs with machine associations
SELECT 'Updating vw_shopfloor_pcs...' AS '';

DROP VIEW IF EXISTS vw_shopfloor_pcs;

CREATE VIEW vw_shopfloor_pcs AS
SELECT
    pcmap.pcid AS pcid,
    m.hostname AS hostname,
    m.serialnumber AS serialnumber,
    v.vendor AS manufacturer,
    md.modelnumber AS model,
    m.loggedinuser AS loggedinuser,
    COALESCE(CONVERT(mo.machinenumber USING utf8mb4), CONVERT(m.machinenumber USING utf8mb4)) AS machinenumber,
    COALESCE(os.operatingsystem, 'Unknown') AS operatingsystem,
    m.lastupdated AS lastupdated
FROM machines m
JOIN pc_to_machine_id_mapping pcmap ON m.machineid = pcmap.new_machineid
JOIN pctype pt ON m.pctypeid = pt.pctypeid
LEFT JOIN machine_overrides mo ON pcmap.pcid = mo.pcid
LEFT JOIN models md ON m.modelnumberid = md.modelnumberid
LEFT JOIN vendors v ON md.vendorid = v.vendorid
LEFT JOIN operatingsystems os ON m.osid = os.osid
WHERE pt.typename = 'Shopfloor'
  AND m.lastupdated > (NOW() - INTERVAL 30 DAY)
  AND m.pctypeid IS NOT NULL
ORDER BY COALESCE(CONVERT(mo.machinenumber USING utf8mb4), CONVERT(m.machinenumber USING utf8mb4)), m.hostname;

SELECT '  ✓ vw_shopfloor_pcs updated' AS status;

-- View 4: vw_engineer_pcs
-- Purpose: Show engineer PCs
SELECT 'Updating vw_engineer_pcs...' AS '';

DROP VIEW IF EXISTS vw_engineer_pcs;

CREATE VIEW vw_engineer_pcs AS
SELECT
    pcmap.pcid AS pcid,
    m.hostname AS hostname,
    m.serialnumber AS serialnumber,
    v.vendor AS manufacturer,
    md.modelnumber AS model,
    m.loggedinuser AS loggedinuser,
    m.machinenumber AS machinenumber,
    COALESCE(os.operatingsystem, 'Unknown') AS operatingsystem,
    m.lastupdated AS lastupdated
FROM machines m
JOIN pc_to_machine_id_mapping pcmap ON m.machineid = pcmap.new_machineid
JOIN pctype pt ON m.pctypeid = pt.pctypeid
LEFT JOIN models md ON m.modelnumberid = md.modelnumberid
LEFT JOIN vendors v ON md.vendorid = v.vendorid
LEFT JOIN operatingsystems os ON m.osid = os.osid
WHERE pt.typename = 'Engineer'
  AND m.lastupdated > (NOW() - INTERVAL 30 DAY)
  AND m.pctypeid IS NOT NULL
ORDER BY m.hostname;

SELECT '  ✓ vw_engineer_pcs updated' AS status;

-- View 5: vw_standard_pcs
-- Purpose: Show standard PCs
SELECT 'Updating vw_standard_pcs...' AS '';

DROP VIEW IF EXISTS vw_standard_pcs;

CREATE VIEW vw_standard_pcs AS
SELECT
    pcmap.pcid AS pcid,
    m.hostname AS hostname,
    m.serialnumber AS serialnumber,
    v.vendor AS manufacturer,
    md.modelnumber AS model,
    m.loggedinuser AS loggedinuser,
    m.machinenumber AS machinenumber,
    COALESCE(os.operatingsystem, 'Unknown') AS operatingsystem,
    m.lastupdated AS lastupdated
FROM machines m
JOIN pc_to_machine_id_mapping pcmap ON m.machineid = pcmap.new_machineid
JOIN pctype pt ON m.pctypeid = pt.pctypeid
LEFT JOIN models md ON m.modelnumberid = md.modelnumberid
LEFT JOIN vendors v ON md.vendorid = v.vendorid
LEFT JOIN operatingsystems os ON m.osid = os.osid
WHERE pt.typename = 'Standard'
  AND m.lastupdated > (NOW() - INTERVAL 30 DAY)
  AND m.pctypeid IS NOT NULL
ORDER BY m.hostname;

SELECT '  ✓ vw_standard_pcs updated' AS status;

-- View 6: vw_pcs_by_hardware
-- Purpose: Group PCs by hardware (vendor/model)
SELECT 'Updating vw_pcs_by_hardware...' AS '';

DROP VIEW IF EXISTS vw_pcs_by_hardware;

CREATE VIEW vw_pcs_by_hardware AS
SELECT
    COALESCE(v.vendor, 'Unknown') AS manufacturer,
    COALESCE(md.modelnumber, 'Unknown') AS model,
    COUNT(m.machineid) AS count,
    GROUP_CONCAT(DISTINCT pt.typename ORDER BY pt.typename SEPARATOR ', ') AS pc_types
FROM machines m
JOIN pc_to_machine_id_mapping pcmap ON m.machineid = pcmap.new_machineid
LEFT JOIN models md ON m.modelnumberid = md.modelnumberid
LEFT JOIN vendors v ON md.vendorid = v.vendorid
LEFT JOIN pctype pt ON m.pctypeid = pt.pctypeid
WHERE m.lastupdated > (NOW() - INTERVAL 30 DAY)
  AND m.pctypeid IS NOT NULL
GROUP BY v.vendor, md.modelnumber
ORDER BY COUNT(m.machineid) DESC, v.vendor, md.modelnumber;

SELECT '  ✓ vw_pcs_by_hardware updated' AS status;

-- View 7: vw_pc_network_summary
-- Purpose: Show PC network interface summary
SELECT 'Updating vw_pc_network_summary...' AS '';

DROP VIEW IF EXISTS vw_pc_network_summary;

CREATE VIEW vw_pc_network_summary AS
SELECT
    pcmap.pcid AS pcid,
    m.hostname AS hostname,
    m.machinenumber AS machinenumber,
    COUNT(c.comid) AS interface_count,
    GROUP_CONCAT(c.address ORDER BY c.comid SEPARATOR ', ') AS ip_addresses,
    GROUP_CONCAT(c.macaddress ORDER BY c.comid SEPARATOR ', ') AS mac_addresses
FROM machines m
JOIN pc_to_machine_id_mapping pcmap ON m.machineid = pcmap.new_machineid
LEFT JOIN communications c ON m.machineid = c.machineid
    AND c.comstypeid = (SELECT comstypeid FROM comstypes WHERE typename = 'Network_Interface' LIMIT 1)
    AND c.isactive = 1
WHERE m.pctypeid IS NOT NULL
GROUP BY pcmap.pcid, m.hostname, m.machinenumber
ORDER BY m.hostname;

SELECT '  ✓ vw_pc_network_summary updated' AS status;

-- View 8: vw_dnc_config
-- Purpose: Show DNC configuration for PCs
SELECT 'Updating vw_dnc_config...' AS '';

DROP VIEW IF EXISTS vw_dnc_config;

CREATE VIEW vw_dnc_config AS
SELECT
    pcmap.pcid AS pcid,
    m.hostname AS hostname,
    m.machinenumber AS machinenumber,
    c.address AS ip_address,
    c.port AS socket,
    c.settings AS config_settings,
    ct.typename AS comm_type
FROM machines m
JOIN pc_to_machine_id_mapping pcmap ON m.machineid = pcmap.new_machineid
LEFT JOIN communications c ON m.machineid = c.machineid AND c.isactive = 1
LEFT JOIN comstypes ct ON c.comstypeid = ct.comstypeid
WHERE m.pctypeid IS NOT NULL
  AND ct.typename IN ('IP', 'Serial')
ORDER BY m.hostname, ct.typename;

SELECT '  ✓ vw_dnc_config updated' AS status;

-- View 9: vw_shopfloor_comm_config
-- Purpose: Communication config for shopfloor PCs
SELECT 'Updating vw_shopfloor_comm_config...' AS '';

DROP VIEW IF EXISTS vw_shopfloor_comm_config;

CREATE VIEW vw_shopfloor_comm_config AS
SELECT
    pcmap.pcid AS pcid,
    m.hostname AS hostname,
    m.machinenumber AS machinenumber,
    pt.typename AS pctype,
    c.address AS ip_address,
    c.port AS port_or_socket,
    c.baud AS baud,
    c.databits AS databits,
    c.stopbits AS stopbits,
    c.parity AS parity,
    ct.typename AS comm_type
FROM machines m
JOIN pc_to_machine_id_mapping pcmap ON m.machineid = pcmap.new_machineid
JOIN pctype pt ON m.pctypeid = pt.pctypeid
LEFT JOIN communications c ON m.machineid = c.machineid AND c.isactive = 1
LEFT JOIN comstypes ct ON c.comstypeid = ct.comstypeid
WHERE pt.typename = 'Shopfloor'
  AND m.pctypeid IS NOT NULL
  AND ct.typename IN ('IP', 'Serial')
ORDER BY m.machinenumber, m.hostname;

SELECT '  ✓ vw_shopfloor_comm_config updated' AS status;

-- View 10: vw_machine_type_stats
-- Purpose: Statistics by machine type (includes PCs)
SELECT 'Updating vw_machine_type_stats...' AS '';

DROP VIEW IF EXISTS vw_machine_type_stats;

CREATE VIEW vw_machine_type_stats AS
SELECT
    mt.machinetypeid,
    mt.machinetype AS machine_type,
    COUNT(m.machineid) AS total_count,
    SUM(CASE WHEN m.pctypeid IS NOT NULL THEN 1 ELSE 0 END) AS pc_count,
    SUM(CASE WHEN m.pctypeid IS NULL THEN 1 ELSE 0 END) AS equipment_count,
    SUM(CASE WHEN m.lastupdated > (NOW() - INTERVAL 30 DAY) THEN 1 ELSE 0 END) AS active_count
FROM machinetypes mt
LEFT JOIN machines m ON mt.machinetypeid = m.machinetypeid AND m.isactive = 1
WHERE mt.isactive = 1
GROUP BY mt.machinetypeid, mt.machinetype
ORDER BY COUNT(m.machineid) DESC;

SELECT '  ✓ vw_machine_type_stats updated' AS status;

-- View 11: vw_vendor_summary
-- Purpose: Vendor summary including PCs
SELECT 'Updating vw_vendor_summary...' AS '';

DROP VIEW IF EXISTS vw_vendor_summary;

CREATE VIEW vw_vendor_summary AS
SELECT
    v.vendorid,
    v.vendor AS vendor_name,
    COUNT(DISTINCT md.modelnumberid) AS model_count,
    COUNT(m.machineid) AS machine_count,
    SUM(CASE WHEN m.pctypeid IS NOT NULL THEN 1 ELSE 0 END) AS pc_count,
    SUM(CASE WHEN m.pctypeid IS NULL THEN 1 ELSE 0 END) AS equipment_count,
    v.ismachine,
    v.isactive
FROM vendors v
LEFT JOIN models md ON v.vendorid = md.vendorid AND md.isactive = 1
LEFT JOIN machines m ON md.modelnumberid = m.modelnumberid AND m.isactive = 1
WHERE v.isactive = 1
GROUP BY v.vendorid, v.vendor, v.ismachine, v.isactive
ORDER BY COUNT(m.machineid) DESC, v.vendor;

SELECT '  ✓ vw_vendor_summary updated' AS status;

-- View 12: vw_dualpath_management
-- Purpose: Dualpath machine management view
SELECT 'Updating vw_dualpath_management...' AS '';

DROP VIEW IF EXISTS vw_dualpath_management;

CREATE VIEW vw_dualpath_management AS
SELECT
    m1.machineid AS machine1_id,
    m1.machinenumber AS machine1_number,
    m1.hostname AS machine1_hostname,
    mr.relationshipid,
    rt.relationshiptype,
    m2.machineid AS machine2_id,
    m2.machinenumber AS machine2_number,
    m2.hostname AS machine2_hostname,
    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 rt.relationshiptype = 'Dualpath'
  AND mr.isactive = 1
ORDER BY m1.machinenumber, m2.machinenumber;

SELECT '  ✓ vw_dualpath_management updated' AS status;

-- View 13: vw_machine_assignments
-- Purpose: Show machine-to-PC assignments (controls/controlled by)
SELECT 'Updating vw_machine_assignments...' AS '';

DROP VIEW IF EXISTS vw_machine_assignments;

CREATE VIEW vw_machine_assignments AS
SELECT
    m1.machineid AS machine_id,
    m1.machinenumber AS machine_number,
    m1.hostname AS machine_hostname,
    rt.relationshiptype,
    m2.machineid AS related_machine_id,
    m2.machinenumber AS related_machine_number,
    m2.hostname AS related_hostname,
    CASE WHEN m2.pctypeid IS NOT NULL THEN 'PC' ELSE 'Equipment' END AS related_type,
    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
  AND rt.relationshiptype IN ('Controlled By', 'Controls')
ORDER BY m1.machinenumber, rt.relationshiptype;

SELECT '  ✓ vw_machine_assignments updated' AS status;

-- View 14: vw_machine_assignment_status
-- Purpose: Machine assignment status summary
SELECT 'Updating vw_machine_assignment_status...' AS '';

DROP VIEW IF EXISTS vw_machine_assignment_status;

CREATE VIEW vw_machine_assignment_status AS
SELECT
    m.machineid,
    m.machinenumber,
    m.hostname,
    CASE WHEN m.pctypeid IS NOT NULL THEN 'PC' ELSE 'Equipment' END AS machine_type,
    COUNT(mr.relationshipid) AS relationship_count,
    GROUP_CONCAT(DISTINCT rt.relationshiptype ORDER BY rt.relationshiptype SEPARATOR ', ') AS relationships,
    m.isactive
FROM machines m
LEFT JOIN machinerelationships mr ON m.machineid = mr.machineid AND mr.isactive = 1
LEFT JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid
WHERE m.isactive = 1
GROUP BY m.machineid, m.machinenumber, m.hostname, m.pctypeid, m.isactive
ORDER BY COUNT(mr.relationshipid) DESC, m.machinenumber;

SELECT '  ✓ vw_machine_assignment_status updated' AS status;

-- View 15: vw_multi_pc_machines
-- Purpose: Show machines with multiple PCs assigned
SELECT 'Updating vw_multi_pc_machines...' AS '';

DROP VIEW IF EXISTS vw_multi_pc_machines;

CREATE VIEW vw_multi_pc_machines AS
SELECT
    m.machineid,
    m.machinenumber,
    m.hostname AS machine_hostname,
    COUNT(DISTINCT mr.related_machineid) AS pc_count,
    GROUP_CONCAT(DISTINCT m2.hostname ORDER BY m2.hostname SEPARATOR ', ') AS pc_hostnames
FROM machines m
JOIN machinerelationships mr ON m.machineid = mr.machineid
JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid
JOIN machines m2 ON mr.related_machineid = m2.machineid
WHERE m.pctypeid IS NULL
  AND m2.pctypeid IS NOT NULL
  AND rt.relationshiptype = 'Controls'
  AND mr.isactive = 1
GROUP BY m.machineid, m.machinenumber, m.hostname
HAVING COUNT(DISTINCT mr.related_machineid) > 1
ORDER BY COUNT(DISTINCT mr.related_machineid) DESC, m.machinenumber;

SELECT '  ✓ vw_multi_pc_machines updated' AS status;

-- View 16: vw_pc_resolved_machines
-- Purpose: Resolve PC to their assigned machines
SELECT 'Updating vw_pc_resolved_machines...' AS '';

DROP VIEW IF EXISTS vw_pc_resolved_machines;

CREATE VIEW vw_pc_resolved_machines AS
SELECT
    pcmap.pcid AS pcid,
    m1.machineid AS pc_machineid,
    m1.hostname AS pc_hostname,
    m1.machinenumber AS pc_machinenumber,
    m2.machineid AS assigned_machine_id,
    m2.machinenumber AS assigned_machine_number,
    m2.hostname AS assigned_machine_hostname,
    rt.relationshiptype
FROM machines m1
JOIN pc_to_machine_id_mapping pcmap ON m1.machineid = pcmap.new_machineid
LEFT JOIN machinerelationships mr ON m1.machineid = mr.machineid AND mr.isactive = 1
LEFT JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid AND rt.relationshiptype = 'Controlled By'
LEFT JOIN machines m2 ON mr.related_machineid = m2.machineid
WHERE m1.pctypeid IS NOT NULL
ORDER BY m1.hostname;

SELECT '  ✓ vw_pc_resolved_machines updated' AS status;

-- View 17: vw_ge_machines
-- Purpose: Show GE network machines (specific to organization)
SELECT 'Updating vw_ge_machines...' AS '';

DROP VIEW IF EXISTS vw_ge_machines;

CREATE VIEW vw_ge_machines AS
SELECT
    m.machineid,
    m.machinenumber,
    m.hostname,
    CASE WHEN m.pctypeid IS NOT NULL THEN 'PC' ELSE 'Equipment' END AS machine_type,
    c.address AS ipaddress,
    c.macaddress,
    v.vendor AS manufacturer,
    md.modelnumber AS model,
    m.lastupdated
FROM machines m
LEFT JOIN communications c ON m.machineid = c.machineid
    AND c.comstypeid = (SELECT comstypeid FROM comstypes WHERE typename = 'Network_Interface' LIMIT 1)
    AND c.isactive = 1
LEFT JOIN models md ON m.modelnumberid = md.modelnumberid
LEFT JOIN vendors v ON md.vendorid = v.vendorid
WHERE m.isactive = 1
  AND (c.address LIKE '10.%' OR c.address LIKE '192.168.%')
ORDER BY c.address, m.machinenumber;

SELECT '  ✓ vw_ge_machines updated' AS status;

-- View 18: vw_recent_updates
-- Purpose: Show recently updated machines (PCs and equipment)
SELECT 'Updating vw_recent_updates...' AS '';

DROP VIEW IF EXISTS vw_recent_updates;

CREATE VIEW vw_recent_updates AS
SELECT
    m.machineid,
    m.machinenumber,
    m.hostname,
    CASE
        WHEN m.pctypeid IS NOT NULL THEN CONCAT('PC - ', pt.typename)
        ELSE mt.machinetype
    END AS machine_type,
    v.vendor AS manufacturer,
    md.modelnumber AS model,
    m.lastupdated,
    (TO_DAYS(NOW()) - TO_DAYS(m.lastupdated)) AS days_since_update
FROM machines m
LEFT JOIN models md ON m.modelnumberid = md.modelnumberid
LEFT JOIN vendors v ON md.vendorid = v.vendorid
LEFT JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
LEFT JOIN pctype pt ON m.pctypeid = pt.pctypeid
WHERE m.isactive = 1
  AND m.lastupdated > (NOW() - INTERVAL 7 DAY)
ORDER BY m.lastupdated DESC
LIMIT 100;

SELECT '  ✓ vw_recent_updates updated' AS status;

-- View 19: vw_shopfloor_applications_summary
-- Purpose: Summary of shopfloor applications (may use PC data)
SELECT 'Updating vw_shopfloor_applications_summary...' AS '';

DROP VIEW IF EXISTS vw_shopfloor_applications_summary;

CREATE VIEW vw_shopfloor_applications_summary AS
SELECT
    m.machineid,
    m.machinenumber,
    m.hostname,
    COUNT(DISTINCT mr.related_machineid) AS connected_pc_count,
    GROUP_CONCAT(DISTINCT m2.hostname ORDER BY m2.hostname SEPARATOR ', ') AS connected_pcs,
    COUNT(DISTINCT c.comid) AS comm_config_count
FROM machines m
LEFT JOIN machinerelationships mr ON m.machineid = mr.machineid
    AND mr.isactive = 1
    AND mr.relationshiptypeid = (SELECT relationshiptypeid FROM relationshiptypes WHERE relationshiptype = 'Controls' LIMIT 1)
LEFT JOIN machines m2 ON mr.related_machineid = m2.machineid AND m2.pctypeid IS NOT NULL
LEFT JOIN communications c ON m.machineid = c.machineid AND c.isactive = 1
WHERE m.isactive = 1
  AND m.pctypeid IS NULL
  AND m.machinetypeid = (SELECT machinetypeid FROM machinetypes WHERE machinetype LIKE '%Shopfloor%' LIMIT 1)
GROUP BY m.machineid, m.machinenumber, m.hostname
ORDER BY m.machinenumber;

SELECT '  ✓ vw_shopfloor_applications_summary updated' AS status;

SELECT '' AS '';

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

SELECT '========================================' AS '';
SELECT 'VIEW UPDATE VERIFICATION' AS '';
SELECT '========================================' AS '';
SELECT '' AS '';

-- Count updated views
SELECT 'Updated views:' AS '';
SELECT COUNT(*) as view_count
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'shopdb'
  AND TABLE_NAME IN (
    'vw_active_pcs', 'vw_dnc_config', 'vw_dualpath_management',
    'vw_engineer_pcs', 'vw_ge_machines', 'vw_machine_assignments',
    'vw_machine_assignment_status', 'vw_machine_type_stats',
    'vw_multi_pc_machines', 'vw_pcs_by_hardware', 'vw_pc_network_summary',
    'vw_pc_resolved_machines', 'vw_pc_summary', 'vw_recent_updates',
    'vw_shopfloor_applications_summary', 'vw_shopfloor_comm_config',
    'vw_shopfloor_pcs', 'vw_standard_pcs', 'vw_vendor_summary'
  );

SELECT '' AS '';

-- Test each view by selecting 1 row
SELECT 'Testing views (selecting 1 row from each):' AS '';

SELECT 'vw_active_pcs' AS view_name, COUNT(*) AS row_count FROM vw_active_pcs;
SELECT 'vw_pc_summary' AS view_name, COUNT(*) AS row_count FROM vw_pc_summary;
SELECT 'vw_shopfloor_pcs' AS view_name, COUNT(*) AS row_count FROM vw_shopfloor_pcs;
SELECT 'vw_engineer_pcs' AS view_name, COUNT(*) AS row_count FROM vw_engineer_pcs;
SELECT 'vw_standard_pcs' AS view_name, COUNT(*) AS row_count FROM vw_standard_pcs;
SELECT 'vw_pcs_by_hardware' AS view_name, COUNT(*) AS row_count FROM vw_pcs_by_hardware;
SELECT 'vw_pc_network_summary' AS view_name, COUNT(*) AS row_count FROM vw_pc_network_summary;
SELECT 'vw_dnc_config' AS view_name, COUNT(*) AS row_count FROM vw_dnc_config;
SELECT 'vw_shopfloor_comm_config' AS view_name, COUNT(*) AS row_count FROM vw_shopfloor_comm_config;
SELECT 'vw_machine_type_stats' AS view_name, COUNT(*) AS row_count FROM vw_machine_type_stats;
SELECT 'vw_vendor_summary' AS view_name, COUNT(*) AS row_count FROM vw_vendor_summary;
SELECT 'vw_dualpath_management' AS view_name, COUNT(*) AS row_count FROM vw_dualpath_management;
SELECT 'vw_machine_assignments' AS view_name, COUNT(*) AS row_count FROM vw_machine_assignments;
SELECT 'vw_machine_assignment_status' AS view_name, COUNT(*) AS row_count FROM vw_machine_assignment_status;
SELECT 'vw_multi_pc_machines' AS view_name, COUNT(*) AS row_count FROM vw_multi_pc_machines;
SELECT 'vw_pc_resolved_machines' AS view_name, COUNT(*) AS row_count FROM vw_pc_resolved_machines;
SELECT 'vw_ge_machines' AS view_name, COUNT(*) AS row_count FROM vw_ge_machines;
SELECT 'vw_recent_updates' AS view_name, COUNT(*) AS row_count FROM vw_recent_updates;
SELECT 'vw_shopfloor_applications_summary' AS view_name, COUNT(*) AS row_count FROM vw_shopfloor_applications_summary;

SELECT '' AS '';

SELECT '✓ Script 06 completed successfully' AS status;
SELECT 'All 19 views updated to use machines table' AS '';
SELECT 'Views maintain backward compatibility with pcid references' AS '';

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- All views updated to query machines table instead of pc table
--
-- Key changes:
-- 1. JOIN to pc_to_machine_id_mapping to maintain pcid references
-- 2. Query machines table with filter: pctypeid IS NOT NULL for PCs
-- 3. Warranty data now from warranties table (LEFT JOIN)
-- 4. All output column names preserved for backward compatibility
--
-- Views maintain same output structure for ASP application compatibility
--
-- ROLLBACK: Re-run old view definitions from backup
-- =====================================================
