import { PrismaClient } from '@prisma/client'
import bcrypt from 'bcryptjs'

const prisma = new PrismaClient()

async function main() {
    console.log('🌱 Seeding database...')

    // Seed Areas
    const areas = await Promise.all([
        prisma.area.upsert({
            where: { name: 'Petra' },
            update: {},
            create: { name: 'Petra' }
        }),
        prisma.area.upsert({
            where: { name: 'Jarash' },
            update: {},
            create: { name: 'Jarash' }
        }),
        prisma.area.upsert({
            where: { name: 'Rum' },
            update: {},
            create: { name: 'Rum' }
        })
    ])

    // Seed DredgerLocations
    const dredgerLocations = await Promise.all([
        prisma.dredgerLocation.upsert({
            where: { name: 'SP1-1' },
            update: {},
            create: { name: 'SP1-1', class: 'SP' }
        }),
        prisma.dredgerLocation.upsert({
            where: { name: 'SP1-2' },
            update: {},
            create: { name: 'SP1-2', class: 'SP' }
        }),
        prisma.dredgerLocation.upsert({
            where: { name: 'C01' },
            update: {},
            create: { name: 'C01', class: 'C' }
        }),
        prisma.dredgerLocation.upsert({
            where: { name: 'D1' },
            update: {},
            create: { name: 'D1', class: 'D' }
        })
    ])

    // Seed ReclamationLocations
    const reclamationLocations = await Promise.all([
        prisma.reclamationLocation.upsert({
            where: { name: 'Eastern Shoreline' },
            update: {},
            create: { name: 'Eastern Shoreline' }
        }),
        prisma.reclamationLocation.upsert({
            where: { name: 'Western Shoreline' },
            update: {},
            create: { name: 'Western Shoreline' }
        })
    ])

    // Seed Employee
    // const employee = await prisma.employee.upsert({
    //     where: { username: 'superuser' },
    //     update: {},
    //     create: {
    //         name: 'Super Admin User',
    //         authLevel: 3,
    //         username: '',
    //         email: '@gmail.com',
    //         password: bcrypt.hashSync('', 10)
    //     }
    // })

    // Seed Employee
    //use the .env file SUPER_ADMIN, SUPER_ADMIN_EMAIL and SUPER_ADMIN_PASSWORD
    const superAdmin = await prisma.employee.upsert({
        where: { username: process.env.SUPER_ADMIN },
        update: {},
        create: {
            name: 'Super Admin User',
            authLevel: 3,
            username: process.env.SUPER_ADMIN,
            email: process.env.SUPER_ADMIN_EMAIL,
            password: bcrypt.hashSync(process.env.SUPER_ADMIN_PASSWORD, 10),
            status: 'active'
        }
    })


    // Seed Foreman
    const foreman = await prisma.foreman.upsert({
        where: { id: 1 },
        update: {},
        create: {
            name: 'John Smith'
        }
    })

    // Seed Equipment
    const equipment = await Promise.all([
        prisma.equipment.upsert({
            where: { id: 1 },
            update: {},
            create: { id: 1, category: 'Dozer', model: 'Dozer6', number: 1 }
        }),
        prisma.equipment.upsert({
            where: { id: 2 },
            update: {},
            create: { id: 2, category: 'Dozer', model: 'Dozer6', number: 2 }
        }),
        prisma.equipment.upsert({
            where: { id: 3 },
            update: {},
            create: { id: 3, category: 'Dozer', model: 'Dozer7', number: 1 }
        }),
        prisma.equipment.upsert({
            where: { id: 4 },
            update: {},
            create: { id: 4, category: 'Dozer', model: 'Dozer8', number: 1 }
        }),
        prisma.equipment.upsert({
            where: { id: 5 },
            update: {},
            create: { id: 5, category: 'Loader', model: 'Loader', number: 1 }
        }),
        prisma.equipment.upsert({
            where: { id: 6 },
            update: {},
            create: { id: 6, category: 'Excavator', model: 'Exc.', number: 1 }
        }),
        prisma.equipment.upsert({
            where: { id: 7 },
            update: {},
            create: { id: 7, category: 'Excavator', model: 'Exc.', number: 9 }
        })
    ])

    // Seed Workers
    const workers = await Promise.all([
        prisma.worker.upsert({
            where: { name: 'Ahmed Ali' },
            update: {},
            create: { name: 'Ahmed Ali', status: 'active' }
        }),
        prisma.worker.upsert({
            where: { name: 'Mohammed Hassan' },
            update: {},
            create: { name: 'Mohammed Hassan', status: 'active' }
        }),
        prisma.worker.upsert({
            where: { name: 'Omar Ibrahim' },
            update: {},
            create: { name: 'Omar Ibrahim', status: 'active' }
        }),
        prisma.worker.upsert({
            where: { name: 'Khalid Mahmoud' },
            update: {},
            create: { name: 'Khalid Mahmoud', status: 'active' }
        }),
        prisma.worker.upsert({
            where: { name: 'Youssef Saleh' },
            update: {},
            create: { name: 'Youssef Saleh', status: 'active' }
        })
    ])

    console.log('✅ Database seeded successfully!')
    console.log(`Created ${areas.length} areas`)
    console.log(`Created ${dredgerLocations.length} dredger locations`)
    console.log(`Created ${reclamationLocations.length} reclamation locations`)
    console.log(`Created 1 employee`)
    console.log(`Created 1 foreman`)
    console.log(`Created ${equipment.length} equipment records`)
    console.log(`Created ${workers.length} workers`)
}

main()
    .catch((e) => {
        console.error('❌ Error seeding database:', e)
        process.exit(1)
    })
    .finally(async () => {
        await prisma.$disconnect()
    })