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

dotenv.config()

const sampleMembers = [
  {
    email: 'alex.rivera@pixelcollective.coop',
    name: 'Alex Rivera',
    circle: 'founder',
    contributionAmount: 50,
    status: 'active',
    avatar: 'sweet',
    slackInvited: true,
    craftTags: ['game-design', 'production-management', 'business-development'],
    board: { slackHandle: 'alex.rivera' },
    createdAt: new Date('2024-01-15'),
    lastLogin: new Date('2026-04-10'),
  },
  {
    email: 'sam.chen@legalcoop.com',
    name: 'Sam Chen',
    circle: 'practitioner',
    contributionAmount: 30,
    status: 'active',
    avatar: 'mild',
    slackInvited: true,
    craftTags: ['business-development', 'marketing-and-comms'],
    board: { slackHandle: 'sam.chen' },
    createdAt: new Date('2024-02-03'),
    lastLogin: new Date('2026-04-08'),
  },
  {
    email: 'maria.garcia@collectivegames.coop',
    name: 'Maria Garcia',
    circle: 'founder',
    contributionAmount: 50,
    status: 'active',
    avatar: 'double-take',
    helcimCustomerId: 'cust_12345',
    helcimSubscriptionId: 'sub_67890',
    slackInvited: true,
    craftTags: ['programming', 'devops-and-tools', 'game-design', 'qa-and-testing'],
    board: { slackHandle: 'maria.g' },
    createdAt: new Date('2024-03-10'),
    lastLogin: new Date('2026-04-12'),
  },
  {
    email: 'david.park@impactinvest.org',
    name: 'David Park',
    circle: 'practitioner',
    contributionAmount: 30,
    status: 'active',
    avatar: 'exasperated',
    slackInvited: true,
    craftTags: ['business-development', 'analytics-and-data'],
    board: { slackHandle: 'david.park' },
    createdAt: new Date('2024-04-12'),
    lastLogin: new Date('2026-04-09'),
  },
  {
    email: 'jennifer.wu@grantspecialist.org',
    name: 'Jennifer Wu',
    circle: 'practitioner',
    contributionAmount: 15,
    status: 'active',
    avatar: 'disbelieving',
    slackInvited: true,
    craftTags: ['education-and-mentoring', 'community-management'],
    board: {},
    createdAt: new Date('2024-05-08'),
    lastLogin: new Date('2026-04-05'),
  },
  {
    email: 'jordan.lee@indiedev.com',
    name: 'Jordan Lee',
    circle: 'community',
    contributionAmount: 15,
    status: 'active',
    avatar: 'wtf',
    slackInvited: true,
    craftTags: ['programming', 'game-design', 'audio-and-music'],
    board: { slackHandle: 'jordan.lee' },
    createdAt: new Date('2024-06-20'),
    lastLogin: new Date('2026-04-07'),
  },
  {
    email: 'taylor.smith@gamemaker.studio',
    name: 'Taylor Smith',
    circle: 'community',
    contributionAmount: 5,
    status: 'active',
    avatar: 'sweet',
    slackInvited: true,
    craftTags: ['art-and-animation', 'ux-and-ui-design', 'accessibility'],
    board: {},
    createdAt: new Date('2024-07-15'),
    lastLogin: new Date('2026-04-01'),
  },
  {
    email: 'casey.wong@studiocoop.dev',
    name: 'Casey Wong',
    circle: 'founder',
    contributionAmount: 30,
    status: 'active',
    avatar: 'mild',
    helcimCustomerId: 'cust_54321',
    slackInvited: true,
    craftTags: ['programming', 'devops-and-tools', 'production-management'],
    board: { slackHandle: 'casey.w' },
    createdAt: new Date('2024-08-01'),
    lastLogin: new Date('2026-04-11'),
  },
  {
    email: 'riley.johnson@cooperativedev.org',
    name: 'Riley Johnson',
    circle: 'community',
    contributionAmount: 0,
    status: 'active',
    avatar: 'double-take',
    slackInvited: false,
    craftTags: ['narrative-design', 'localization'],
    board: {},
    createdAt: new Date('2024-08-15'),
    lastLogin: new Date('2026-03-28'),
  },
  {
    email: 'morgan.davis@gamecollective.coop',
    name: 'Morgan Davis',
    circle: 'founder',
    contributionAmount: 50,
    status: 'active',
    avatar: 'exasperated',
    helcimCustomerId: 'cust_98765',
    helcimSubscriptionId: 'sub_13579',
    slackInvited: true,
    craftTags: ['game-design', 'production-management', 'marketing-and-comms', 'business-development'],
    board: { slackHandle: 'morgan.d' },
    createdAt: new Date('2024-09-01'),
    lastLogin: new Date('2026-04-13'),
  },
  {
    email: 'avery.brown@newdevstudio.com',
    name: 'Avery Brown',
    circle: 'community',
    contributionAmount: 5,
    status: 'active',
    avatar: 'disbelieving',
    slackInvited: false,
    craftTags: ['programming', 'qa-and-testing'],
    board: {},
    createdAt: new Date('2024-10-10'),
    lastLogin: new Date('2026-03-20'),
  },
  {
    email: 'phoenix.martinez@coopgames.dev',
    name: 'Phoenix Martinez',
    circle: 'practitioner',
    contributionAmount: 15,
    status: 'active',
    avatar: 'wtf',
    slackInvited: true,
    craftTags: ['community-management', 'education-and-mentoring', 'marketing-and-comms'],
    board: { slackHandle: 'phoenix.m' },
    createdAt: new Date('2024-11-05'),
    lastLogin: new Date('2026-04-06'),
  },
  {
    email: 'sage.anderson@collaborativestudio.org',
    name: 'Sage Anderson',
    circle: 'community',
    contributionAmount: 15,
    status: 'active',
    avatar: 'sweet',
    slackInvited: true,
    craftTags: ['narrative-design', 'accessibility', 'education-and-mentoring'],
    board: { slackHandle: 'sage.a' },
    createdAt: new Date('2024-12-01'),
    lastLogin: new Date('2026-04-02'),
  },
  {
    email: 'dakota.wilson@indieguildstudio.com',
    name: 'Dakota Wilson',
    circle: 'founder',
    contributionAmount: 30,
    status: 'active',
    avatar: 'mild',
    slackInvited: true,
    craftTags: ['game-design', 'art-and-animation', 'audio-and-music'],
    board: { slackHandle: 'dakota.w' },
    createdAt: new Date('2025-01-10'),
    lastLogin: new Date('2026-04-10'),
  },
  {
    email: 'charlie.thompson@gamecooperative.net',
    name: 'Charlie Thompson',
    circle: 'practitioner',
    contributionAmount: 50,
    status: 'active',
    avatar: 'double-take',
    helcimCustomerId: 'cust_11111',
    helcimSubscriptionId: 'sub_22222',
    slackInvited: true,
    craftTags: ['business-development', 'analytics-and-data', 'production-management'],
    board: { slackHandle: 'charlie.t' },
    createdAt: new Date('2025-02-14'),
    lastLogin: new Date('2026-04-12'),
  },
  // Additional members for more Board density
  {
    email: 'robin.nakamura@workerowned.games',
    name: 'Robin Nakamura',
    circle: 'founder',
    contributionAmount: 50,
    status: 'active',
    avatar: 'exasperated',
    slackInvited: true,
    craftTags: ['programming', 'game-design', 'devops-and-tools'],
    board: { slackHandle: 'robin.n' },
    createdAt: new Date('2025-03-01'),
    lastLogin: new Date('2026-04-13'),
  },
  {
    email: 'emery.okafor@solidaritygames.org',
    name: 'Emery Okafor',
    circle: 'community',
    contributionAmount: 15,
    status: 'active',
    avatar: 'wtf',
    slackInvited: true,
    craftTags: ['art-and-animation', 'community-management'],
    board: { slackHandle: 'emery.o' },
    createdAt: new Date('2025-03-15'),
    lastLogin: new Date('2026-04-11'),
  },
  {
    email: 'quinn.fairweather@mutualgames.dev',
    name: 'Quinn Fairweather',
    circle: 'practitioner',
    contributionAmount: 30,
    status: 'active',
    avatar: 'disbelieving',
    slackInvited: true,
    craftTags: ['production-management', 'business-development', 'education-and-mentoring'],
    board: { slackHandle: 'quinn.f' },
    createdAt: new Date('2025-04-01'),
    lastLogin: new Date('2026-04-14'),
  },
  {
    email: 'wren.castellano@commonsdev.coop',
    name: 'Wren Castellano',
    circle: 'founder',
    contributionAmount: 30,
    status: 'active',
    avatar: 'sweet',
    slackInvited: true,
    craftTags: ['ux-and-ui-design', 'accessibility', 'narrative-design'],
    board: {},
    createdAt: new Date('2025-05-10'),
    lastLogin: new Date('2026-04-09'),
  },
  {
    email: 'indigo.ramirez@collectivecraft.studio',
    name: 'Indigo Ramirez',
    circle: 'community',
    contributionAmount: 5,
    status: 'active',
    avatar: 'mild',
    slackInvited: true,
    craftTags: ['audio-and-music', 'localization'],
    board: { slackHandle: 'indigo.r' },
    createdAt: new Date('2025-06-01'),
    lastLogin: new Date('2026-04-04'),
  },
]

