import { BaseSeeder } from '@adonisjs/lucid/seeders'
import RestaurantMenuItem from '#models/restaurant_menu_item'
import Category from '#models/category'
import fs from 'node:fs'
import path from 'node:path'
import app from '@adonisjs/core/services/app'

export default class extends BaseSeeder {
  async run() {
    const filePath = path.join(app.makePath(), 'menu.json')
    const content = fs.readFileSync(filePath, 'utf-8')
    const menuItems = JSON.parse(content)

    // Create categories
    const categoriesData = [
      { name: 'Levesek', nameHu: 'Levesek', nameEn: 'Soups', slug: 'levesek' },
      { name: 'Főzelékek', nameHu: 'Főzelékek', nameEn: 'Vegetable Stews', slug: 'fozelekek' },
      { name: 'Készételek', nameHu: 'Készételek', nameEn: 'Ready Meals', slug: 'keszetelek' },
      {
        name: 'Frissensültek',
        nameHu: 'Frissensültek',
        nameEn: 'Fresh Roasts',
        slug: 'frissensultek',
      },
      { name: 'Tészták', nameHu: 'Tészták', nameEn: 'Pasta', slug: 'tesztak' },
      { name: 'Desszertek', nameHu: 'Desszertek', nameEn: 'Desserts', slug: 'desszertek' },
      { name: 'Egyéb', nameHu: 'Egyéb', nameEn: 'Other', slug: 'egyeb' },
      { name: 'Köretek', nameHu: 'Köretek', nameEn: 'Sides', slug: 'koretek' },
      { name: 'Savanyúság', nameHu: 'Savanyúság', nameEn: 'Pickles', slug: 'savanyusag' },
    ]

    const categories: Record<string, Category> = {}
    for (const catData of categoriesData) {
      categories[catData.slug] = await Category.updateOrCreate({ slug: catData.slug }, catData)
    }

    const getCategoryId = (name: string): number => {
      const lowerName = name.toLowerCase()
      if (lowerName.includes('leves')) return categories['levesek'].id
      if (lowerName.includes('főzelék') || lowerName.includes('káposzta'))
        return categories['fozelekek'].id
      if (
        lowerName.includes('fánk') ||
        (lowerName.includes('szelet') &&
          (lowerName.includes('üdítős') || lowerName.includes('gesztenyés'))) ||
        lowerName.includes('gombóc') ||
        lowerName.includes('linzer') ||
        lowerName.includes('nudli') ||
        lowerName.includes('lúdláb') ||
        lowerName.includes('tekercs')
      )
        return categories['desszertek'].id
      if (
        lowerName.includes('tészta') ||
        lowerName.includes('spagetti') ||
        lowerName.includes('nudli') ||
        lowerName.includes('csusza')
      )
        return categories['tesztak'].id
      if (
        lowerName.includes('rántott') ||
        lowerName.includes('sült') ||
        lowerName.includes('grill')
      )
        return categories['frissensultek'].id
      if (
        lowerName.includes('pörkölt') ||
        lowerName.includes('tokány') ||
        lowerName.includes('aprópecsenye') ||
        lowerName.includes('rakott')
      )
        return categories['keszetelek'].id
      return categories['egyeb'].id
    }

    for (const item of menuItems) {
      const { name, nameHu, nameEn, priceLarge, priceSmall } = item

      if (name) {
        await RestaurantMenuItem.updateOrCreate(
          { name },
          {
            name,
            nameHu,
            nameEn,
            priceLarge,
            priceSmall,
            categoryId: getCategoryId(nameHu),
          }
        )
      }
    }
  }
}
