package maindb

import (
	"context"

	"mstore/pkg/descr"
)

func (db *Database) InsertAccount(ctx context.Context, account *descr.Account) error {
	var err error

	request := `INSERT INTO accounts(id, username, passhash, disabled, created_at, updated_at, created_by, updated_by)
                        VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`
	_, err = db.db.Exec(request, account.ID, account.Username, account.Passhash, account.Disabled,
		account.CreatedAt, account.UpdatedAt, account.CreatedBy, account.UpdatedBy)
	if err != nil {
		return err
	}
	return err
}

func (db *Database) UpdateAccountByID(ctx context.Context, accountID string, account *descr.Account) error {
	var err error
	request := `UPDATE accounts SET username = $1, passhash = $2, disabled = $3, updated_at = $4, updated_by = $5 WHERE id = $6`
	_, err = db.db.Exec(request, account.Username, account.Passhash, account.Disabled, account.UpdatedAt, account.UpdatedBy, accountID)
	if err != nil {
		return err
	}
	return err
}

func (db *Database) ReducedListAccounts(ctx context.Context) ([]descr.Account, error) {
	var err error
	request := `SELECT id, username, disabled, created_at, updated_at, created_by, updated_by FROM accounts`
	res := make([]descr.Account, 0)
	err = db.db.Select(&res, request)
	if err != nil {
		return res, err
	}
	return res, err
}

func (db *Database) ListAccounts(ctx context.Context) ([]descr.Account, error) {
	var err error
	request := `SELECT * FROM accounts`
	res := make([]descr.Account, 0)
	err = db.db.Select(&res, request)
	if err != nil {
		return res, err
	}
	return res, err
}

func (db *Database) GetAccountByID(ctx context.Context, accountID string) (bool, *descr.Account, error) {
	var err error
	var res *descr.Account
	var exists bool = false

	request := `SELECT * FROM accounts WHERE id = $1 LiMIT 1`
	dbRes := make([]descr.Account, 0)
	err = db.db.Select(&dbRes, request, accountID)
	if err != nil {
		return exists, res, err
	}
	if len(dbRes) == 0 {
		return exists, res, err
	}
	exists = true
	res = &dbRes[0]
	return exists, res, err
}

func (db *Database) GetAccountByUsername(ctx context.Context, username string) (bool, *descr.Account, error) {
	var err error
	var res *descr.Account
	var exists bool

	request := `SELECT * FROM accounts WHERE username = $1 LIMIT 1`
	dbRes := make([]descr.Account, 0)
	err = db.db.Select(&dbRes, request, username)
	if err != nil {
		return exists, res, err
	}

	if len(dbRes) == 0 {
		return false, res, err
	}
	exists = true
	res = &dbRes[0]
	return exists, res, err
}

func (db *Database) DeleteAccountByID(ctx context.Context, accountID string) error {
	var err error

	request := `DELETE FROM accounts WHERE id = $1`
	_, err = db.db.Exec(request, accountID)
	if err != nil {
		return err
	}
	return err
}

func (db *Database) DeleteAccountByUsername(ctx context.Context, username string) error {
	var err error

	request := `DELETE FROM accounts WHERE username = $1`
	_, err = db.db.Exec(request, username)
	if err != nil {
		return err
	}
	return err
}
