// scripts/seed-mock-blogs.ts
import { drizzle } from 'drizzle-orm/neon-serverless';
import { Pool } from '@neondatabase/serverless';
import { blogPosts } from '../shared/schema';
import { eq, and, like } from 'drizzle-orm';
import * as dotenv from 'dotenv';

dotenv.config({ path: '.env.local' });

const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const db = drizzle(pool);

const SITE_ID = 'ten-sura-game.com';

const mockBlogs = [
  {
    siteId: SITE_ID,
    externalId: 'mock-001',
    title: '【まおりゅう】リセマラ当たりランキング最新版',
    slug: 'tensura-reroll-ranking',
    content: `
      <p>まおりゅう（魔王と竜の建国譚）のリセマラランキングです。</p>
      <h2>リセマラの終了基準</h2>
      <p>星5キャラが2体以上出たら終了がおすすめです。</p>
    `,
    excerpt: 'まおりゅうのリセマラ当たりランキングを掲載。最新キャラの評価も随時更新中！',
    category: 'リセマラ・ガチャ情報',
    author: '編集部',
    cardImage: 'https://picsum.photos/seed/tensura-reroll/800/600',
    heroImage: 'https://picsum.photos/seed/tensura-reroll/1200/600',
    publicationDate: new Date('2026-02-17'),
    readingTime: 5,
    wordCount: 850,
    featured: true,
    isActive: true,
    language: 'ja',
    metaTitle: '【まおりゅう】リセマラ当たりランキング最新版',
    metaDescription: 'まおりゅうのリセマラ当たりランキングを掲載。最新キャラの評価も随時更新中！',
  },
  {
    siteId: SITE_ID,
    externalId: 'mock-002',
    title: '【まおりゅう】リムルの評価とおすすめ編成',
    slug: 'tensura-rimuru-evaluation',
    content: `
      <p>リムル＝テンペストの評価と使い方を解説します。</p>
      <h2>リムルの強み</h2>
      <p>圧倒的なサポート能力で味方を強化できます。</p>
    `,
    excerpt: 'リムルの評価とおすすめ編成を紹介します。',
    category: 'キャラクター評価',
    author: '編集部',
    cardImage: 'https://picsum.photos/seed/tensura-rimuru/800/600',
    heroImage: 'https://picsum.photos/seed/tensura-rimuru/1200/600',
    publicationDate: new Date('2026-02-16'),
    readingTime: 4,
    wordCount: 650,
    featured: false,
    isActive: true,
    language: 'ja',
    metaTitle: '【まおりゅう】リムルの評価とおすすめ編成',
    metaDescription: 'リムルの評価とおすすめ編成を紹介します。',
  }
];

async function seedMockBlogs() {
  console.log('🌱 Starting mock blog seeding...\n');

  try {
    const existingMock = await db
      .select()
      .from(blogPosts)
      .where(
        and(
          like(blogPosts.externalId, 'mock-%'),
          eq(blogPosts.siteId, SITE_ID)
        )
      )
      .limit(1);

    if (existingMock.length > 0) {
      console.log('⚠️  Mock data already exists in the database.');
      console.log('⚠️  Please run "npx tsx scripts/delete-mock-data.ts" first to clean up.\n');
      await pool.end();
      process.exit(0);
    }

    console.log('📝 Inserting mock blog posts...\n');

    for (const blog of mockBlogs) {
      const [inserted] = await db
        .insert(blogPosts)
        .values({
          ...blog,
        })
        .returning();

      console.log(`✅ Created: [${blog.externalId}] ${blog.title}`);
    }

    console.log('🎉 MOCK DATA SEEDING COMPLETED!\n');

  } catch (error) {
    console.error('❌ Error seeding mock blogs:', error);
    process.exit(1);
  } finally {
    await pool.end();
  }
}

seedMockBlogs();
