
import { PrismaClient, SensorType } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
    console.log('Mapping Pulse sensor to Demo Room...');

    // 1. Ensure Floor Exists
    let floor = await prisma.facilityFloor.findFirst();

    if (!floor) {
        console.log('No floors found, creating default structure...');
        let property = await prisma.facilityProperty.findFirst();
        if (!property) {
            property = await prisma.facilityProperty.create({
                data: { name: 'Demo Facility' }
            });
        }

        let building = await prisma.facilityBuilding.findFirst();
        if (!building) {
            building = await prisma.facilityBuilding.create({
                data: {
                    propertyId: property.id,
                    name: 'Main Building',
                    code: 'MB',
                    type: 'CULTIVATION'
                }
            });
        }

        floor = await prisma.facilityFloor.create({
            data: {
                buildingId: building.id,
                name: 'Ground Floor',
                number: 1,
                width: 100,
                height: 100
            }
        });
    }

    // 2. Ensure Demo Room Exists
    let room = await prisma.facilityRoom.findFirst({
        where: { name: 'Demo Room' }
    });

    if (!room) {
        console.log('Creating Demo Room...');
        const defaultType = 'FLOWER'; // Assuming enum 'FLOWER' is valid for RoomType
        // Note: RoomType is an enum in schema, need to match it.
        // enum RoomType { VEG, FLOWER, DRY, CURE, MOTHER, CLONE, OTHER }

        room = await prisma.facilityRoom.create({
            data: {
                floorId: floor.id,
                name: 'Demo Room',
                code: 'DEMO',
                type: 'FLOWER',
                posX: 0,
                posY: 0,
                width: 20,
                height: 20
            }
        });
    }
    console.log(`Using Room: ${room.name} (${room.id})`);

    // 3. Upsert Pulse Sensor
    const sensorId = 'pulse-11666';

    const existing = await prisma.sensor.findUnique({
        where: { id: sensorId }
    });

    if (existing) {
        console.log('Sensor already exists, updating connection...');
        await prisma.sensor.update({
            where: { id: sensorId },
            data: {
                roomId: room.id,
                isActive: true,
                type: 'VPD'
            }
        });
    } else {
        console.log('Creating new Pulse sensor...');
        await prisma.sensor.create({
            data: {
                id: sensorId,
                name: 'Veridian Demo Pulse',
                type: 'VPD',
                deviceId: '11666',
                roomId: room.id,
                isActive: true
            }
        });
    }

    console.log('✅ Pulse Sensor 11666 mapped to Demo Room');
}

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