import { db, pool } from "./index";
import {
  users,
  familyGroups,
  familyGroupMembers,
  subscriptions,
  watchlistItems,
  exposures,
  alerts,
  blogPosts,
  propertyWatchlistItems,
  infoBrokers,
  removalRequests,
} from "./schema";

const ALICE_ID = "a0000000-0000-0000-0000-000000000001";
const BOB_ID = "a0000000-0000-0000-0000-000000000002";
const CAROL_ID = "a0000000-0000-0000-0000-000000000003";
const FAMILY_GROUP_ID = "a0000000-0000-0000-0000-000000000010";
const ALICE_SUB_ID = "a0000000-0000-0000-0000-000000000020";
const BOB_SUB_ID = "a0000000-0000-0000-0000-000000000021";
const WATCH_EMAIL_ALICE = "a0000000-0000-0000-0000-000000000030";
const WATCH_PHONE_ALICE = "a0000000-0000-0000-0000-000000000031";
const WATCH_EMAIL_BOB = "a0000000-0000-0000-0000-000000000032";
const WATCH_SSN_BOB = "a0000000-0000-0000-0000-000000000033";
const WATCH_DOMAIN_BOB = "a0000000-0000-0000-0000-000000000034";
const EXPOSURE_1 = "a0000000-0000-0000-0000-000000000040";
const EXPOSURE_2 = "a0000000-0000-0000-0000-000000000041";
const EXPOSURE_3 = "a0000000-0000-0000-0000-000000000042";
const PROPERTY_1 = "a0000000-0000-0000-0000-000000000050";
const PROPERTY_2 = "a0000000-0000-0000-0000-000000000051";
const BROKER_ID = "a0000000-0000-0000-0000-000000000060";
const REMOVAL_REQUEST_ID = "a0000000-0000-0000-0000-000000000070";

