<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Migration — ShanuFx Portfolio</title>
    <style>
        body{font-family:'JetBrains Mono',monospace;background:#0f0f14;color:#c0caf5;padding:40px}
        #log{white-space:pre-wrap;line-height:1.8}
        button{padding:14px 32px;font-size:1rem;cursor:pointer;background:linear-gradient(135deg,#7c3aed,#06b6d4);color:#fff;border:none;border-radius:8px;margin-bottom:20px;font-family:inherit}
        button:disabled{opacity:.5;cursor:not-allowed}
        .err{color:#f43f5e} .ok{color:#10b981} .warn{color:#f59e0b}
        h1{font-family:'Syne',sans-serif;margin-bottom:10px}
    </style>
</head>
<body>
    <h1>📦 Migrate Existing Content → Firestore</h1>
    <p>Imports all hardcoded portfolio data into your Firebase Firestore database.</p>
    <p><strong>You must be signed in as admin first (open admin.html and login).</strong></p>
    <button id="runBtn" onclick="runMigration()">🚀 Run Migration</button>
    <div id="log"></div>

    <script src="https://www.gstatic.com/firebasejs/10.12.0/firebase-app-compat.js"></script>
    <script src="https://www.gstatic.com/firebasejs/10.12.0/firebase-auth-compat.js"></script>
    <script src="https://www.gstatic.com/firebasejs/10.12.0/firebase-firestore-compat.js"></script>
    <script src="firebase-config.js"></script>
    <script>
        const logEl = document.getElementById('log');
        function log(msg, cls) {
            const s = document.createElement('span');
            s.className = cls || '';
            s.textContent = msg + '\n';
            logEl.appendChild(s);
        }

        // ===== ALL EXISTING DATA =====
        const projectsData = [
            // Flagships
            { title:"NovaMesh", category:"flagship", icon:"fas fa-network-wired", description:"Advanced Android utility for system-level internet stabilization. Optimizes connection routing and hotspot management for lower latency and superior reliability in mission-critical environments.", githubUrl:"https://github.com/ShanudhaTirosh/Novamesh", tags:["Android Internals","Shell Scripting","Networking"], color:"primary", order:1 },
            { title:"SHANU-MD", category:"flagship", icon:"fab fa-whatsapp", description:"A high-intelligence, multi-device WhatsApp automation framework. Engineered for speed and stability, featuring advanced plugin architecture and real-time task orchestration.", githubUrl:"https://github.com/ShanudhaTirosh/SHANU-MD", tags:["Node.js","Automation","APIs"], color:"secondary", order:2 },
            // Advanced
            { title:"SocialGrab", category:"advanced", icon:"fas fa-download", description:"Instant content extraction tool for multiple social media platforms with high-res support.", githubUrl:"https://github.com/ShanudhaTirosh/Socialgrab", tags:["Web"], color:"primary", order:3 },
            { title:"Scrcpy-GUI", category:"advanced", icon:"fas fa-desktop", description:"Streamlined graphical interface for high-performance Android screen mirroring and control.", githubUrl:"https://github.com/ShanudhaTirosh/Scrcpy-GUI", tags:["Desktop"], color:"secondary", order:4 },
            { title:"Face-Sense", category:"advanced", icon:"fas fa-user-shield", description:"School attendance infrastructure utilizing sophisticated face-recognition algorithms for automated tracking.", githubUrl:"https://github.com/ShanudhaTirosh/Face-dedetection-system-", tags:["AI","Python"], color:"primary", order:5 },
            { title:"FlexPOS", category:"advanced", icon:"fas fa-cash-register", description:"Scalable Point of Sale architecture designed for modern retail environments and inventory management.", githubUrl:"https://github.com/ShanudhaTirosh/Flexpos", tags:["Full-Stack"], color:"secondary", order:6 },
            { title:"ExpenseTrack", category:"advanced", icon:"fas fa-chart-pie", description:"Full-stack financial management application featuring real-time analytics and budget optimization logic.", githubUrl:"https://github.com/ShanudhaTirosh/Expenses-tracker", tags:["Finance"], color:"primary", order:7 },
            { title:"Mega-Downloader", category:"advanced", icon:"fas fa-link", description:"Universal file acquisition utility optimized for concurrent streams and connection resumption.", githubUrl:"https://github.com/ShanudhaTirosh/Downloader", tags:["Utility"], color:"secondary", order:8 },
            { title:"Hydro-Calc", category:"advanced", icon:"fas fa-tint", description:"Automated utility billing system with tier-based calculation logic and reporting tools.", githubUrl:"https://github.com/ShanudhaTirosh/waterbill", tags:["Utility"], color:"primary", order:9 },
            { title:"POS-Core", category:"advanced", icon:"fas fa-store", description:"Foundational management system for inventory tracking, sales reporting, and customer insights.", githubUrl:"https://github.com/ShanudhaTirosh/Pos-system", tags:["Full-Stack"], color:"secondary", order:10 },
            // Utilities
            { title:"Finance Calc", category:"utility", icon:"fas fa-calculator", description:"Financial calculator utility", githubUrl:"projects_sub/Financial Calculator.html", tags:[], color:"primary", order:11 },
            { title:"QR Gen", category:"utility", icon:"fas fa-qrcode", description:"QR code generator", githubUrl:"projects_sub/QRCodeGenerator.html", tags:[], color:"secondary", order:12 },
            { title:"Typing Test", category:"utility", icon:"fas fa-keyboard", description:"Typing speed test", githubUrl:"projects_sub/TypingSpeedTest.html", tags:[], color:"primary", order:13 },
            { title:"Math Solver", category:"utility", icon:"fas fa-square-root-variable", description:"Math solver utility", githubUrl:"projects_sub/mathsolver.html", tags:[], color:"secondary", order:14 },
            { title:"Unit Conv", category:"utility", icon:"fas fa-exchange-alt", description:"Unit converter", githubUrl:"projects_sub/UnitConverter.html", tags:[], color:"primary", order:15 },
            { title:"Gallery", category:"utility", icon:"fas fa-images", description:"Image gallery", githubUrl:"projects_sub/ImageGallery.html", tags:[], color:"secondary", order:16 },
            { title:"Task List", category:"utility", icon:"fas fa-tasks", description:"To-do list", githubUrl:"projects_sub/ToDoList.html", tags:[], color:"primary", order:17 },
            { title:"Calendar", category:"utility", icon:"fas fa-calendar-alt", description:"Calendar utility", githubUrl:"projects_sub/calender.html", tags:[], color:"secondary", order:18 },
            { title:"Clock", category:"utility", icon:"fas fa-clock", description:"Clock utility", githubUrl:"projects_sub/clock.html", tags:[], color:"primary", order:19 },
            { title:"Scientific", category:"utility", icon:"fas fa-compass", description:"Scientific calculator", githubUrl:"projects_sub/calculator.html", tags:[], color:"secondary", order:20 },
            // Smart Plant IoT (from projects.html)
            { title:"Smart-Plant-IoT", category:"advanced", icon:"fas fa-leaf", description:"ESP-8266 based hardware project for automated soil moisture tracking and irrigation.", githubUrl:"https://github.com/ShanudhaTirosh/Esp8266-smart-iot-progect", tags:["C++","Hardware"], color:"green", order:21 }
        ];

        const skillsData = [
            { name:"Java Core", icon:"fab fa-java", level:"Specialist", color:"primary", order:1 },
            { name:"Node.js", icon:"fab fa-node-js", level:"Proficient", color:"secondary", order:2 },
            { name:"Python", icon:"fab fa-python", level:"Automation", color:"primary", order:3 },
            { name:"C++ / IoT", icon:"fas fa-microchip", level:"Proficient", color:"secondary", order:4 }
        ];

        const heroData = {
            title1: "I am",
            title2: "ShanuFx",
            roles: ["Android System Specialist","NovaMesh Developer","IoT Integrator","Full-Stack Architect","Performance Engineer","Creative Technologist"],
            description: "> Pushing Android System Internals to the limit.\n> Stabilizing the future of mobile networking with NovaMesh.\n> Full-stack development with a performance-first mindset.",
            tags: ["Internals","Networking","IoT","Full-Stack"]
        };

        const aboutData = {
            paragraph1: '[ <span class="text-primary">INIT</span> ] I\'m a 17.5-year-old <b>System Developer</b> from Sri Lanka. I view <strong class="text-white">Code as my Canvas</strong>, and performance as my priority.',
            paragraph2: 'A student at <strong class="text-secondary">G/Dharmashoka College</strong>, I specialize in <b>Android System Internals</b> and <b>IoT automation</b>. I\'m driven by the challenge of making devices smarter, faster, and more stable.',
            paragraph3: 'As the creator of <strong class="text-gradient">NovaMesh</strong>, I\'ve developed core system-level utilities that stabilize mobile internet and optimize hotspot management for power users.',
            tagline: "Exploring the limits of system performance.",
            stat1Value: "20+", stat1Label: "Repositories",
            stat2Value: "5k+", stat2Label: "Reach",
            stat3Value: "1", stat3Label: "Purpose"
        };

        const socialData = {
            facebook: "https://web.facebook.com/tirosh.shanudha/",
            instagram: "https://www.instagram.com/shanudha_tirosh/",
            linkedin: "https://lk.linkedin.com/in/shanudha-tirosh-93454b305",
            github: "https://github.com/ShanudhaTirosh",
            youtube: "https://www.youtube.com/@SHANU_FX_LK",
            tiktok: "https://www.tiktok.com/@shanu_fx?is_from_webapp=1&sender_device=pc",
            email: "tiroshbrot123@gmail.com"
        };

        async function runMigration() {
            const btn = document.getElementById('runBtn');
            btn.disabled = true; btn.textContent = '⏳ Running...';

            const user = auth.currentUser;
            if (!user) { log('❌ Not signed in! Open admin.html first.','err'); btn.disabled=false; btn.textContent='🚀 Run Migration'; return; }
            log('✅ Signed in as: ' + user.email, 'ok');

            // Projects
            const existingProj = await db.collection('projects').get();
            if (existingProj.empty) {
                log('\n📁 Importing ' + projectsData.length + ' projects...\n');
                let s = 0;
                for (const p of projectsData) {
                    try {
                        await db.collection('projects').add({ ...p, createdAt: firebase.firestore.FieldValue.serverTimestamp() });
                        log('  ✅ ' + p.title, 'ok'); s++;
                    } catch(e) { log('  ❌ ' + p.title + ' — ' + e.message, 'err'); }
                }
                log('\n✅ Projects: ' + s + '/' + projectsData.length, 'ok');
            } else { log('⚠️ Projects already exist (' + existingProj.size + '). Skipped.', 'warn'); }

            // Skills
            const existingSkills = await db.collection('skills').get();
            if (existingSkills.empty) {
                log('\n📁 Importing ' + skillsData.length + ' skills...\n');
                let s = 0;
                for (const sk of skillsData) {
                    try {
                        await db.collection('skills').add({ ...sk, createdAt: firebase.firestore.FieldValue.serverTimestamp() });
                        log('  ✅ ' + sk.name, 'ok'); s++;
                    } catch(e) { log('  ❌ ' + sk.name + ' — ' + e.message, 'err'); }
                }
                log('\n✅ Skills: ' + s + '/' + skillsData.length, 'ok');
            } else { log('⚠️ Skills already exist. Skipped.', 'warn'); }

            // Settings
            try {
                const heroDoc = await db.collection('siteSettings').doc('hero').get();
                if (!heroDoc.exists) {
                    await db.collection('siteSettings').doc('hero').set(heroData);
                    log('✅ Hero settings imported', 'ok');
                } else { log('⚠️ Hero settings exist. Skipped.', 'warn'); }

                const aboutDoc = await db.collection('siteSettings').doc('about').get();
                if (!aboutDoc.exists) {
                    await db.collection('siteSettings').doc('about').set(aboutData);
                    log('✅ About settings imported', 'ok');
                } else { log('⚠️ About settings exist. Skipped.', 'warn'); }

                const socialDoc = await db.collection('siteSettings').doc('social').get();
                if (!socialDoc.exists) {
                    await db.collection('siteSettings').doc('social').set(socialData);
                    log('✅ Social links imported', 'ok');
                } else { log('⚠️ Social links exist. Skipped.', 'warn'); }
            } catch(e) { log('❌ Settings error: ' + e.message, 'err'); }

            log('\n========================================');
            log('🎉 Migration complete!', 'ok');
            log('You can now delete this migrate.html file.', 'ok');
            btn.textContent = '✅ Done!';
        }
    </script>
</body>
</html>
