import { NextResponse } from 'next/server';
import { db } from '@/lib/db';
import { hashPassword } from '@/lib/auth-utils';

const CATEGORIES = [
  { name: 'Facebook Marketplace Fraud', slug: 'facebook-marketplace-fraud', description: 'Scams through Facebook Marketplace and buy/sell groups', icon: 'ShoppingCart', color: '#1877F2', order: 1 },
  { name: 'Fake Online Shop', slug: 'fake-online-shop', description: 'Fraudulent e-commerce websites and online shops', icon: 'Store', color: '#E74C3C', order: 2 },
  { name: 'Advance Payment Scam', slug: 'advance-payment-scam', description: 'Scams requiring upfront payment before delivering goods/services', icon: 'CreditCard', color: '#F39C12', order: 3 },
  { name: 'Fake Job Circular', slug: 'fake-job-circular', description: 'Fraudulent job postings and employment scams', icon: 'Briefcase', color: '#9B59B6', order: 4 },
  { name: 'Courier Scam', slug: 'courier-scam', description: 'Delivery and courier service related scams', icon: 'Package', color: '#1ABC9C', order: 5 },
  { name: 'Investment Scam', slug: 'investment-scam', description: 'Fraudulent investment opportunities and Ponzi schemes', icon: 'TrendingUp', color: '#E67E22', order: 6 },
  { name: 'Mobile/Laptop Scam', slug: 'mobile-laptop-scam', description: 'Scams involving mobile phones and laptop sales', icon: 'Smartphone', color: '#3498DB', order: 7 },
  { name: 'Rental Scam', slug: 'rental-scam', description: 'Fraudulent house and apartment rental listings', icon: 'Home', color: '#2ECC71', order: 8 },
  { name: 'Valorant ID Buy/Sell Scam', slug: 'valorant-id-scam', description: 'Scams involving buying and selling of Valorant game accounts and IDs', icon: 'Gamepad2', color: '#FF4654', order: 9 },
  { name: 'Game Reseller Scam', slug: 'game-reseller-scam', description: 'Scams by fraudulent game key/cd-key resellers and unauthorized game code sellers', icon: 'Disc', color: '#9146FF', order: 10 },
  { name: 'Online Product Scam', slug: 'online-product-scam', description: 'Fake subscriptions for Canva, CapCut, Netflix, and other premium digital services', icon: 'Crown', color: '#E91E63', order: 11 },
  { name: 'Others', slug: 'others', description: 'Other types of scams not categorized above', icon: 'AlertTriangle', color: '#95A5A6', order: 12 },
];

export async function POST() {
  try {
    // Create categories
    for (const cat of CATEGORIES) {
      const existing = await db.scamCategory.findUnique({
        where: { slug: cat.slug },
      });
      if (!existing) {
        await db.scamCategory.create({ data: cat });
      }
    }

    // Create admin user
    const adminEmail = 'admin@scamcheckerbd.com';
    const existingAdmin = await db.user.findUnique({ where: { email: adminEmail } });
    if (!existingAdmin) {
      const hashedPassword = await hashPassword('admin123');
      await db.user.create({
        data: {
          email: adminEmail,
          name: 'Admin',
          password: hashedPassword,
          role: 'admin',
          emailVerified: true,
        },
      });
    }

    // Count what was created
    const totalReports = await db.scamReport.count();
    const totalUsers = await db.user.count();
    const totalCategories = await db.scamCategory.count();

    return NextResponse.json(
      {
        success: true,
        data: {
          message: 'Database seeded successfully (categories and admin user only)',
          stats: {
            categories: totalCategories,
            users: totalUsers,
            reports: totalReports,
          },
        },
      },
      { status: 201 }
    );
  } catch (error) {
    console.error('Seed error:', error);
    return NextResponse.json(
      { success: false, error: 'Internal server error: ' + (error instanceof Error ? error.message : String(error)) },
      { status: 500 }
    );
  }
}
