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

const prisma = new PrismaClient()

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

  // Clear existing data
  await prisma.activityLog.deleteMany()
  await prisma.funnelMetric.deleteMany()
  await prisma.contentSession.deleteMany()
  await prisma.launchTask.deleteMany()
  await prisma.subscriptionTier.deleteMany()
  await prisma.user.deleteMany()

  // Create users
  const hash = await bcrypt.hash('oskvlabs2026', 10)
  const users = await Promise.all([
    prisma.user.create({ data: { username: 'quo', password: hash, name: 'Olly (Quo)', role: 'member' } }),
    prisma.user.create({ data: { username: 'kevin', password: hash, name: 'Kevin', role: 'member' } }),
    prisma.user.create({ data: { username: 'jake', password: hash, name: 'Jake', role: 'admin' } }),
    prisma.user.create({ data: { username: 'buba', password: hash, name: 'Buba (AI Coach)', role: 'admin' } }),
  ])
  console.log(`✅ Created ${users.length} users`)

  // Create launch tasks
  const tasks = await Promise.all([
    // Phase 1 — Seed Content
    prisma.launchTask.create({ data: { title: 'Post process fragments on @quowavy', description: 'Share BTS/process clips — no links, no prices', phase: 1, sortOrder: 1, status: 'in_progress' } }),
    prisma.launchTask.create({ data: { title: 'Post process fragments on @kevinthevp', description: 'Share BTS/process clips — no links, no prices', phase: 1, sortOrder: 2, status: 'in_progress' } }),
    prisma.launchTask.create({ data: { title: 'Build curiosity without revealing the product', description: 'Let the audience ask questions organically', phase: 1, sortOrder: 3, status: 'todo' } }),
    prisma.launchTask.create({ data: { title: 'Collect 3-5 strong process clips each', description: 'Have a backlog of content ready before Phase 2', phase: 1, sortOrder: 4, status: 'todo' } }),

    // Phase 2 — Build Presence
    prisma.launchTask.create({ data: { title: 'Launch @oskv.labs Instagram', description: 'Start posting branded content from the OSKV Labs account', phase: 2, sortOrder: 1, status: 'todo' } }),
    prisma.launchTask.create({ data: { title: 'Mention Discord verbally in content', description: 'Talk about the community naturally in stories/videos', phase: 2, sortOrder: 2, status: 'todo' } }),
    prisma.launchTask.create({ data: { title: 'Create OSKV Labs brand assets', description: 'Logo, color palette, templates for IG content', phase: 2, sortOrder: 3, status: 'todo' } }),
    prisma.launchTask.create({ data: { title: 'Set up Discord server structure', description: 'Channels, roles, permissions, welcome flow', phase: 2, sortOrder: 4, status: 'todo' } }),

    // Phase 3 — Launch
    prisma.launchTask.create({ data: { title: 'Open free Lab Floor (Tier 0)', description: 'Let people in for free to experience the community', phase: 3, sortOrder: 1, status: 'todo' } }),
    prisma.launchTask.create({ data: { title: 'Announce first cohort presentation', description: 'Create buzz around the first live session', phase: 3, sortOrder: 2, status: 'todo' } }),
    prisma.launchTask.create({ data: { title: 'Open paid tiers', description: 'Launch Observers, Operators, Inner Circle, Apprenticeship', phase: 3, sortOrder: 3, status: 'todo' } }),
    prisma.launchTask.create({ data: { title: 'Set up payment processing', description: 'Connect Stripe/payment links for all paid tiers', phase: 3, sortOrder: 4, status: 'todo' } }),

    // Current priority tasks
    prisma.launchTask.create({ data: { title: '📌 Finish Google Doc: Tier descriptions', description: 'What people get per tier — deliverables, access, pricing', phase: 1, sortOrder: 0, status: 'in_progress', assignee: 'quo' } }),
    prisma.launchTask.create({ data: { title: '📌 Collect payment links for all tiers', description: 'Stripe links or equivalent for each paid tier', phase: 1, sortOrder: 0, status: 'todo', assignee: 'kevin' } }),
  ])
  console.log(`✅ Created ${tasks.length} launch tasks`)

  // Create subscription tiers
  const tiers = await Promise.all([
    prisma.subscriptionTier.create({ data: { tierNumber: 0, name: 'The Lab Floor', price: 0, description: 'Free community access — the front door', paymentLinkStatus: 'collected' } }),
    prisma.subscriptionTier.create({ data: { tierNumber: 1, name: 'Observers', price: 12, description: 'Watch live sessions, access replays, community chat' } }),
    prisma.subscriptionTier.create({ data: { tierNumber: 2, name: 'Operators', price: 35, description: 'Everything in Observers + breakdowns, templates, feedback rounds' } }),
    prisma.subscriptionTier.create({ data: { tierNumber: 3, name: 'Inner Circle', price: 90, description: 'Full access — group calls, direct feedback, inner community' } }),
    prisma.subscriptionTier.create({ data: { tierNumber: 4, name: 'Apprenticeship', price: 300, priceMax: 500, description: '1-on-1 mentorship, project collab, direct coaching', isApplicationOnly: true, hardCap: 5 } }),
  ])
  console.log(`✅ Created ${tiers.length} subscription tiers`)

  // Create content calendar template for next 2 weeks
  const now = new Date()
  const monday = new Date(now)
  monday.setDate(now.getDate() - now.getDay() + 1) // This Monday

  for (let week = 0; week < 2; week++) {
    const weekStart = new Date(monday)
    weekStart.setDate(monday.getDate() + week * 7)

    await Promise.all([
      prisma.contentSession.create({ data: { title: 'Industry Strategy Session', dayOfWeek: 'Monday', topic: 'Freelance strategy & networking', weekStart, status: week === 0 ? 'planned' : 'planned' } }),
      prisma.contentSession.create({ data: { title: 'AI Workflow Deep Dive', dayOfWeek: 'Wednesday', topic: 'AI workflows, prompting, tool stacks', weekStart, status: 'planned' } }),
      prisma.contentSession.create({ data: { title: 'Post-Production Masterclass', dayOfWeek: 'Friday', topic: 'Editing, post-production, career case studies', weekStart, status: 'planned' } }),
    ])
  }
  console.log('✅ Created content calendar (2 weeks)')

  // Seed initial funnel metric
  await prisma.funnelMetric.create({
    data: {
      date: new Date(),
      quowavyFollowers: 0,
      kevinthevpFollowers: 0,
      oskvlabsFollowers: 0,
      discordMembersFree: 0,
      discordMembersPaid: 0,
      conversionRate: 0,
      notes: 'Pre-launch baseline',
    },
  })
  console.log('✅ Created initial funnel metric')

  console.log('🎉 Seeding complete!')
}

main()
  .catch(e => { console.error(e); process.exit(1) })
  .finally(() => prisma.$disconnect())
