/**
 * Verify Staff Dashboard Schema
 */

import { PrismaClient } from '../lib/generated/prisma/index.js';

const prisma = new PrismaClient();

async function verifySchema() {
  console.log('\n🔍 Verifying Staff Dashboard Schema...\n');

  try {
    // Check new tables exist and can be queried
    console.log('✓ Checking Contact table...');
    const contactCount = await prisma.contacts.count();
    console.log(`  Found ${contactCount} contacts`);

    console.log('✓ Checking StaffNote table...');
    const noteCount = await prisma.staffNote.count();
    console.log(`  Found ${noteCount} staff notes`);

    console.log('✓ Checking SiteVisit table...');
    const visitCount = await prisma.siteVisit.count();
    console.log(`  Found ${visitCount} site visits`);

    console.log('✓ Checking StaffAlert table...');
    const alertCount = await prisma.staff_alerts.count();
    console.log(`  Found ${alertCount} staff alerts`);

    console.log('\n✓ Checking User table updates...');
    const staffUsers = await prisma.users.findMany({
      where: {
        role: { in: ['STAFF', 'ADMIN', 'SUPER_ADMIN'] },
      },
      select: {
        username: true,
        role: true,
        isActive: true,
      },
    });
    console.log(`  Found ${staffUsers.length} staff/admin users:`);
    staffUsers.forEach(u => {
      console.log(`    - ${u.username}: ${u.role} (${u.isActive ? 'Active' : 'Inactive'})`);
    });

    console.log('\n✓ Checking DemoBooking table updates...');
    const demosWithStaffFields = await prisma.demo_bookings.findFirst({
      select: {
        assignedTo: true,
        contactedAt: true,
        meetingLink: true,
        internalNotes: true,
      },
    });
    console.log('  Staff fields available:', demosWithStaffFields !== null ? 'Yes' : 'No');

    console.log('\n✅ Schema Verification Complete!\n');
    console.log('All staff dashboard tables and fields are properly configured.\n');
  } catch (error) {
    console.error('\n❌ Schema verification failed:');
    console.error(error.message);
    console.error('\nPlease run: npx prisma db push\n');
  } finally {
    await prisma.$disconnect();
  }
}

verifySchema();
