package migrations

import (
	"context"

	"github.com/uptrace/bun"
)

func init() {
	migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
		_, err := db.ExecContext(ctx, `
create table if not exists ennoblements
(
    ?ID_COL,
    server_key   varchar(100)                                       not null
        references servers,
    village_id   bigint                                             not null,
    new_owner_id bigint,
    new_tribe_id bigint,
    old_owner_id bigint,
    old_tribe_id bigint,
    points       bigint                   default 0,
    created_at   timestamp with time zone default CURRENT_TIMESTAMP not null
);

create index if not exists ennoblements_server_key_created_at_idx
    on ennoblements (server_key, created_at);

create index if not exists ennoblements_server_key_village_id_idx
    on ennoblements (server_key, village_id);

create index if not exists ennoblements_server_key_new_owner_id_idx
    on ennoblements (server_key, new_owner_id);

create index if not exists ennoblements_server_key_old_owner_id_idx
    on ennoblements (server_key, old_owner_id);

create index if not exists ennoblements_server_key_new_tribe_id_idx
    on ennoblements (server_key, new_tribe_id);

create index if not exists ennoblements_server_key_old_tribe_id_idx
    on ennoblements (server_key, old_tribe_id);
`)
		return err
	}, func(ctx context.Context, db *bun.DB) error {
		_, err := db.ExecContext(ctx, "drop table if exists ennoblements cascade;")
		return err
	})
}
