# frozen_string_literal: true

class CreateUxToolkitChecklistTemplates < ActiveRecord::Migration[6.1]
  def up
    unless table_exists?(:ux_toolkit_checklist_templates)
      create_table :ux_toolkit_checklist_templates do |t|
        t.string :name, null: false
        t.boolean :active, null: false, default: true
        t.timestamps
      end
    end

    unless index_exists?(:ux_toolkit_checklist_templates, :name)
      add_index :ux_toolkit_checklist_templates, :name
    end

    unless index_exists?(:ux_toolkit_checklist_templates, :active)
      add_index :ux_toolkit_checklist_templates, :active
    end

    unless table_exists?(:ux_toolkit_checklist_template_items)
      create_table :ux_toolkit_checklist_template_items do |t|
        t.integer :checklist_template_id, null: false
        t.string :subject, null: false
        t.integer :position, null: false, default: 1
        t.timestamps
      end
    end

    unless index_exists?(:ux_toolkit_checklist_template_items, :checklist_template_id)
      add_index :ux_toolkit_checklist_template_items,
                :checklist_template_id,
                name: 'idx_ux_template_items_template_id'
    end

    unless index_exists?(:ux_toolkit_checklist_template_items, [:checklist_template_id, :position],
                         name: 'index_ux_toolkit_template_items_on_template_and_position')
      add_index :ux_toolkit_checklist_template_items,
                [:checklist_template_id, :position],
                name: 'index_ux_toolkit_template_items_on_template_and_position'
    end
  end

  def down
    drop_table :ux_toolkit_checklist_template_items if table_exists?(:ux_toolkit_checklist_template_items)
    drop_table :ux_toolkit_checklist_templates if table_exists?(:ux_toolkit_checklist_templates)
  end
end
