package db

func (d *DB) ListAllowedMembers() ([]string, error) {
	rows, err := d.Query(`select did from known_dids`)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var dids []string
	for rows.Next() {
		var did string
		if err := rows.Scan(&did); err != nil {
			return nil, err
		}
		dids = append(dids, did)
	}

	return dids, rows.Err()
}

func (d *DB) IsAllowedUser(did string, isPublic bool) (bool, error) {
	var blocked bool
	if err := d.QueryRow(`select exists (select 1 from blocked_dids where did = ?)`, did).Scan(&blocked); err != nil {
		return false, err
	}
	if blocked {
		return false, nil
	}
	if isPublic {
		return true, nil
	}

	var allowed bool
	err := d.QueryRow(`select exists (select 1 from known_dids where did = ?)`, did).Scan(&allowed)
	return allowed, err
}

func (d *DB) AllowUser(did string) error {
	panic("unimplemented")
}

func (d *DB) BlockUser(did string) error {
	panic("unimplemented")
}
