import mongoose from 'mongoose'
import Member from '../server/models/member.js'
import { connectDB } from '../server/utils/mongoose.js'
import dotenv from 'dotenv'

// Load environment variables
dotenv.config()

const sampleMembers = [
  {
    email: 'alex.rivera@pixelcollective.coop',
    name: 'Alex Rivera',
    circle: 'founder',
    contributionTier: '50',
    slackInvited: true,
    createdAt: new Date('2024-01-15'),
    lastLogin: new Date('2025-08-20')
  },
  {
    email: 'sam.chen@legalcoop.com',
    name: 'Sam Chen',
    circle: 'practitioner',
    contributionTier: '30',
    slackInvited: true,
    createdAt: new Date('2024-02-03'),
    lastLogin: new Date('2025-08-18')
  },
  {
    email: 'maria.garcia@collectivegames.coop',
    name: 'Maria Garcia',
    circle: 'founder',
    contributionTier: '50',
    helcimCustomerId: 'cust_12345',
    helcimSubscriptionId: 'sub_67890',
    slackInvited: true,
    createdAt: new Date('2024-03-10'),
    lastLogin: new Date('2025-08-25')
  },
  {
    email: 'david.park@impactinvest.org',
    name: 'David Park',
    circle: 'practitioner',
    contributionTier: '30',
    slackInvited: true,
    createdAt: new Date('2024-04-12'),
    lastLogin: new Date('2025-08-22')
  },
  {
    email: 'jennifer.wu@grantspecialist.org',
    name: 'Jennifer Wu',
    circle: 'practitioner',
    contributionTier: '15',
    slackInvited: true,
    createdAt: new Date('2024-05-08'),
    lastLogin: new Date('2025-08-19')
  },
  {
    email: 'jordan.lee@indiedev.com',
    name: 'Jordan Lee',
    circle: 'community',
    contributionTier: '15',
    slackInvited: false,
    createdAt: new Date('2024-06-20'),
    lastLogin: new Date('2025-08-15')
  },
  {
    email: 'taylor.smith@gamemaker.studio',
    name: 'Taylor Smith',
    circle: 'community',
    contributionTier: '5',
    slackInvited: true,
    createdAt: new Date('2024-07-15'),
    lastLogin: new Date('2025-08-10')
  },
  {
    email: 'casey.wong@studiocoop.dev',
    name: 'Casey Wong',
    circle: 'founder',
    contributionTier: '30',
    helcimCustomerId: 'cust_54321',
    slackInvited: true,
    createdAt: new Date('2024-08-01'),
    lastLogin: new Date('2025-08-24')
  },
  {
    email: 'riley.johnson@cooperativedev.org',
    name: 'Riley Johnson',
    circle: 'community',
    contributionTier: '0',
    slackInvited: false,
    createdAt: new Date('2024-08-15'),
    lastLogin: new Date('2025-08-12')
  },
  {
    email: 'morgan.davis@gamecollective.coop',
    name: 'Morgan Davis',
    circle: 'founder',
    contributionTier: '50',
    helcimCustomerId: 'cust_98765',
    helcimSubscriptionId: 'sub_13579',
    slackInvited: true,
    createdAt: new Date('2024-09-01'),
    lastLogin: new Date('2025-08-26')
  },
  {
    email: 'avery.brown@newdevstudio.com',
    name: 'Avery Brown',
    circle: 'community',
    contributionTier: '5',
    slackInvited: false,
    createdAt: new Date('2024-10-10'),
    lastLogin: new Date('2025-08-14')
  },
  {
    email: 'phoenix.martinez@coopgames.dev',
    name: 'Phoenix Martinez',
    circle: 'practitioner',
    contributionTier: '15',
    slackInvited: true,
    createdAt: new Date('2024-11-05'),
    lastLogin: new Date('2025-08-21')
  },
  {
    email: 'sage.anderson@collaborativestudio.org',
    name: 'Sage Anderson',
    circle: 'community',
    contributionTier: '15',
    slackInvited: true,
    createdAt: new Date('2024-12-01'),
    lastLogin: new Date('2025-08-16')
  },
  {
    email: 'dakota.wilson@indieguildstudio.com',
    name: 'Dakota Wilson',
    circle: 'founder',
    contributionTier: '30',
    slackInvited: true,
    createdAt: new Date('2025-01-10'),
    lastLogin: new Date('2025-08-23')
  },
  {
    email: 'charlie.thompson@gamecooperative.net',
    name: 'Charlie Thompson',
    circle: 'practitioner',
    contributionTier: '50',
    helcimCustomerId: 'cust_11111',
    helcimSubscriptionId: 'sub_22222',
    slackInvited: true,
    createdAt: new Date('2025-02-14'),
    lastLogin: new Date('2025-08-25')
  }
]

async function seedMembers() {
  try {
    await connectDB()
    
    // Clear existing members
    await Member.deleteMany({})
    console.log('Cleared existing members')
    
    // Insert sample members
    await Member.insertMany(sampleMembers)
    console.log(`Added ${sampleMembers.length} sample members`)
    
    // Verify insertion and show summary
    const count = await Member.countDocuments()
    console.log(`Total members in database: ${count}`)
    
    // Show breakdown by circle
    const circleBreakdown = await Member.aggregate([
      { $group: { _id: '$circle', count: { $sum: 1 } } },
      { $sort: { _id: 1 } }
    ])
    
    console.log('\nBreakdown by circle:')
    circleBreakdown.forEach(circle => {
      console.log(`  ${circle._id}: ${circle.count} members`)
    })
    
    // Show breakdown by contribution tier
    const tierBreakdown = await Member.aggregate([
      { $group: { _id: '$contributionTier', count: { $sum: 1 } } },
      { $sort: { _id: 1 } }
    ])
    
    console.log('\nBreakdown by contribution tier:')
    tierBreakdown.forEach(tier => {
      console.log(`  $${tier._id}: ${tier.count} members`)
    })
    
    process.exit(0)
  } catch (error) {
    console.error('Error seeding members:', error)
    process.exit(1)
  }
}

seedMembers()