/**
 * FIX SECTION POSITIONS - 777 Wolfpack 3D Viewer
 * 
 * Problem: All sections (A1-A6, B1-B6) have identical coordinates,
 * causing all Flower Rooms to stack on top of each other in the 3D viewer.
 * 
 * Solution: Offset each room's sections horizontally so they don't overlap.
 * - Flower Room A sections: X offset = 0
 * - Flower Room B sections: X offset = 700 (one room width)
 * - Flower Room C sections: X offset = 0, Y offset = 700 (below Room A)
 * 
 * Run: node prisma/seed-fix-sections.js
 */

const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function main() {
    console.log('🔧 Fixing section positions for 3D viewer...\n');

    // Define room offsets (in pixels)
    const roomOffsets = {
        'Flower Room A': { x: 0, y: 0 },
        'Flower Room B': { x: 700, y: 0 },     // Offset right
        'Flower Room C': { x: 0, y: 700 },     // Offset down
        'Veg Room 1': { x: 1400, y: 0 },       // Far right
        'Veg Room 2': { x: 1400, y: 700 },     // Far right, down
        'Dry Room': { x: 0, y: 1400 },         // Bottom left
        'Cure Room': { x: 700, y: 1400 },      // Bottom center
        'Mother Room': { x: 1400, y: 1400 },   // Bottom right
    };

    // Update room positions to match the layout
    console.log('📍 Updating room positions...\n');

    for (const [roomName, offset] of Object.entries(roomOffsets)) {
        const result = await prisma.facilityRoom.updateMany({
            where: { name: roomName },
            data: {
                posX: offset.x,
                posY: offset.y,
                width: 650,  // Standard room width
                height: 650   // Standard room height
            }
        });

        console.log(`  ${roomName}: posX=${offset.x}, posY=${offset.y} (updated ${result.count})`);
    }

    console.log('\n📐 Updating section positions (relative to rooms)...\n');

    // Get all rooms with their sections
    const rooms = await prisma.facilityRoom.findMany({
        include: { sections: true }
    });

    for (const room of rooms) {
        const offset = roomOffsets[room.name];
        if (!offset) continue;

        for (const section of room.sections) {
            // Section positions are currently in a 0-700 grid
            // Keep them relative, but now the room offset handles separation
            const newPosX = section.posX + offset.x;
            const newPosY = section.posY + offset.y;

            await prisma.facilitySection.update({
                where: { id: section.id },
                data: {
                    posX: newPosX,
                    posY: newPosY
                }
            });

            console.log(`  ${room.name} / ${section.code}: (${section.posX}, ${section.posY}) → (${newPosX}, ${newPosY})`);
        }
    }

    // Update floor dimensions to fit all rooms
    console.log('\n📏 Updating floor dimensions...\n');

    await prisma.facilityFloor.updateMany({
        data: {
            width: 2100,  // 3 columns × 700
            height: 2100  // 3 rows × 700
        }
    });
    console.log('  Floor dimensions set to 2100 × 2100');

    // Verify
    console.log('\n✅ Verification:');
    const verification = await prisma.facilityRoom.findMany({
        select: { name: true, posX: true, posY: true, width: true, height: true },
        orderBy: { name: 'asc' }
    });

    console.table(verification);
    console.log('\n🎉 Section positions fixed! Redeploy to see changes.');
}

main()
    .catch((e) => {
        console.error('Fix failed:', e);
        process.exit(1);
    })
    .finally(async () => {
        await prisma.$disconnect();
    });
