import { db } from '@/db';
import {
  clients,
  projects,
  phases,
  tasks,
  deliverables,
  payments,
  documents,
  notes,
} from '@/db/schema';
import { nanoid } from 'nanoid';

async function seed() {
  console.log('🌱 Seeding database...\n');

  try {
    const clientToken = nanoid();
    const [client] = await db
      .insert(clients)
      .values({
        name: 'Test Client Inc.',
        brand_name: 'TestBrand',
        brief:
          'A comprehensive personal branding overhaul, positioning as a premium consultancy in the digital transformation space.',
        token: clientToken,
        accepted_total: '5000.00',
      })
      .returning();

    console.log(`✓ Client created: ${client.name} (ID: ${client.id})`);

    // Create a default project for the client (all work items are project-scoped)
    const [project] = await db
      .insert(projects)
      .values({
        client_id: client.id,
        name: client.brand_name,
        accepted_total: '5000.00',
      })
      .returning();

    console.log(`✓ Project created: ${project.name} (ID: ${project.id})`);

    const [phase1, phase2, phase3] = await db
      .insert(phases)
      .values([
        { project_id: project.id, title: 'Discovery & Strategy', sort_order: 1, status: 'done' },
        { project_id: project.id, title: 'Design & Messaging', sort_order: 2, status: 'active' },
        { project_id: project.id, title: 'Implementation & Launch', sort_order: 3, status: 'upcoming' },
      ])
      .returning();

    console.log('✓ Phases created (3 total)');

    const [task1, task2, task3, task4, task5, task6] = await db
      .insert(tasks)
      .values([
        { phase_id: phase1.id, title: 'Stakeholder interviews', description: 'In-depth conversations with leadership team', sort_order: 1, status: 'done' },
        { phase_id: phase1.id, title: 'Competitive analysis', description: 'Research top 10 competitors in the space', sort_order: 2, status: 'done' },
        { phase_id: phase2.id, title: 'Brand positioning document', description: 'Write and refine the core positioning statement', sort_order: 1, status: 'in_progress' },
        { phase_id: phase2.id, title: 'Visual identity design', description: 'Logo, color palette, typography', sort_order: 2, status: 'in_progress' },
        { phase_id: phase3.id, title: 'Website build & launch', description: 'Design and develop new company website', sort_order: 1, status: 'todo' },
        { phase_id: phase3.id, title: 'Social media rollout', description: 'Launch branded social media accounts', sort_order: 2, status: 'todo' },
      ])
      .returning();

    console.log('✓ Tasks created (6 total)');

    await db
      .insert(deliverables)
      .values([
        { task_id: task1.id, title: 'Interview notes & synthesis', url: 'https://docs.google.com/document/d/1example', status: 'approved', approved_at: new Date('2026-04-15') },
        { task_id: task2.id, title: 'Competitive landscape report', url: 'https://docs.google.com/presentation/d/1example', status: 'approved', approved_at: new Date('2026-04-20') },
        { task_id: task3.id, title: 'Brand positioning document (draft)', url: 'https://docs.google.com/document/d/2example', status: 'submitted', approved_at: null },
        { task_id: task4.id, title: 'Logo concepts (3 variations)', url: 'https://www.figma.com/file/example', status: 'pending', approved_at: null },
      ]);

    console.log('✓ Deliverables created (4 total)');

    await db
      .insert(payments)
      .values([
        { project_id: project.id, label: 'Acconto 50%', amount: '2500.00', status: 'saldato', paid_at: new Date('2026-04-01') },
        { project_id: project.id, label: 'Saldo 50%', amount: '2500.00', status: 'inviata', paid_at: null },
      ]);

    console.log('✓ Payments created (2 total)');

    await db
      .insert(documents)
      .values([
        { project_id: project.id, label: 'Brand Guidelines PDF', url: 'https://example.com/brand-guidelines.pdf' },
        { project_id: project.id, label: 'Design Mockups Figma', url: 'https://www.figma.com/file/example' },
      ]);

    console.log('✓ Documents created (2 total)');

    await db
      .insert(notes)
      .values([
        { project_id: project.id, body: 'Initial strategy session completed. Key insight: positioning needs to emphasize tech expertise and creative thinking balance.', created_at: new Date('2026-04-10') },
        { project_id: project.id, body: 'Phase 1 approved. Moving forward with design phase. Stakeholders excited about direction.', created_at: new Date('2026-04-22') },
      ]);

    console.log('✓ Notes created (2 total)');

    console.log('\n✨ Seed complete!\n');
    console.log('📎 Shareable client link:');
    console.log(`   http://localhost:3000/client/${clientToken}\n`);
    console.log('This link is unique and secret. Send it to the client via Slack or email.\n');

    process.exit(0);
  } catch (error) {
    console.error('❌ Seed failed:', error);
    process.exit(1);
  }
}

seed();