import { schemaMigrations, addColumns, createTable, unsafeExecuteSql } from '@nozbe/watermelondb/Schema/migrations';

export const migrations = schemaMigrations({
  migrations: [
    {
      toVersion: 2,
      steps: [
        addColumns({
          table: 'tasks',
          columns: [{ name: 'repeat', type: 'string' }],
        }),
      ],
    },
    {
      toVersion: 3,
      steps: [
        addColumns({
          table: 'tasks',
          columns: [
            { name: 'repeat_interval', type: 'number', isOptional: true },
            { name: 'repeat_days', type: 'string', isOptional: true },
            { name: 'color', type: 'string', isOptional: true },
            { name: 'series_id', type: 'string', isOptional: true },
          ],
        }),
      ],
    },
    {
      toVersion: 4,
      steps: [
        addColumns({
          table: 'tasks',
          columns: [{ name: 'reminder', type: 'string', isOptional: true }],
        }),
      ],
    },
    {
      toVersion: 5,
      steps: [
        addColumns({
          table: 'tasks',
          columns: [{ name: 'end_time', type: 'string', isOptional: true }],
        }),
      ],
    },
    {
      toVersion: 6,
      steps: [
        createTable({
          name: 'repeat_profiles',
          columns: [
            { name: 'name', type: 'string', isOptional: true },
            { name: 'repeat', type: 'string', isOptional: true },
            { name: 'repeat_interval', type: 'number', isOptional: true },
            { name: 'repeat_days', type: 'string', isOptional: true },
            { name: 'created_at', type: 'number' },
            { name: 'updated_at', type: 'number' },
          ],
        }),
      ],
    },
    {
      toVersion: 7,
      steps: [
        addColumns({
          table: 'tasks',
          columns: [{ name: 'assignee_id', type: 'string', isOptional: true }],
        }),
      ],
    },
    {
      toVersion: 8,
      steps: [
        addColumns({
          table: 'tasks',
          columns: [{ name: 'all_day', type: 'boolean', isOptional: true }],
        }),
      ],
    },
    {
      toVersion: 9,
      steps: [
        addColumns({
          table: 'subtasks',
          columns: [
            { name: 'description', type: 'string', isOptional: true },
            { name: 'priority', type: 'string', isOptional: true },
            { name: 'due_date', type: 'number', isOptional: true },
            { name: 'due_time', type: 'string', isOptional: true },
            { name: 'end_time', type: 'string', isOptional: true },
            { name: 'reminder', type: 'string', isOptional: true },
            { name: 'assignee_id', type: 'string', isOptional: true },
          ],
        }),
      ],
    },
    {
      toVersion: 10,
      steps: [
        addColumns({
          table: 'subtasks',
          columns: [
            { name: 'all_day', type: 'boolean', isOptional: true },
            { name: 'repeat', type: 'string', isOptional: true },
            { name: 'repeat_interval', type: 'number', isOptional: true },
            { name: 'repeat_days', type: 'string', isOptional: true },
            { name: 'series_id', type: 'string', isOptional: true },
          ],
        }),
      ],
    },
    {
      toVersion: 11,
      steps: [
        addColumns({
          table: 'tasks',
          columns: [{ name: 'reminders', type: 'string', isOptional: true }],
        }),
        addColumns({
          table: 'subtasks',
          columns: [{ name: 'reminders', type: 'string', isOptional: true }],
        }),
      ],
    },
    {
      toVersion: 12,
      steps: [
        createTable({
          name: 'friendships',
          columns: [
            { name: 'user_id', type: 'string', isIndexed: true },
            { name: 'friend_id', type: 'string', isIndexed: true },
            { name: 'status', type: 'string' },
            { name: 'created_at', type: 'number' },
            { name: 'updated_at', type: 'number' },
          ],
        }),
      ],
    },
    {
      toVersion: 13,
      steps: [
        addColumns({
          table: 'tasks',
          columns: [{ name: 'completed_at', type: 'number', isOptional: true }],
        }),
      ],
    },
    {
      toVersion: 14,
      steps: [
        unsafeExecuteSql('CREATE INDEX IF NOT EXISTS idx_tasks_updated_at ON tasks(updated_at);'),
        unsafeExecuteSql('CREATE INDEX IF NOT EXISTS idx_tasks_created_at ON tasks(created_at);'),
        unsafeExecuteSql('CREATE INDEX IF NOT EXISTS idx_tasks_completed ON tasks(completed);'),
        unsafeExecuteSql('CREATE INDEX IF NOT EXISTS idx_subtasks_updated_at ON subtasks(updated_at);'),
        unsafeExecuteSql('CREATE INDEX IF NOT EXISTS idx_subtasks_created_at ON subtasks(created_at);'),
        unsafeExecuteSql('CREATE INDEX IF NOT EXISTS idx_categories_updated_at ON categories(updated_at);'),
        unsafeExecuteSql('CREATE INDEX IF NOT EXISTS idx_repeat_profiles_updated_at ON repeat_profiles(updated_at);'),
        unsafeExecuteSql('CREATE INDEX IF NOT EXISTS idx_friendships_updated_at ON friendships(updated_at);'),
        unsafeExecuteSql('CREATE INDEX IF NOT EXISTS idx_tasks_series_id ON tasks(series_id);'),
      ],
    },
    {
      toVersion: 15,
      steps: [
        addColumns({
          table: 'subtasks',
          columns: [
            { name: 'parent_subtask_id', type: 'string', isIndexed: true, isOptional: true },
          ],
        }),
      ],
    },
    {
      toVersion: 16,
      steps: [
        addColumns({
          table: 'tasks',
          columns: [{ name: 'order', type: 'number', isOptional: true }],
        }),
      ],
    },
    {
      toVersion: 17,
      steps: [
        createTable({
          name: 'tombstones',
          columns: [
            { name: 'entity', type: 'string' },
            { name: 'entity_id', type: 'string', isIndexed: true },
            { name: 'deleted_at', type: 'number', isIndexed: true },
          ],
        }),
      ],
    },
    {
      toVersion: 18,
      steps: [
        addColumns({
          table: 'tasks',
          columns: [{ name: 'tags', type: 'string', isOptional: true }],
        }),
      ],
    },
    {
      toVersion: 19,
      steps: [
        addColumns({
          table: 'subtasks',
          columns: [{ name: 'category_id', type: 'string', isOptional: true }],
        }),
      ],
    },
    {
      toVersion: 20,
      steps: [
        addColumns({
          table: 'subtasks',
          columns: [{ name: 'tags', type: 'string', isOptional: true }],
        }),
      ],
    },
  ],
});
