const { PrismaClient, RoomType, SectionType } = require('@prisma/client');

const prisma = new PrismaClient();

async function main() {
    console.log('Seeding database with realistic 3D coordinates...');

    // Seed Roles
    const rolesData = [
        {
            name: 'Facility Owner',
            permissions: { admin: true },
            isSystem: true
        },
        {
            name: 'Manager',
            permissions: {
                users: { manage: true },
                tasks: { manage: true },
                inventory: { manage: true }
            },
            isSystem: true
        },
        {
            name: 'Grower',
            permissions: {
                tasks: { view: true, complete: true },
                inventory: { view: true }
            },
            isSystem: true
        },
        {
            name: 'Worker',
            permissions: {
                tasks: { view: true, complete: true }
            },
            isSystem: true
        }
    ];

    for (const r of rolesData) {
        const existing = await prisma.role.findUnique({ where: { name: r.name } });
        if (!existing) {
            await prisma.role.create({ data: r });
            console.log(`Created Role: ${r.name}`);
        }
    }

    const ownerRole = await prisma.role.findUnique({ where: { name: 'Facility Owner' } });

    // Create Owner
    const ownerEmail = 'admin@runfoo.run';
    const existingOwner = await prisma.user.findUnique({ where: { email: ownerEmail } });

    if (!existingOwner) {
        await prisma.user.create({
            data: {
                email: ownerEmail,
                passwordHash: 'password123',
                name: 'Travis',
                role: 'OWNER',
                roleId: ownerRole?.id,
                rate: 100.00
            }
        });
        console.log('Created Owner: Travis (admin@runfoo.com)');
    } else {
        // Update existing owner to have roleId if missing
        if (!existingOwner.roleId && ownerRole) {
            await prisma.user.update({
                where: { email: ownerEmail },
                data: { roleId: ownerRole.id, name: 'Travis' }
            });
            console.log('Updated Owner permissions');
        }
    }

    // Create Default Supplies
    const supplies = [
        {
            name: 'Nitrile Gloves (L)',
            category: 'PPE',
            quantity: 5,
            minThreshold: 10,
            unit: 'box',
            location: 'Ante Room',
            vendor: 'Uline',
            productUrl: 'https://uline.com'
        },
        {
            name: 'Hypochlorous Acid',
            category: 'CLEANING',
            quantity: 12,
            minThreshold: 5,
            unit: 'gallon',
            location: 'Janitor Clonet',
            vendor: 'Athena'
        },
        {
            name: 'Rockwool Cubes 4"',
            category: 'OTHER',
            quantity: 450,
            minThreshold: 200,
            unit: 'cube',
            location: 'Veg Storage',
            vendor: 'GrowGen'
        },
        {
            name: 'Trimmers (Fiskars)',
            category: 'MAINTENANCE',
            quantity: 15,
            minThreshold: 15,
            unit: 'pair',
            location: 'Trim Jail',
            vendor: 'Amazon'
        }
    ];

    for (const s of supplies) {
        const existing = await prisma.supplyItem.findFirst({ where: { name: s.name } });
        if (!existing) {
            await prisma.supplyItem.create({
                data: {
                    ...s,
                    category: s.category
                }
            });
            console.log(`Created Supply: ${s.name}`);
        }
    }

    // ============================================
    // FACILITY 3D LAYOUT (New)
    // ============================================

    // 1. Property
    let property = await prisma.facilityProperty.findFirst();
    if (!property) {
        property = await prisma.facilityProperty.create({
            data: {
                name: 'Wolfpack Facility',
                address: '123 Grow St',
                licenseNum: 'CML-123456'
            }
        });
        console.log('Created Facility Property');
    }

    // 2. Building
    let building = await prisma.facilityBuilding.findFirst({ where: { propertyId: property.id } });
    if (!building) {
        building = await prisma.facilityBuilding.create({
            data: {
                propertyId: property.id,
                name: 'Main Building',
                code: 'MAIN',
                type: 'CULTIVATION'
            }
        });
        console.log('Created Facility Building');
    }

    // 3. Floor
    let floor = await prisma.facilityFloor.findFirst({ where: { buildingId: building.id } });
    if (!floor) {
        floor = await prisma.facilityFloor.create({
            data: {
                buildingId: building.id,
                name: 'Ground Floor',
                number: 1,
                width: 100, // 100x100 grid
                height: 100
            }
        });
        console.log('Created Facility Floor');
    }

    // 4. Rooms (Spatial Layout Organization)
    // We define explicit positions for known room names to ensure a clean, non-overlapping layout.
    // Any existing rooms matching these names will be moved to these coordinates.
    const layoutMap = new Map([
        // Legacy Rooms (from original seed)
        ['Veg Room 1', { x: 5, y: 5, w: 30, h: 40, c: '#4ade80', type: RoomType.VEG }],
        ['Veg Room 2', { x: 5, y: 55, w: 30, h: 40, c: '#4ade80', type: RoomType.VEG }],
        ['Flower Room A', { x: 45, y: 5, w: 50, h: 60, c: '#a855f7', type: RoomType.FLOWER }],
        ['Flower Room B', { x: 45, y: 75, w: 50, h: 60, c: '#a855f7', type: RoomType.FLOWER }],

        // New/Demo Rooms
        ['Veg Room', { x: 5, y: 105, w: 30, h: 40, c: '#4ade80', type: RoomType.VEG }],
        ['Flower Room', { x: 45, y: 145, w: 50, h: 60, c: '#a855f7', type: RoomType.FLOWER }],

        // Common Rooms
        ['Dry Room', { x: 105, y: 5, w: 25, h: 30, c: '#f59e0b', type: RoomType.DRY }],
        ['Mother Room', { x: 105, y: 45, w: 25, h: 25, c: '#4ade80', type: RoomType.MOTHER }],
        ['Clone Room', { x: 105, y: 80, w: 20, h: 20, c: '#4ade80', type: RoomType.CLONE }],
    ]);

    // 1. Update/Move Existing Rooms
    const existingRooms = await prisma.facilityRoom.findMany({
        where: { floorId: floor.id }
    });

    for (const room of existingRooms) {
        if (layoutMap.has(room.name)) {
            const layout = layoutMap.get(room.name);
            await prisma.facilityRoom.update({
                where: { id: room.id },
                data: {
                    posX: layout.x,
                    posY: layout.y,
                    width: layout.w,
                    height: layout.h,
                    color: layout.c
                }
            });
            console.log(`Updated Layout for: ${room.name}`);
            // Remove from map so we don't double-create
            layoutMap.delete(room.name);
        } else {
            // Move unknown/other rooms out of the way to a "Garage" area
            console.log(`Moving unknown room to storage area: ${room.name}`);
            await prisma.facilityRoom.update({
                where: { id: room.id },
                data: { posX: -100, posY: -100 } // Hide off-map or in negative space
            });
        }
    }

    // 2. Create missing rooms from the map
    for (const [name, layout] of layoutMap) {
        const room = await prisma.facilityRoom.create({
            data: {
                floorId: floor.id,
                name: name,
                code: name.substring(0, 3).toUpperCase(),
                type: layout.type,
                posX: layout.x,
                posY: layout.y,
                width: layout.w,
                height: layout.h,
                color: layout.c
            }
        });
        console.log(`Created Missing Room: ${name}`);

        // Add basic sections if it's a new room
        if (layout.type === RoomType.VEG || layout.type === RoomType.FLOWER) {
            const isVeg = layout.type === RoomType.VEG;
            await prisma.facilitySection.create({
                data: {
                    roomId: room.id,
                    name: isVeg ? 'Rack A' : 'Bench 1',
                    code: isVeg ? 'RA' : 'B1',
                    type: SectionType.RACK,
                    posX: 2, posY: 2,
                    width: 10, height: 20,
                    rows: 4, columns: 4, spacing: 2,
                    positions: {
                        create: Array(16).fill({ row: 1, column: 1 }).map((_, i) => ({
                            row: Math.floor(i / 4) + 1, column: (i % 4) + 1, tier: 1
                        }))
                    }
                }
            });
        }
    }

    console.log('Seeding complete.');
}

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