import { storage } from "./storage";
import { scrypt, randomBytes } from "crypto";
import { promisify } from "util";

const scryptAsync = promisify(scrypt);

async function hashPassword(password: string): Promise<string> {
  const salt = randomBytes(16).toString("hex");
  const buf = (await scryptAsync(password, salt, 64)) as Buffer;
  return `${buf.toString("hex")}.${salt}`;
}

export async function seedDummyData() {
  try {
    // Create admin user
    const adminUser = await storage.createUser({
      username: "admin",
      password: await hashPassword("admin123"),
      name: "Admin User",
      email: "admin@example.com",
      role: "admin",
      profileImage: "https://placehold.co/400x400?text=Admin"
    });

    // Create organizer user
    const organizerUser = await storage.createUser({
      username: "organizer",
      password: await hashPassword("organizer123"),
      name: "Event Organizer",
      email: "organizer@example.com",
      role: "organizer",
      profileImage: "https://placehold.co/400x400?text=Organizer"
    });

    // Create events
    const event = await storage.createEvent({
      eventName: "National Development Summit",
      eventNameAr: "قمة التنمية الوطنية",
      eventDescription: "Annual summit focusing on national development strategies and sustainable growth",
      eventDescriptionAr: "قمة سنوية تركز على استراتيجيات التنمية الوطنية والنمو المستدام",
      startDate: "2025-03-15",
      endDate: "2025-03-17",
      location: "Dubai International Convention Centre",
      locationAr: "مركز دبي الدولي للمؤتمرات",
      eventLogo: "https://placehold.co/400x400?text=National+Development+Summit"
    });

    // Create multiple speakers
    const speakers = [
      {
        name: "Dr. Sarah Mitchell",
        nameAr: "د. سارة ميتشل",
        bio: "Development Economics Expert and Policy Advisor",
        bioAr: "خبيرة في اقتصاديات التنمية ومستشارة سياسات",
        imageUrl: "https://placehold.co/400x400?text=Dr.+Sarah+Mitchell",
        title: "Dr.",
        titleAr: "د.",
        organization: "Global Development Institute",
        organizationAr: "معهد التنمية العالمي",
        position: "Executive Director",
        positionAr: "المدير التنفيذي"
      },
      {
        name: "Prof. Ahmed Hassan",
        nameAr: "أ.د. أحمد حسن",
        bio: "Sustainable Infrastructure Development Specialist",
        bioAr: "متخصص في تطوير البنية التحتية المستدامة",
        imageUrl: "https://placehold.co/400x400?text=Prof.+Hassan",
        title: "Prof.",
        titleAr: "أ.د.",
        organization: "Urban Planning Institute",
        organizationAr: "معهد التخطيط العمراني",
        position: "Director of Research",
        positionAr: "مدير الأبحاث"
      },
      {
        name: "Dr. Maria Rodriguez",
        nameAr: "د. ماريا رودريغيز",
        bio: "International Development and Policy Expert",
        bioAr: "خبيرة في التنمية الدولية والسياسات",
        imageUrl: "https://placehold.co/400x400?text=Dr.+Rodriguez",
        title: "Dr.",
        titleAr: "د.",
        organization: "International Development Forum",
        organizationAr: "منتدى التنمية الدولي",
        position: "Senior Advisor",
        positionAr: "مستشار أول"
      }
    ];

    const createdSpeakers = await Promise.all(
      speakers.map(speaker => storage.createSpeaker(speaker))
    );

    // Create multiple sessions
    const sessions = [
      {
        eventId: event.id,
        sessionTitle: "Sustainable Development Goals 2030",
        sessionTitleAr: "أهداف التنمية المستدامة 2030",
        sessionDescription: "Discussing progress and strategies towards achieving SDGs",
        sessionDescriptionAr: "مناقشة التقدم والاستراتيجيات نحو تحقيق أهداف التنمية المستدامة",
        venue: "Main Conference Hall",
        venueAr: "القاعة الرئيسية للمؤتمرات",
        sessionDate: "2025-03-15",
        sessionTime: "10:00",
        liveStreamUrl: "https://youtube.com/live/example1"
      },
      {
        eventId: event.id,
        sessionTitle: "Urban Development and Smart Cities",
        sessionTitleAr: "التطوير الحضري والمدن الذكية",
        sessionDescription: "Future of urban planning and smart city initiatives",
        sessionDescriptionAr: "مستقبل التخطيط الحضري ومبادرات المدن الذكية",
        venue: "Innovation Hall",
        venueAr: "قاعة الابتكار",
        sessionDate: "2025-03-16",
        sessionTime: "11:30",
        liveStreamUrl: "https://youtube.com/live/example2"
      },
      {
        eventId: event.id,
        sessionTitle: "International Cooperation for Development",
        sessionTitleAr: "التعاون الدولي للتنمية",
        sessionDescription: "Strengthening international partnerships for development",
        sessionDescriptionAr: "تعزيز الشراكات الدولية من أجل التنمية",
        venue: "Partnership Hall",
        venueAr: "قاعة الشراكة",
        sessionDate: "2025-03-17",
        sessionTime: "09:30",
        liveStreamUrl: "https://youtube.com/live/example3"
      }
    ];

    const createdSessions = await Promise.all(
      sessions.map(session => storage.createSession(session))
    );

    // Assign speakers to sessions
    await Promise.all([
      storage.assignSpeakerToSession(createdSessions[0].id, createdSpeakers[0].id),
      storage.assignSpeakerToSession(createdSessions[1].id, createdSpeakers[1].id),
      storage.assignSpeakerToSession(createdSessions[2].id, createdSpeakers[2].id)
    ]);

    // Create news articles
    await storage.createNews({
      title: "National Development Summit 2025 Announced",
      titleAr: "الإعلان عن قمة التنمية الوطنية 2025",
      content: "Join us for the premier event focused on national development and sustainable growth strategies",
      contentAr: "انضم إلينا في الحدث الرئيسي الذي يركز على استراتيجيات التنمية الوطنية والنمو المستدام",
      image: "https://placehold.co/800x400?text=National+Development+Summit+Announcement",
      authorId: adminUser.id,
      publishDate: new Date(),
      isPublished: true
    });

    // Create media items
    await storage.createMedia({
      type: "image",
      title: "Opening Ceremony Highlights",
      titleAr: "أبرز لحظات حفل الافتتاح",
      description: "National Development Summit 2024 Opening Ceremony",
      descriptionAr: "حفل افتتاح قمة التنمية الوطنية 2024",
      url: "https://placehold.co/800x400?text=Opening+Ceremony",
      thumbnail: "https://placehold.co/400x200?text=Opening+Ceremony",
      uploaderId: adminUser.id,
      eventId: event.id
    });

    await storage.createMedia({
      type: "video",
      title: "Keynote: Future of Sustainable Development",
      titleAr: "الكلمة الرئيسية: مستقبل التنمية المستدامة",
      description: "Opening keynote speech on sustainable development strategies",
      descriptionAr: "الكلمة الافتتاحية حول استراتيجيات التنمية المستدامة",
      url: "https://www.youtube.com/embed/example",
      thumbnail: "https://placehold.co/400x200?text=Keynote",
      uploaderId: adminUser.id,
      eventId: event.id
    });

    console.log("✅ Dummy data seeded successfully");
  } catch (error) {
    console.error("Error seeding data:", error);
  }
}