-- =====================================================
-- SCRIPT 04: Migrate PC Warranty Data
-- =====================================================
-- Date: 2025-11-06
-- Purpose: Migrate PC warranty data to warranties table
-- Status: REQUIRES TESTING - Depends on Script 01
-- Estimated Time: 2-3 minutes
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

-- =====================================================
-- STEP 1: Pre-migration checks
-- =====================================================

SELECT '========================================' AS '';
SELECT 'WARRANTY DATA MIGRATION' AS '';
SELECT '========================================' AS '';
SELECT '' AS '';

-- Count PCs with warranty data
SELECT CONCAT('PCs with warranty end dates: ', COUNT(*)) AS info
FROM pc
WHERE warrantyenddate IS NOT NULL
  AND isactive = 1;

-- Show warranty status distribution
SELECT 'Warranty status distribution:' AS '';
SELECT
    CASE
        WHEN warrantyenddate IS NULL THEN 'No warranty data'
        WHEN warrantyenddate < CURDATE() THEN 'Expired'
        WHEN warrantyenddate < DATE_ADD(CURDATE(), INTERVAL 90 DAY) THEN 'Expiring soon'
        ELSE 'Active'
    END AS status,
    COUNT(*) as count
FROM pc
WHERE isactive = 1
GROUP BY status
ORDER BY count DESC;

SELECT '' AS '';

-- =====================================================
-- STEP 2: Show sample data
-- =====================================================

SELECT 'Sample warranty data (first 5):' AS '';
SELECT
    p.pcid,
    p.hostname,
    m.new_machineid,
    p.warrantyenddate,
    p.warrantyservicelevel,
    p.warrantylastchecked
FROM pc p
LEFT JOIN pc_to_machine_id_mapping m ON p.pcid = m.pcid
WHERE p.warrantyenddate IS NOT NULL
  AND p.isactive = 1
LIMIT 5;

SELECT '' AS '';

-- =====================================================
-- STEP 3: Migrate warranty data
-- =====================================================

SELECT 'Migrating warranty data...' AS '';

INSERT INTO warranties (
    machineid,
    warrantyname,
    enddate,
    servicelevel,
    lastcheckeddate
)
SELECT
    m.new_machineid,
    NULL AS warrantyname,           -- Not in old PC table, set to NULL
    p.warrantyenddate AS enddate,
    p.warrantyservicelevel AS servicelevel,
    p.warrantylastchecked AS lastcheckeddate
FROM pc p
JOIN pc_to_machine_id_mapping m ON p.pcid = m.pcid
WHERE p.warrantyenddate IS NOT NULL  -- Only migrate if warranty date exists
  AND p.isactive = 1
  AND m.new_machineid IS NOT NULL;

SELECT CONCAT('✓ Migrated ', ROW_COUNT(), ' warranty records') AS status;

SELECT '' AS '';

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

SELECT '========================================' AS '';
SELECT 'MIGRATION VERIFICATION' AS '';
SELECT '========================================' AS '';
SELECT '' AS '';

-- Count warranties
SELECT CONCAT('Total warranty records: ', COUNT(*)) AS info
FROM warranties;

-- Warranty status using view
SELECT 'Warranty status summary:' AS '';
SELECT
    warrantystatus,
    COUNT(*) as count
FROM vw_warranty_status
WHERE machineid IN (SELECT new_machineid FROM pc_to_machine_id_mapping)
GROUP BY warrantystatus
ORDER BY count DESC;

SELECT '' AS '';

-- Sample migrated warranties
SELECT 'Sample migrated warranties:' AS '';
SELECT
    w.warrantyid,
    m.hostname,
    w.enddate,
    w.servicelevel,
    DATEDIFF(w.enddate, CURDATE()) as days_remaining
FROM warranties w
JOIN machines m ON w.machineid = m.machineid
WHERE m.pctypeid IS NOT NULL
LIMIT 5;

SELECT '' AS '';

-- Expiring warranties
SELECT 'Warranties expiring in next 90 days:' AS '';
SELECT COUNT(*) as count
FROM vw_warranties_expiring
WHERE machineid IN (SELECT new_machineid FROM pc_to_machine_id_mapping);

SELECT '' AS '';

SELECT '✓ Script 04 completed successfully' AS status;
SELECT 'Next: Run script 05_migrate_dualpath_assignments.sql' AS '';

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- Migrated fields from pc table:
-- - warrantyenddate → warranties.enddate
-- - warrantyservicelevel → warranties.servicelevel
-- - warrantylastchecked → warranties.lastcheckeddate
-- - warrantyname → NULL (not in old schema)
--
-- Computed fields (not stored):
-- - warrantystatus (computed in vw_warranty_status view)
-- - warrantydaysremaining (computed in vw_warranty_status view)
--
-- Only migrated PCs with warrantyenddate NOT NULL
--
-- ROLLBACK: DELETE FROM warranties WHERE machineid IN
--           (SELECT new_machineid FROM pc_to_machine_id_mapping)
-- =====================================================
