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

async function seedHospitals() {
    try {
        const sales1 = await prisma.user.findFirst({ where: { username: 'sales1' } });
        const sales2 = await prisma.user.findFirst({ where: { username: 'sales2' } });

        if (!sales1 || !sales2) {
            throw new Error('User sales1 or sales2 not found');
        }

        // Ambil province_id dari user_areas
        const sales1Area = await prisma.userArea.findFirst({ where: { user_id: sales1.id } });
        const sales2Area = await prisma.userArea.findFirst({ where: { user_id: sales2.id } });

        if (!sales1Area || !sales2Area) {
            throw new Error('UserArea for sales1 or sales2 not found');
        }

        // Ambil city yang berada di province_id tersebut
        const city1 = await prisma.city.findFirst({ where: { province_id: sales1Area.province_id } });
        const city2 = await prisma.city.findFirst({ where: { province_id: sales2Area.province_id } });

        if (!city1 || !city2) {
            throw new Error('City not found for one of province id');
        }

        // Data rumah sakit
        const hospitals = [
            {
                name: 'RS Sehat Selalu',
                hospital_code: 'RSS001',
                type: 'Tipe B',
                ownership: 'Swasta',
                address: 'Jl. Kesehatan No. 1',
                simrs_type: 'MediSoft',
                contact: '021-123456',
                image: null,
                progress_status: 'cari_data',
                note: 'Calon potensial',
                province_id: sales1Area.province_id,
                city_id: city1.id,
                created_by: sales1.id,
            },
            {
                name: 'RS Harapan Bangsa',
                hospital_code: 'RSB002',
                type: 'Tipe C',
                ownership: 'Pemerintah',
                address: 'Jl. Harapan No. 2',
                simrs_type: 'Hospicare',
                contact: '021-654321',
                image: null,
                progress_status: 'cari_data',
                note: null,
                province_id: sales1Area.province_id,
                city_id: city1.id,
                created_by: sales1.id,
            },
            {
                name: 'RS Mitra Medika',
                hospital_code: 'RSM003',
                type: 'Tipe D',
                ownership: 'Swasta',
                address: 'Jl. Mitra No. 3',
                simrs_type: 'SimRS Platinum',
                contact: '021-999999',
                image: null,
                progress_status: 'cari_data',
                note: 'Baru dihubungi',
                province_id: sales1Area.province_id,
                city_id: city1.id,
                created_by: sales1.id,
            },
            {
                name: 'RS Sembuh Bersama',
                hospital_code: 'RSB004',
                type: 'Tipe B',
                ownership: 'Swasta',
                address: 'Jl. Bersama No. 4',
                simrs_type: 'MediSoft',
                contact: '0274-111111',
                image: null,
                progress_status: 'cari_data',
                note: null,
                province_id: sales2Area.province_id,
                city_id: city2.id,
                created_by: sales2.id,
            },
            {
                name: 'RS Kasih Ibu',
                hospital_code: 'RSK005',
                type: 'Tipe C',
                ownership: 'Yayasan',
                address: 'Jl. Kasih No. 5',
                simrs_type: 'Hospicare',
                contact: '0274-222222',
                image: null,
                progress_status: 'cari_data',
                note: 'Perlu follow up',
                province_id: sales2Area.province_id,
                city_id: city2.id,
                created_by: sales2.id,
            },
            {
                name: 'RS Bhakti Sehat',
                hospital_code: 'RSB006',
                type: 'Tipe D',
                ownership: 'Swasta',
                address: 'Jl. Bhakti No. 6',
                simrs_type: 'SimRS Platinum',
                contact: '0274-333333',
                image: null,
                progress_status: 'cari_data',
                note: null,
                province_id: sales2Area.province_id,
                city_id: city2.id,
                created_by: sales2.id,
            },
        ];

        // Masukkan ke DB
        for (const hospital of hospitals) {
            await prisma.hospital.create({ data: hospital });
        }

        console.log('✅ Hospital seeded!');
    } catch (err) {
        console.error('❌ Error seeding hospital:', err.message);
    }
}

module.exports = { seedHospitals };