import { BaseSeeder } from '@adonisjs/lucid/seeders'
import db from '@adonisjs/lucid/services/db'

export default class DrinkAllYouCanSeeder extends BaseSeeder {
  static environment = ['development', 'testing']

  public async run() {
    // カテゴリーIDを取得
    const categories = await db.from('categories').select('id', 'name', 'type', 'parent_id')

    // カテゴリー名からIDを取得する関数
    const getCategoryId = (name: string, type: string, parentName?: string) => {
      if (type === 'large') {
        return categories.find(c => c.name === name && c.type === 'large')?.id
      } else if (type === 'medium') {
        const parentId = parentName ? getCategoryId(parentName, 'large') : null
        return categories.find(c => c.name === name && c.type === 'medium' && c.parent_id === parentId)?.id
      } else if (type === 'small') {
        const parentId = parentName ? getCategoryId(parentName, 'medium') : null
        return categories.find(c => c.name === name && c.type === 'small' && c.parent_id === parentId)?.id
      }
      return null
    }

    // 飲み放題カテゴリーを作成
    await db.table('products').insert({
      name: '飲み放題',
      description: '時間制限内で飲み放題のメニュー',
      price: 0,
      max_order_quantity: 1,
      large_category_id: getCategoryId('飲み放題', 'large'),
      requires_age_verification: true,
      is_visible: true,
      sold_out_today: false,
      display_start_time: '10:00',
      display_end_time: '22:00',
      created_at: new Date(),
      updated_at: new Date()
    })

    // ビール飲み放題0円を作成
    await db.table('products').insert({
      name: 'ビール飲み放題',
      description: '時間制限内でビール飲み放題（0円）',
      price: 0,
      max_order_quantity: 1,
      large_category_id: getCategoryId('飲み放題', 'large'),
      medium_category_id: getCategoryId('ビール', 'medium', '飲み放題'),
      requires_age_verification: true,
      is_visible: true,
      sold_out_today: false,
      display_start_time: '10:00',
      display_end_time: '22:00',
      created_at: new Date(),
      updated_at: new Date()
    })

    // ハイボール飲み放題0円を作成
    await db.table('products').insert({
      name: 'ハイボール飲み放題',
      description: '時間制限内でハイボール飲み放題（0円）',
      price: 0,
      max_order_quantity: 1,
      large_category_id: getCategoryId('飲み放題', 'large'),
      medium_category_id: getCategoryId('ハイボール', 'medium', '飲み放題'),
      requires_age_verification: true,
      is_visible: true,
      sold_out_today: false,
      display_start_time: '10:00',
      display_end_time: '22:00',
      created_at: new Date(),
      updated_at: new Date()
    })

    // 作成された飲み放題商品のIDを取得
    const drinkProducts = await db.from('products')
      .whereIn('name', ['飲み放題', 'ビール飲み放題', 'ハイボール飲み放題'])
      .select('id')

    // すべての店舗に飲み放題商品を関連付け
    const storeIds = [1, 2, 3, 4] // シーダーで作成された店舗ID
    
    for (const product of drinkProducts) {
      for (const storeId of storeIds) {
        await db.table('product_store').insert({
          product_id: product.id,
          store_id: storeId,
          is_visible: true,
          sort_order: 0,
          created_at: new Date(),
          updated_at: new Date()
        })
        console.log(`飲み放題商品ID ${product.id} を店舗ID ${storeId}に追加しました`)
      }
    }

    console.log('Drink all-you-can menu created:')
    console.log('- 飲み放題カテゴリー')
    console.log('- ビール飲み放題（0円）')
    console.log('- ハイボール飲み放題（0円）')
  }
}
