import type { Knex } from 'knex';

/** SEO Phase 13: every 404 page, counted per path (admin → "Not found log"). */
export async function up(knex: Knex): Promise<void> {
  await knex.schema.createTable('not_found_log', (t) => {
    t.engine('InnoDB');
    t.charset('utf8mb4');
    t.collate('utf8mb4_unicode_ci');
    t.bigIncrements('id').unsigned().primary();
    t.string('path', 300).notNullable().unique();
    t.string('referrer', 300).nullable();
    t.integer('hits').unsigned().notNullable().defaultTo(1);
    t.timestamp('first_seen_at').notNullable().defaultTo(knex.fn.now());
    t.timestamp('last_seen_at').notNullable().defaultTo(knex.fn.now());
    t.index(['last_seen_at']);
    t.index(['hits']);
  });
}

export async function down(knex: Knex): Promise<void> {
  await knex.schema.dropTable('not_found_log');
}
