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 villages
(
    id          bigint                                             not null,
    server_key  varchar(100)                                       not null
        references servers,
    name        varchar(150)                                       not null,
    points      bigint                   default 0,
    x           bigint                   default 0,
    y           bigint                   default 0,
    continent   varchar(5),
    bonus       bigint                   default 0,
    player_id   bigint,
    created_at  timestamp with time zone default CURRENT_TIMESTAMP not null,
    profile_url varchar(150),
    primary key (id, server_key)
);

create index if not exists villages_server_key_idx
    on villages (server_key);

create index if not exists villages_server_key_x_y_idx
    on villages (server_key, x, y);

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