import { BaseSchema } from '@adonisjs/lucid/schema'

export default class extends BaseSchema {
  async up() {
    this.schema.createTable('users', (table) => {
      table.increments('id')
      table.string('full_name').notNullable()
      table.string('email').notNullable().unique()
      table.string('role').notNullable().defaultTo('user')
      table.string('password').notNullable()

      table.timestamp('created_at', { useTz: true }).notNullable()
      table.timestamp('updated_at', { useTz: true }).notNullable()
    })

    this.schema.createTable('auth_access_tokens', (table) => {
      table.increments('id')
      table
        .integer('tokenable_id')
        .unsigned()
        .notNullable()
        .references('id')
        .inTable('users')
        .onDelete('CASCADE')
      table.string('type').notNullable()
      table.string('name').nullable()
      table.string('hash').notNullable()
      table.text('abilities').notNullable()
      table.timestamp('created_at')
      table.timestamp('updated_at')
      table.timestamp('last_used_at').nullable()
      table.timestamp('expires_at').nullable()
    })

    this.schema.createTable('categories', (table) => {
      table.increments('id')
      table.string('name').notNullable().unique()
      table.string('name_hu')
      table.string('name_en')
      table.string('slug').notNullable().unique()

      table.timestamp('created_at', { useTz: true }).notNullable()
      table.timestamp('updated_at', { useTz: true }).notNullable()
    })

    this.schema.createTable('restaurant_menus', (table) => {
      table.increments('id')
      table.string('name').notNullable()
      table.string('name_hu')
      table.string('name_en')

      table.timestamp('created_at', { useTz: true }).notNullable()
      table.timestamp('updated_at', { useTz: true }).notNullable()
    })

    this.schema.createTable('restaurant_menu_items', (table) => {
      table.increments('id')
      table.string('name').notNullable()
      table.string('name_hu')
      table.string('name_en')
      table.decimal('price_small', 10, 2).notNullable()
      table.decimal('price_large', 10, 2).notNullable()
      table.text('description')
      table.text('description_hu')
      table.text('description_en')
      table
        .integer('category_id')
        .unsigned()
        .references('id')
        .inTable('categories')
        .onDelete('SET NULL')

      table.timestamp('created_at', { useTz: true }).notNullable()
      table.timestamp('updated_at', { useTz: true }).notNullable()
    })

    this.schema.createTable('menu_item_menus', (table) => {
      table.increments('id')
      table
        .integer('restaurant_menu_item_id')
        .unsigned()
        .notNullable()
        .references('id')
        .inTable('restaurant_menu_items')
        .onDelete('CASCADE')
      table
        .integer('restaurant_menu_id')
        .unsigned()
        .notNullable()
        .references('id')
        .inTable('restaurant_menus')
        .onDelete('CASCADE')

      table.unique(['restaurant_menu_item_id', 'restaurant_menu_id'])
    })

    this.schema.createTable('rate_limits', (table) => {
      table.string('key', 255).notNullable().primary()
      table.integer('points', 9).notNullable().defaultTo(0)
      table.bigint('expire').unsigned()
    })
  }

  async down() {
    this.schema.dropTable('menu_item_menus')
    this.schema.dropTable('restaurant_menu_items')
    this.schema.dropTable('restaurant_menus')
    this.schema.dropTable('categories')
    this.schema.dropTable('auth_access_tokens')
    this.schema.dropTable('users')
    this.schema.dropTable('rate_limits')
  }
}
