import { db } from "./config/migrations";
import { users } from "./schema/users";
import { projects } from "./schema/projects";
import { scripts } from "./schema/scripts";
import { characters } from "./schema/characters";
import { scenes, sceneCharacters } from "./schema/scenes";
import { waitlistSignups, waitlistEvents } from "./schema/waitlist";

export async function seedDatabase() {
  console.log("Seeding database...");

  // Create admin user
  const admin = await db.insert(users).values({
    email: "admin@frenocorp.com",
    username: "admin",
    fullName: "Admin User",
    role: "admin",
  }).returning();

  if (!admin[0]) throw new Error("Failed to create admin user");

  // Create test project
  const project = await db.insert(projects).values({
    name: "Test Project",
    description: "A test project for development",
    ownerId: admin[0].id,
    isPublic: true,
  }).returning();

  if (!project[0]) throw new Error("Failed to create project");

  // Create test script
  const script = await db.insert(scripts).values({
    projectId: project[0].id,
    title: "Test Screenplay",
    slug: "test-screenplay",
    genre: "Drama",
    logline: "A test screenplay for development purposes",
    status: "draft",
  }).returning();

  if (!script[0]) throw new Error("Failed to create script");

  // Create test character
  const character = await db.insert(characters).values({
    projectId: project[0].id,
    name: "John Doe",
    slug: "john-doe",
    role: "protagonist",
    description: "The main character",
    age: 30,
    gender: "male",
  }).returning();

  if (!character[0]) throw new Error("Failed to create character");

  // Create test scene
  const scene = await db.insert(scenes).values({
    projectId: project[0].id,
    title: "INT. OFFICE - DAY",
    content: "John sits at his desk, contemplating his next move.",
    order: 1,
  }).returning();

  if (!scene[0]) throw new Error("Failed to create scene");

  // Link character to scene
  await db.insert(sceneCharacters).values({
    sceneId: scene[0].id,
    characterId: character[0].id,
    screenTime: 5,
    dialogueLines: 3,
  });

  console.log("Database seeded successfully!");
  console.log({
    user: admin[0],
    project: project[0],
    script: script[0],
    character: character[0],
    scene: scene[0],
  });

  // Seed waitlist signups for Product Hunt launch
  const waitlistData = [
    { email: "writer1@example.com", name: "Jane Screenwriter", source: "organic" },
    { email: "writer2@example.com", name: "John Director", source: "referral" },
    { email: "writer3@example.com", name: "Sarah Producer", source: "twitter" },
    { email: "writer4@example.com", name: "Mike Writer", source: "organic" },
    { email: "writer5@example.com", name: "Emily Creator", source: "referral" },
  ];

  const insertedSignups = await db.insert(waitlistSignups).values(waitlistData).returning();
  console.log(`Seeded ${insertedSignups.length} waitlist signups`);

  // Seed some engagement events
  if (insertedSignups[0]) {
    await db.insert(waitlistEvents).values({
      signupId: insertedSignups[0].id,
      eventType: "email_opened",
      eventData: JSON.stringify({ campaign: "welcome" }),
    });
  }

  return {
    user: admin[0],
    project: project[0],
    script: script[0],
    character: character[0],
    scene: scene[0],
    waitlist: insertedSignups,
  };
}