const TEST_ADMIN_BOARD = {
  slackHandle: 'test-admin',
}

async function seedMembers() {
  try {
    await connectDB()

    // Clear existing members (except test admin)
    await Member.deleteMany({ email: { $ne: 'test-admin@ghostguild.dev' } })
    console.log('Cleared existing members (kept test admin)')

    // Update test admin with slack handle + craft tags
    const adminUpdate = await Member.findOneAndUpdate(
      { email: 'test-admin@ghostguild.dev' },
      {
        $set: {
          board: TEST_ADMIN_BOARD,
          craftTags: ['game-design', 'programming', 'production-management'],
        },
      },
    )
    if (adminUpdate) {
      console.log('Updated test admin with board + craft tags')
    } else {
      console.log('Test admin not found — run /api/dev/test-login first to create it')
    }

    // Insert sample members
    await Member.insertMany(sampleMembers)
    console.log(`Added ${sampleMembers.length} sample members`)

    // Verify
    const count = await Member.countDocuments()
    console.log(`\nTotal members in database: ${count}`)

    const circleBreakdown = await Member.aggregate([
      { $group: { _id: '$circle', count: { $sum: 1 } } },
      { $sort: { _id: 1 } },
    ])
    console.log('\nBreakdown by circle:')
    circleBreakdown.forEach((c) => console.log(`  ${c._id}: ${c.count}`))

    const withSlack = await Member.countDocuments({ 'board.slackHandle': { $exists: true, $ne: null } })
    console.log(`\nMembers with slack handles: ${withSlack}`)

    process.exit(0)
  } catch (error) {
    console.error('Error seeding members:', error)
    process.exit(1)
  }
}

seedMembers()
