package models

import (
	"context"

	"github.com/uptrace/bun"
)

type WhiteList struct {
	bun.BaseModel    `bun:"table:whitelist"`
	UserID           int64  `bun:",pk,autoincrement"`
	Name             string `bun:"name,type:text,unique" json:"name" yaml:"name"`
	TelegramID       int64  `bun:"telegram_id,type:int,unique" json:"telegram_id" yaml:"telegram_id"`
	TelegramUsername string `bun:"telegram_username,type:text,unique" json:"telegram_username" yaml:"telegram_username"`
}

var _ bun.AfterCreateTableHook = (*WhiteList)(nil)

func (*WhiteList) AfterCreateTable(ctx context.Context, query *bun.CreateTableQuery) error {
	_, err := query.DB().NewCreateIndex().Model((*WhiteList)(nil)).Index("idx_wl_telegram_id").Column("telegram_id").Exec(ctx)
	if err != nil {
		return err
	}
	_, err = query.DB().NewCreateIndex().Model((*WhiteList)(nil)).Index("idx_wl_telegram_username").Column("telegram_username").Exec(ctx)
	if err != nil {
		return err
	}
	return nil
}