export async function seed() {
  console.log("[seed] Seeding database...");

  await db.insert(users).values([
    { id: ALICE_ID, email: "alice@example.com", name: "Alice Smith", role: "family_admin", emailVerified: new Date() },
    { id: BOB_ID, email: "bob@example.com", name: "Bob Smith", role: "user", emailVerified: new Date() },
    { id: CAROL_ID, email: "carol@example.com", name: "Carol Johnson", role: "user" },
  ]).onConflictDoNothing();
  console.log("[seed] Users created");

  await db.insert(familyGroups).values([
    { id: FAMILY_GROUP_ID, name: "Smith Family", ownerId: ALICE_ID },
  ]).onConflictDoNothing();
  console.log("[seed] Family group created");

  await db.insert(familyGroupMembers).values([
    { groupId: FAMILY_GROUP_ID, userId: ALICE_ID, role: "owner" },
    { groupId: FAMILY_GROUP_ID, userId: BOB_ID, role: "member" },
  ]).onConflictDoNothing();
  console.log("[seed] Family group members created");

  const now = new Date();
  const periodStart = new Date(now.getFullYear(), now.getMonth(), 1);
  const periodEnd = new Date(now.getFullYear(), now.getMonth() + 1, 0);

  await db.insert(subscriptions).values([
    {
      id: ALICE_SUB_ID,
      userId: ALICE_ID,
      familyGroupId: FAMILY_GROUP_ID,
      tier: "premium",
      status: "active",
      currentPeriodStart: periodStart,
      currentPeriodEnd: periodEnd,
    },
    {
      id: BOB_SUB_ID,
      userId: BOB_ID,
      tier: "basic",
      status: "active",
      currentPeriodStart: periodStart,
      currentPeriodEnd: periodEnd,
    },
  ]).onConflictDoNothing();
  console.log("[seed] Subscriptions created");

  await db.insert(watchlistItems).values([
    { id: WATCH_EMAIL_ALICE, subscriptionId: ALICE_SUB_ID, type: "email", value: "alice@example.com", hash: "hash-alice-email" },
    { id: WATCH_PHONE_ALICE, subscriptionId: ALICE_SUB_ID, type: "phoneNumber", value: "+15551234567", hash: "hash-alice-phone" },
    { id: WATCH_EMAIL_BOB, subscriptionId: BOB_SUB_ID, type: "email", value: "bob@example.com", hash: "hash-bob-email" },
    { id: WATCH_SSN_BOB, subscriptionId: BOB_SUB_ID, type: "ssn", value: "XXX-XX-1234", hash: "hash-bob-ssn" },
    { id: WATCH_DOMAIN_BOB, subscriptionId: BOB_SUB_ID, type: "domain", value: "bobsmith.example.com", hash: "hash-bob-domain" },
  ]).onConflictDoNothing();
  console.log("[seed] Watchlist items created");

  const pastDate = (daysAgo: number) => new Date(now.getTime() - daysAgo * 24 * 60 * 60 * 1000);

  await db.insert(exposures).values([
    {
      id: EXPOSURE_1,
      subscriptionId: ALICE_SUB_ID,
      watchlistItemId: WATCH_EMAIL_ALICE,
      source: "hibp",
      dataType: "email",
      identifier: "alice@example.com",
      identifierHash: "hash-alice-email",
      severity: "critical",
      metadata: { breach: "Adobe 2013", dataClasses: ["email", "password_hint", "password"] },
      isFirstTime: true,
      detectedAt: pastDate(30),
    },
    {
      id: EXPOSURE_2,
      subscriptionId: ALICE_SUB_ID,
      watchlistItemId: WATCH_PHONE_ALICE,
      source: "darkWebForum",
      dataType: "phoneNumber",
      identifier: "+15551234567",
      identifierHash: "hash-alice-phone",
      severity: "warning",
      metadata: { forum: "Nulled.to", postedAt: "2025-04-01" },
      detectedAt: pastDate(14),
    },
    {
      id: EXPOSURE_3,
      subscriptionId: BOB_SUB_ID,
      watchlistItemId: WATCH_EMAIL_BOB,
      source: "hibp",
      dataType: "email",
      identifier: "bob@example.com",
      identifierHash: "hash-bob-email",
      severity: "info",
      metadata: { breach: "LinkedIn 2021", dataClasses: ["email", "name"] },
      detectedAt: pastDate(7),
    },
  ]).onConflictDoNothing();
  console.log("[seed] Exposures created");

  await db.insert(alerts).values([
    {
      subscriptionId: ALICE_SUB_ID,
      userId: ALICE_ID,
      exposureId: EXPOSURE_1,
      type: "exposure_detected",
      title: "Critical breach detected: Adobe 2013",
      message: "Your email alice@example.com was found in the Adobe 2013 data breach.",
      severity: "critical",
      channel: ["email", "push"],
      createdAt: pastDate(30),
    },
    {
      subscriptionId: ALICE_SUB_ID,
      userId: ALICE_ID,
      exposureId: EXPOSURE_2,
      type: "exposure_detected",
      title: "Phone number found on dark web forum",
      message: "Your phone number was posted on Nulled.to.",
      severity: "warning",
      channel: ["push"],
      createdAt: pastDate(14),
    },
    {
      subscriptionId: ALICE_SUB_ID,
      userId: ALICE_ID,
      type: "scan_complete",
      title: "Dark web scan completed",
      message: "Weekly dark web scan found 2 new exposures.",
      severity: "info",
      channel: ["email"],
      createdAt: pastDate(1),
    },
    {
      subscriptionId: BOB_SUB_ID,
      userId: BOB_ID,
      exposureId: EXPOSURE_3,
      type: "exposure_detected",
      title: "Breach detected: LinkedIn 2021",
      message: "Your email bob@example.com was found in the LinkedIn 2021 breach.",
      severity: "info",
      channel: ["email", "push"],
      createdAt: pastDate(7),
    },
    {
      subscriptionId: BOB_SUB_ID,
      userId: BOB_ID,
      type: "scan_complete",
      title: "Monthly scan complete",
      message: "Your monthly dark web scan has finished. 1 exposure found.",
      severity: "info",
      channel: ["email"],
      createdAt: pastDate(0),
    },
    {
      subscriptionId: ALICE_SUB_ID,
      userId: ALICE_ID,
      type: "exposure_resolved",
      title: "Exposure resolved: Adobe 2013",
      message: "The Adobe 2013 breach exposure has been marked as resolved.",
      severity: "info",
      channel: ["push"],
      createdAt: pastDate(0),
    },
  ]).onConflictDoNothing();
  console.log("[seed] Alerts created");

  await db.insert(blogPosts).values([
    {
      slug: "what-is-dark-web-monitoring",
      title: "What Is Dark Web Monitoring and Why You Need It",
      excerpt: "Learn how dark web monitoring protects your personal information from cybercriminals.",
      content: "The dark web is a hidden part of the internet where cybercriminals buy and sell stolen data. ShieldAI helps you monitor your digital footprint...",
      authorName: "ShieldAI Team",
      tags: ["dark-web", "monitoring", "security"],
      published: true,
      publishedAt: pastDate(60),
      viewCount: 1250,
    },
    {
      slug: "protect-your-family-online",
      title: "5 Tips to Protect Your Family's Online Privacy",
      excerpt: "Simple steps to keep your family's personal information safe from data brokers.",
      content: "In today's digital age, protecting your family's privacy is more important than ever. Here are five actionable tips...",
      authorName: "ShieldAI Team",
      tags: ["family", "privacy", "tips"],
      published: true,
      publishedAt: pastDate(30),
      viewCount: 820,
    },
    {
      slug: "understanding-data-brokers",
      title: "Understanding Data Brokers: Who Has Your Information?",
      excerpt: "A comprehensive guide to data brokers and how to opt out of their databases.",
      content: "Data brokers collect and sell personal information. This guide explains how they operate and how you can remove your data...",
      authorName: "ShieldAI Team",
      tags: ["data-brokers", "privacy", "opt-out"],
      published: true,
      publishedAt: pastDate(7),
      viewCount: 340,
    },
  ]).onConflictDoNothing();
  console.log("[seed] Blog posts created");

  await db.insert(propertyWatchlistItems).values([
    {
      id: PROPERTY_1,
      subscriptionId: ALICE_SUB_ID,
      address: "123 Main St, Springfield, IL 62701",
      parcelId: "SPR-1234-5678",
      ownerName: "Alice Smith",
      streetAddress: "123 Main St",
      city: "Springfield",
      state: "IL",
      zipCode: "62701",
      latitude: 39.7817,
      longitude: -89.6501,
    },
    {
      id: PROPERTY_2,
      subscriptionId: ALICE_SUB_ID,
      address: "456 Oak Ave, Springfield, IL 62704",
      parcelId: "SPR-8765-4321",
      ownerName: "Alice Smith",
      streetAddress: "456 Oak Ave",
      city: "Springfield",
      state: "IL",
      zipCode: "62704",
      latitude: 39.7654,
      longitude: -89.6321,
    },
  ]).onConflictDoNothing();
  console.log("[seed] Property watchlist items created");

  await db.insert(infoBrokers).values([
    {
      id: BROKER_ID,
      name: "PeopleFind Pro",
      domain: "peoplefindpro.example.com",
      category: "PEOPLE_SEARCH",
      removalMethod: "MANUAL_FORM",
      removalUrl: "https://peoplefindpro.example.com/opt-out",
      requiresAccount: true,
      requiresVerification: true,
      estimatedDays: 30,
    },
  ]).onConflictDoNothing();
  console.log("[seed] Info brokers created");

  await db.insert(removalRequests).values([
    {
      id: REMOVAL_REQUEST_ID,
      subscriptionId: ALICE_SUB_ID,
      brokerId: BROKER_ID,
      status: "PENDING",
      personalInfo: { name: "Alice Smith", email: "alice@example.com", phone: "+15551234567" },
      method: "MANUAL_FORM",
      attempts: 2,
      nextRetryAt: new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000),
      notes: "Previous attempts failed due to CAPTCHA",
    },
  ]).onConflictDoNothing();
  console.log("[seed] Removal requests created");

  console.log("[seed] Seeding complete");
}

const isMainModule = process.argv[1]?.includes("seed");
if (isMainModule) {
  seed()
    .then(() => pool.end())
    .catch((error) => {
      console.error("[seed] Error:", error);
      process.exit(1);
    });
}
